Void-Droid Void-Droid - 3 months ago 5
PHP Question

PHP & XML Creation: Node in wrong parent

I am working currently on an OOP based PHP project in MVC style.
For my project i need to create/send/recieve/process XMLs.

Now i have a BIG problem with creating XML-Structures with DOMDocument.
Everytime i create a new XML-Node without an attributes or values, all nodes afterwards will be a child if this node!

In other words: I can not create an empty XML-Node without all nodes afterwards beeing a child of this empty node!!!

This problem bugs me for while now but I really need the way I am dealing right now with the XML creation.
I couldn't find any solutions but some similar problems.

This PHP tests my XmlHandler-Class, which creates the XML-Request:

Test.php:

<?php

include "Handler/XmlHandler.php";


$xmlHandler=new XmlHandler();

$xmlHandler->CreateNewXmlInstance();
$root = $xmlHandler->CreateRootNode('RootNode');
$l1 = $xmlHandler->AppendNodeWithChild($root, "NodeLevel1", "Text1 - This node one has text");
$l2 = $xmlHandler->AppendNodeWithChild($root, "NodeLevel2", "Text2 - Next node Level3 level is not gonna have text");
$l21 = $xmlHandler->AppendNodeWithChild($l2, "NodeLevel2_1", "Text2_1 - This node will be a child of Level2, everything fine");
$l3 = $xmlHandler->AppendNodeWithChild($root, "NodeLevel3", "");
$l4 = $xmlHandler->AppendNodeWithChild($root, "NodeLevel4", "Text4 - This node should be on same level like 3, 2 & 1, but instead it's a child of Level 3 (?!?!?!?!)");

echo "<p style='display:none;'>".$xmlHandler->SaveXml()."</p>";

?>


Here is the XML-Handler Class which i use to create the XML-Request-Structure (i just posted the neccessary parts of the class here)

XmlHandler.php:

<?php

class XmlHandler{


private $xml;

/**
*
* Constructor
*
*/

function __construct()
{
$this->xml=null;

}

//[...]

/*
* Custom XML-Creator Functions
*
*/

public function CreateNewXmlInstance(){
/*********************************************/
/** XML DOM example of building XML Request **/
/*********************************************/
$this->xml = new DOMDocument('1.0', 'UTF-8');
return $this->xml;
}

public function CreateRootNode($name){
$rootElement = $this->xml->appendChild( $this->xml->createElement($name) );
return $rootElement;
}

public function AppendNodeWithChild($node, $childName, $childText){
$result = $node->appendChild($this->xml->createElement($childName));
if(null != $childText && !empty($childText)){
$result->appendChild( $this->xml->createTextNode($childText) );
}

return $result;
}

public function SetNodeAttributes($node, $nameAndValues){
if(null != $nameAndValues && sizeof($nameAndValues) > 0){
foreach($nameAndValues as $name => $value){
$this->SetNodeAttribute($node, $name, $value);
}
}
}

public function SetNodeAttribute($node, $name, $value){
$node->setAttribute($name, $value);
}

public function SaveXml(){
return $this->xml->saveXML();
}

//[...]

}

?>


This is the Result:

<!-- ?xml version="1.0" encoding="UTF-8"? -->
<rootnode>
<nodelevel1>Text1 - This node one has text</nodelevel1>
<nodelevel2>Text2 - Next node Level3 level is not gonna have text
<nodelevel2_1>Text2_1 - This node will be a child of Level2, everything fine</nodelevel2_1>
</nodelevel2>
<nodelevel3>
<nodelevel4>Text4 - This node should be on same level like 3, 2 &amp; 1, but instead it's a child of Level 3 (?!?!?!?!)</nodelevel4>
</nodelevel3>
</rootnode>


But in theory, there should be somethign like that:

<!-- ?xml version="1.0" encoding="UTF-8"? -->
<rootnode>
<nodelevel1>Text1 - This node one has text</nodelevel1>
<nodelevel2>Text2 - Next node Level3 level is not gonna have text
<nodelevel2_1>Text2_1 - This node will be a child of Level2, everything fine</nodelevel2_1>
</nodelevel2>
<nodelevel3/>
<nodelevel4>Text4 - This node should be on same level like 3, 2 &amp; 1, but instead it's a child of Level 3 (?!?!?!?!)</nodelevel4>
</rootnode>


As you can see: Something went wrong when i had not set a value for the new created Node on Level3!:

$l3 = $xmlHandler->AppendNodeWithChild($root, "NodeLevel3", "");
$l4 = $xmlHandler->AppendNodeWithChild($root, "NodeLevel4", "Text4 - This node should be on same level like 3, 2 & 1, but instead it's a child of Level 3 (?!?!?!?!)");


As long as i set attributes or put in value sin the new created node, everything is fine.
But i have some situations where also pure empty nodes have to be created!

My question is:

What am I doing wrong here?

Or does PHP do something wrong?

Maybe my browser does a bad preparation of the XML, but the outgoing XML request was build and send correctly and the mistake lies something else?

If so, how can I check the XML request though?

Edit Nr.2:

I changed my question/original post somehow.
The above example is a bit more easy to understand.
At least i hope so.

Answer

Ok guys, somebody helped me with this problem. Here is the conversation:

Him: The tags are wrong (since they've been changed to all-lowercase)

Me: Yes, it seems the XML gets messy after echoing it out into the browser instead of saving it into a file!

Him: And the output shouldn't even be formatted

Me: I did that myself for a better view

Him: This is the raw output of $xmlHandler->SaveXml() after enabling formatted output

<?xml version="1.0" encoding="UTF-8"?>
<RootNode>
  <NodeLevel1>Text1 - This node one has text</NodeLevel1>
  <NodeLevel2>Text2 - Next node Level3 level is not gonna have text<NodeLevel2_1>Text2_1 - This node will be a child of Level2, everything fine</NodeLevel2_1></NodeLevel2>
  <NodeLevel3/>
  <NodeLevel4>Text4 - This node should be on same level like 3, 2 &amp; 1, but instead it's a child of Level 3 (?!?!?!?!)</NodeLevel4>
</RootNode>

Me: After printing it out into a file, i got the same result as you.

The conclusion:

Now I've wrote the saveXml-output into a file. The Result is he same as Him told me.

So were was the mess?

I thoght it would be enough to print the XML out into the browser with echo and check it through the HTML-sourcecode via FireFox.

As you can see in my first post, i used echo...

echo "<p style='display:none;'>".$xmlHandler->SaveXml()."</p>";

to somehow print out the result of my XML-Creation (because i didn't know how else).

Therefore the XML-Creation was a full success from start up and the error i get from the response-Server lies somewhere else (from now on i can not rely on your help anymore)!

Comments