Void-Droid Posted August 14, 2016 Share Posted August 14, 2016 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 of 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 & 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 & 1, but instead it's a child of Level 3 (?!?!?!?!)</nodelevel4> </rootnode> As you can see: Something went wrong because 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 questions are: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? And if nothing helps: What workaround solutions do you suggest? Greetz Void-Droid Quote Link to comment https://forums.phpfreaks.com/topic/301889-xml-creation-problem-empty-node/ Share on other sites More sharing options...
Solution Jacques1 Posted August 14, 2016 Solution Share Posted August 14, 2016 (edited) Your output doesn't make any sense at all. The XML declaration is fudged up, the tags are wrong (since they've been changed to all-lowercase), and the output shouldn't even be formatted. Either you haven't provided your real code, or there's something which messes with the XML output. 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 & 1, but instead it's a child of Level 3 (?!?!?!?!)</NodeLevel4> </RootNode> If you get something else, save the return value of $xmlHandler->SaveXml() in a file on your server and inspect that file. Edited August 14, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301889-xml-creation-problem-empty-node/#findComment-1536146 Share on other sites More sharing options...
Void-Droid Posted August 14, 2016 Author Share Posted August 14, 2016 Your output doesn't make any sense at all. The XML declaration is fudged up, the tags are wrong (since they've been changed to all-lowercase), and the output shouldn't even be formatted. Either you haven't provided your real code, or there's something which messes with the XML output. 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 & 1, but instead it's a child of Level 3 (?!?!?!?!)</NodeLevel4> </RootNode> If you get something else, save the return value of $xmlHandler->SaveXml() in a file on your server and inspect that file. You, sir, are a true genius! That was ist! You: 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! You: and the output shouldn't even be formatted Me: I did that myself for a better view You: This is the raw output of $xmlHandler->SaveXml() after enabling formatted output Me: After printing it out into a file, i got the same result. The conclusion: I wrote the saveXml-output into a file. This is the output (after formatting it by myself): <?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 & 1, but instead it's a child of Level 3 (?!?!?!?!)</NodeLevel4> </RootNode> 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 sourcecode. As you can see in my first post, i used echo with "<p style='display:none;'>" 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)! Your solution made may day. Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/301889-xml-creation-problem-empty-node/#findComment-1536147 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.