开发者

Building an xml document from functions that return DOMNodes

My question is a rather simple one for anyone familiar with the DOM* classes in PHP. Basically i have different classes that i want to return to me something that I can append in my xml document

Following pseudo-code should demonstrate better

Class ChildObject{ function exportToXML( return a DOMNode ? ) }

Class ContainerObject{ 
function exportToXML(){
    $domSomething = new DOM*SOMETHING*;
    foreach($children as $child) $domSomething->appendChild($child->exportToXML);
    return $domSomething ;
} 
}


Now i want to create the entire DOMDocument

$xml = new DOMDocument();
$root = $xml->createElement('root');
foreach($containers as $container) $root->appendChild($container->exportToXML());

I tried sending the DOMDocument object as a reference, did not work. I tried creating DOMNodes but didn't work as well....so i'm looking at a simple answer: what datatypes do i need to return in order for me to achieve the above functionality?

<?php
    $xml = new DOMDocument();
    $h = $xml->createElement('hello');

    $node1 = new DOMNode('aaa'); 
    $node1->appendChild(new DOMText('new text content'));
    //node1 is being returned by a function

    $node2 = new DOMNode('bbb');
    $n开发者_如何学Goode2->appendChild(new DOMText('new text content'));
    //node2 is being returned by some other function

    $h->appendChild($node1);//append to this element the returned node1
    $h->appendChild($node2);//append to this element the returned node2

    $xml->appendChild($h);//append to the document the root node

    $content = $xml->saveXML();
    file_put_contents('xml.xml', $content);//output to an xml file
?>

The above code should do the following:

consider that i want to build the following xml

<hello>
 <node1>aaa</node1>
 <node2>bbb</node2>
</hello>

node1 could be again a node that has multiple children so node1 could be as well as something like this:

<node1>
 <child1>text</child1>
 <child2>text</child2>
 <child3>
  <subchild1>text</subchild1>
 </child3>
</node1>

Basically when i call exportToXML() something should be returned, call it $x that i can append in my document using $xml->appendChild($x);

I want to create the above structure and return the object that can be appended in the DOMDocument


The following code:

<?php
$xml = new DOMDocument();
$h = $xml->appendChild($xml->createElement('hello'));

$node1 = $h->appendChild($xml->createElement('aaa'));
$node1->appendChild($xml->createTextNode('new text content'));

$node2 = $h->appendChild($xml->createElement('bbb'));
$node2->appendChild($xml->createTextNode('new text content'));

$xml->save("xml.xml");
?>

will produce:

<?xml version="1.0"?>
<hello>
    <aaa>new text content</aaa>
    <bbb>new text content</bbb>
</hello>

Your example XML showed <node1>aaa</node1> but I think your various code snippet examples went out of sync when you were editing =) In case you need that output, try:

<?php
$xml = new DOMDocument();
$h = $xml->appendChild($xml->createElement('hello'));

$node1 = $h->appendChild($xml->createElement('node1'));
$node1->appendChild($xml->createTextNode('aaa'));

$node2 = $h->appendChild($xml->createElement('node2'));
$node2->appendChild($xml->createTextNode('bbb'));

$xml->save("xml.xml");
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜