UltimateWeapon Posted February 16, 2011 Share Posted February 16, 2011 I have the following DOM document ( this is a sample document and is "as is", it's 100% intended to look like this ): <everything> <cars> <car>ladia</car> <car>ferrari</car> <garage>big blue building</garage> i also have a McLaren F1 but i wont brag about it </cars> <houses> <house>Big Red House</house> </houses> </everything> This is all being put into a $newdoc = new DOMDocument(); $myString = ................. ; // ????? I only want the content of <cars> in here, and without having the <cars> tag How can I make this "$myString look like the following: $myString = ' <car>ladia</car> <car>ferrari</car> <garage>big blue building</garage> i also have a McLaren F1 but i wont brag about it '; Preferably without any looping. Quote Link to comment https://forums.phpfreaks.com/topic/227811-converting-part-of-dom-xml-document-to-a-string/ Share on other sites More sharing options...
UltimateWeapon Posted February 17, 2011 Author Share Posted February 17, 2011 Can anyone help me with this please? Quote Link to comment https://forums.phpfreaks.com/topic/227811-converting-part-of-dom-xml-document-to-a-string/#findComment-1175443 Share on other sites More sharing options...
cgsmith105 Posted February 17, 2011 Share Posted February 17, 2011 UltimateWeapon, You are asking a couple questions that conflict with one another. Try http://us3.php.net/manual/en/simplexmlelement.getname.php for your solution. Quote Link to comment https://forums.phpfreaks.com/topic/227811-converting-part-of-dom-xml-document-to-a-string/#findComment-1175531 Share on other sites More sharing options...
UltimateWeapon Posted February 17, 2011 Author Share Posted February 17, 2011 UltimateWeapon, You are asking a couple questions that conflict with one another. Try http://us3.php.net/manual/en/simplexmlelement.getname.php for your solution. Conflict ? I have no idea what you mean. And that page hasn't got much to do with my question either Quote Link to comment https://forums.phpfreaks.com/topic/227811-converting-part-of-dom-xml-document-to-a-string/#findComment-1175784 Share on other sites More sharing options...
cgsmith105 Posted February 17, 2011 Share Posted February 17, 2011 I only want the content of <cars> in here, and without having the <cars> tag SimpleXML will do this for you. Quote Link to comment https://forums.phpfreaks.com/topic/227811-converting-part-of-dom-xml-document-to-a-string/#findComment-1175835 Share on other sites More sharing options...
salathe Posted February 17, 2011 Share Posted February 17, 2011 Welcome to PHPFreaks, UltimateWeapon. This seems like an odd request, and an even odder example file to work with, but I'll humour you. Here's a snippet which, using DOM as you wanted, takes the source XML and, via a DOMDocumentFragment (which many people have never even heard of, so don't worry if you haven't), outputs the content of that <cars> element. The important stuff is overly-commented within the if() block. $xml = ' <everything> <cars> <car>ladia</car> <car>ferrari</car> <garage>big blue building</garage> i also have a McLaren F1 but i wont brag about it </cars> <houses> <house>Big Red House</house> </houses> </everything> '; $newdoc = new DOMDocument(); $newdoc->loadXML($xml); $cars = $newdoc->getElementsByTagName('cars'); $myString = ''; if ($cars->length > 0) { // Get the first cars element $cars = $cars->item(0); // Create a new, empty DOMDocumentFragment $fragment = $newdoc->createDocumentFragment(); // Loop over cars childNodes and add a copy to the fragment foreach ($cars->childNodes as $child) { $fragment->appendChild($child->cloneNode(true)); } // Save fragment as an XML string $myString = $newdoc->saveXML($fragment); } var_dump($myString); To read: DOMDocumentFragment DOMNode::cloneNode() DOMDocument::saveXML() Quote Link to comment https://forums.phpfreaks.com/topic/227811-converting-part-of-dom-xml-document-to-a-string/#findComment-1175857 Share on other sites More sharing options...
UltimateWeapon Posted February 21, 2011 Author Share Posted February 21, 2011 Hi, sorry for my late response, I just got back from a small vacation. Thank you for your help, i'll look into it today and mark it as solved if it works out for me Quote Link to comment https://forums.phpfreaks.com/topic/227811-converting-part-of-dom-xml-document-to-a-string/#findComment-1177554 Share on other sites More sharing options...
salathe Posted February 21, 2011 Share Posted February 21, 2011 By the way, my snippet could be simplified since you only want a string (I usually want to play around with the DOMDocumentFragment some more). $myString = ''; if ($cars->length > 0) { foreach ($cars->item(0)->childNodes as $child) { $myString .= $newdoc->saveXML($child); } } Quote Link to comment https://forums.phpfreaks.com/topic/227811-converting-part-of-dom-xml-document-to-a-string/#findComment-1177577 Share on other sites More sharing options...
UltimateWeapon Posted February 21, 2011 Author Share Posted February 21, 2011 I wrapped everything in a function and it all works brilliantly, thank you ! Marked as Solved Quote Link to comment https://forums.phpfreaks.com/topic/227811-converting-part-of-dom-xml-document-to-a-string/#findComment-1177798 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.