如何用PHP解析实体内容到简单的xml字符串ID属性


How with PHP parse entity content through to simple xml string ID attributes

下面是提供实时URL xml提要的" $image1 "实体的当前结果

$output .= "<image>".$image1."</image>'n"; 

下面是我的工作ID属性,需要上面的内容(URL)实体

$output .= $string = <<<XML
<images>
     <image id='1'><url></url></image>
    </images> 
  XML;

这段代码产生以下XML提要:

<root>
 <property>
      <images>
            <image id='1'><url> </url></image>
       </images>

请问我的问题是什么PHP代码!数组!需要有内容$image1 (URL)现在在xml字符串的URL标签内工作?

我想要的结果在我当前的文档中:

    <root>
        <property>
         <images>
              <image id='1'><url>$image1(url required from this entity as above)/url>   </image>
    </images>

你可以这样使用DOMDocument:

$document = new DOMDocument();
$images = $document->createElement('images');
$image = $document->createElement('image');
$url = $document->createElement('url', 'www.mydomain.com');
$document->appendChild($images);
$images->appendChild($image);
$image->setAttribute('id', 1);
$image->appendChild($url);
print $document->saveXML();
// or
$document->save("/path/to/file.xml");