Page 1 of 1
Quick question: Adding children in XML
Posted: Tue May 29, 2007 5:28 pm
by McManCSU
I want my code to add children within my xml file if the queried tag does not exist, but I can't figure how to add it into the proper section within the nodes.
For example:
Code: Select all
$newText = 'New Text';
$entries = $this->getQuery($query); // does a query using a DOMXPath object
$curTag = $entries->item(0);
if ($curTag == null)
{
echo "No entries found, creating it now...\n";
$node = $this->_doc->createElement($entries, "$newText");
$newnode = $this->_doc->appendChild($node);
return;
}
Of course, there are problems with the code above ($entries is NULL and not a text field), but if you could help me out on this, I would appreciate it! Also, I would prefer to maintain some sort of order (ie maybe use insertBefore(), but if none of the children exist around it, I dont know how to find the 'next best' place to insert the node...
Thanks!
Posted: Wed May 30, 2007 1:16 am
by volka
You better test
if ( 0==$entries->lenght ) instead of
$curTag = $entries->item(0);
if ($curTag == null)
McManCSU wrote:$node = $this->_doc->createElement($entries, "$newText");
createElement takes the new element's name as first parameter. But $entries is still the empty DOMNodeList returned by getQuery.
Posted: Wed May 30, 2007 9:08 am
by McManCSU
Right, thats what I was meaning when I said it wasn't right

. How can I insert the right node in the right location? Do I use something like the above or what?
Posted: Wed May 30, 2007 9:25 am
by volka
Define "right location".
createElement() does what its name says, it creates an element. createElement('nameOfTheElement', 'contents of the new element');
appendChild() also does what its name says, if you call $elem->appendChild(...) it appends the given node as a child to $elem.
Posted: Wed May 30, 2007 9:27 am
by McManCSU
Right place in the tree:
Code: Select all
<xyz>
<abc>
<def></def>
<NEW_NODE></NEW_NODE>
<sss></sss>
</abc>
</xyz>
Or even to just append the node after <sss>.
I am assuming that creating a node and appending it will put it after the /abc tag??
Thanks
Posted: Wed May 30, 2007 9:31 am
by volka
http://de2.php.net/manual/en/function.d ... before.php might be of interest.
McManCSU wrote:I am assuming that creating a node and appending it will put it after the /abc tag??
if you call $xyz->appendChild($newNode) where $xyz repesents the <xyz> element in your example, yes.
If you call $abc->appendChild($newNode) it will appear "after" <sss>
Posted: Wed May 30, 2007 9:47 am
by McManCSU
Ok, we're getting closer. Now, what if no other nodes around it exist, say we want to add <xyz>-><bbb>-><ccc>-><new_node>? Now, I am not sure that the append child will work since there is only one 'common' node (<xyz>)...
Code: Select all
<xyz>
<abc>
<def></def>
<NEW_NODE></NEW_NODE>
<sss></sss>
</abc>
<bbb>
<ccc>
<new_node></new_node>
</ccc>
</bbb>
</xyz>
In case you are wondering what I am doing, I am creating an interface that will first search for data, then replace/add data from the xml doc. The data is user inputted configurations (from POST array). The reason some of it may or may not be there is because I want to reduce the overhead of adding additional inputs to the configuration pages. So, if it doesn't exist, create it (which is easier said than done it appears)...
Posted: Wed May 30, 2007 10:09 am
by volka
Your document has always a so-called "document element" (otherwise it is not an xml document). This element can be accessed by the property
documentElement of the DOMDocument object, in your case $this->_doc->documentElement. There is one, exactly one document element.
From there on you can append childs to any element you like. But you must know to which element you want to append a new node. There is no magic function
putTheNodeWhereItBelongs() 
Posted: Wed May 30, 2007 11:27 am
by McManCSU
Thanks. Ya, it would be nice to have a function like query($str) where you create a node (or nodes in the path) defined by $str...