0

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');
    $node2->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

user253530
  • 2,583
  • 13
  • 44
  • 61
  • `return a DOMNode ?` -> Yes, see http://php.net/manual/en/domnode.appendchild.php – hakre Sep 29 '11 at 22:56
  • i have edited my question, please check it out and tell me what you think..this is pretty annoying. – user253530 Sep 29 '11 at 23:02
  • You should enable [error reporting](http://php.net/manual/en/function.error-reporting.php), that code gives: PHP Fatal error: Call to undefined method DOMNode::createTextNode(). – hakre Sep 29 '11 at 23:05
  • ok if you know how to do it can you post the answer using that code i just added? i am really stuck here. I get this error now: PHP Warning: DOMNode::appendChild() [domnode.appendchild]: Couldn't fetch DOMNode – user253530 Sep 29 '11 at 23:13
  • What should `new DOMNode('aaa');` do? Can you explain that to me? Then I might better understand what your problem is, I don't know the code, I can only help you, that's all. – hakre Sep 29 '11 at 23:22
  • i have changed the code snippet and added comments. do you understand now? – user253530 Sep 29 '11 at 23:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3916/discussion-between-user253530-and-hakre) – user253530 Sep 29 '11 at 23:27
  • Nah, let's continue in comments. – hakre Sep 29 '11 at 23:28
  • ok post your explanation here...either way is good as long as i get this running – user253530 Sep 29 '11 at 23:33
  • I repeat my last question again as it's not yet answered. What should `new DOMNode('aaa');` do? What is it? – hakre Sep 29 '11 at 23:35
  • i have edited my question again. do you undertand now? – user253530 Sep 29 '11 at 23:42
  • Ah, you want to add a `DOMElement` which is a *sub-type of* `DOMNode` - try with `new DOMElement('aaa');` instead. – hakre Sep 29 '11 at 23:46
  • no, i want to create a structure of nodes that can be appended wherever i want. Basically i want my functions to create small Nodes (that can have child nodes which again can have child nodes) and then return them. The returned data should be appended in a DOMDocument. Post some sample code if you understand my request. – user253530 Sep 29 '11 at 23:56
  • `DOMElement` **is** a `DOMNode`, you can do it with that. Just add small `DOMElement` Nodes, add children to them and then append it to where-ever you want. Unless you can't do it with `DOMElement` is still can not understand what you request, so I can't post code. But I have the feeling you're really close to get it finished. – hakre Sep 30 '11 at 00:04
  • no it didn't do the trick...thanks very much for the help. I'm going to send the DOMDocument and element in which to append the data as params to the functions. That works. – user253530 Sep 30 '11 at 00:14

1 Answers1

0

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");
?>
matb33
  • 2,820
  • 1
  • 19
  • 28