Page 1 of 1

Output edited XML tree to file

Posted: Wed May 24, 2006 11:31 am
by cthrax
I will start off by saying I am working with XML and DOM for the first time.

The idea is that I will be able to use PHP to generate multiple files into multiple directories. These files will each be based off of a single XML file and changed minutely before being sent to their respective destinations. (I'm basically being lazy so that I only have to change one file to change all the files).

I thought it would be as simple as navigating the tree and inserting before a certain node or appending a child to a node. I am finding however that the node manipulation doesn't appear to change the tree. Only when the document uses a node method does it change anything. For example,

Code: Select all

$doc = new DOMDocument('1.0', 'iso-8859-1');
 $doc->load("test.xml");
 $ele = $doc->getElementsByTagName("ul");
 	if($ele->item(1)->hasChildNodes()){
 		$childnodes = $ele->item(1)->childNodes;
                
                //Doesn't change tree at all
                $childnodes->item(0)->insertBefore($doc->createTextNode("hello");  
                
                //Inserts into very beginning of document
                $doc->insertBefore($doc->createTextNode("hello");  
        }
 $doc->saveHTMLFile("test.html");

I don't understand why a DOMDocument Object can use a DOMNode method, nor why the navigation of the children does not insert properly. Is it because the lists that are returned by "childNodes" are only copies of the actual nodes? Do I need to be using XPath to do what I want? (I've been trying to avoid using XPath because as I have said, I've only recently done any work in XML or DOM and would like to get those down a little more solid before tackling another query language).

Any help is greatly appreciated. Thank you in advance.