watsmyname Posted September 6, 2010 Share Posted September 6, 2010 Hello I have xml like this. <?xml version="1.0" encoding="utf-8"?> <data> <item ID="30001"> <Company>Navarro Corp.</Company> </item> <item ID="30002"> <Company>Performant Systems</Company> </item> <item ID="30003"> <Company>Digital Showcase</Company> </item> </data> All i need to do is get any one of the whole node from starting item tag to ending item tag from the xml, for example. getting <item ID="30002"> <Company>Performant Systems</Company> </item> from whole xml file. Can this be achieved using SimpleXML object from PHP? if not how it can be done. Thanks in advance watsmyname Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/ Share on other sites More sharing options...
Adam Posted September 6, 2010 Share Posted September 6, 2010 Do you want to return the XML as a SimpleXMLElement object, or just as a string? Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1107864 Share on other sites More sharing options...
Adam Posted September 6, 2010 Share Posted September 6, 2010 $str = '<?xml version="1.0" encoding="utf-8"?> <data> <item ID="30001"> <Company>Navarro Corp.</Company> </item> <item ID="30002"> <Company>Performant Systems</Company> </item> <item ID="30003"> <Company>Digital Showcase</Company> </item> </data>'; $xml = new SimpleXMLElement($str); foreach ($xml->children() as $node) { // right now $node contains the SimpleXMLElement // but you can return it as a string with ->asXML() echo $node->asXML(); } Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1107865 Share on other sites More sharing options...
watsmyname Posted September 6, 2010 Author Share Posted September 6, 2010 $str = '<?xml version="1.0" encoding="utf-8"?> <data> <item ID="30001"> <Company>Navarro Corp.</Company> </item> <item ID="30002"> <Company>Performant Systems</Company> </item> <item ID="30003"> <Company>Digital Showcase</Company> </item> </data>'; $xml = new SimpleXMLElement($str); foreach ($xml->children() as $node) { // right now $node contains the SimpleXMLElement // but you can return it as a string with ->asXML() echo $node->asXML(); } well thanks for the reply but your code returns " Navarro Corp. Performant Systems Digital Showcase " i want whole <item ID="30001"> <Company>Navarro Corp.</Company> </item> out of the given xml string, i.e. including Item start tag to item end tag Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1107907 Share on other sites More sharing options...
Adam Posted September 6, 2010 Share Posted September 6, 2010 That's because the browser's trying to render it as HTML, view the source. Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1107909 Share on other sites More sharing options...
watsmyname Posted September 6, 2010 Author Share Posted September 6, 2010 That's because the browser's trying to render it as HTML, view the source. Thanks, you're right! I used htmlentities to get tag as well echo htmlentities($node->asXML()); Thanks a ton Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1107914 Share on other sites More sharing options...
watsmyname Posted September 7, 2010 Author Share Posted September 7, 2010 Well, I have having a problem now. If a given xml has node <Company>Navarro Corp.</Company> There is no problem asXML returns as it is. But if it has <company></company> It returns <company/> only i want it to return <company></company> Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1108160 Share on other sites More sharing options...
Adam Posted September 7, 2010 Share Posted September 7, 2010 I'm not sure to be honest. ->asXML() doesn't have any formatting options so I'm not sure you'll be able to. They do mean the same thing though, why do you need it changing? Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1108173 Share on other sites More sharing options...
watsmyname Posted September 7, 2010 Author Share Posted September 7, 2010 I'm not sure to be honest. ->asXML() doesn't have any formatting options so I'm not sure you'll be able to. They do mean the same thing though, why do you need it changing? well the matter of fact is that a code reads an xml file, and each node is listed in each row of the table through loop. and each time it puts a equivalent node in hidden text area. So first time in text area it puts <item ID="30001"> <Company>Navarro Corp.</Company> </item> Second time it loops, in another text area it puts <item ID="30002"> <Company>Performant Systems</Company> </item> and so on. Actually this is done to delete a particular node. There is delete button in the last column of each row. When it is clicked, The value of corresponding text area is posted to next page. In next page again whole previous xml file is loaded as string. We match the value of text area to this string and replace it with blank space. and resulting string is saved as xml again. Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1108175 Share on other sites More sharing options...
Adam Posted September 7, 2010 Share Posted September 7, 2010 Why don't you just pass the item ID and remove the node using SimpleXML? Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1108176 Share on other sites More sharing options...
watsmyname Posted September 7, 2010 Author Share Posted September 7, 2010 Why don't you just pass the item ID and remove the node using SimpleXML? The given xml is just an example. There might be many tags with many attributes, and the amount of attributes on each node will be different and so are their values. Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1108177 Share on other sites More sharing options...
watsmyname Posted September 7, 2010 Author Share Posted September 7, 2010 Why don't you just pass the item ID and remove the node using SimpleXML? The given xml is just an example. There might be many tags with many attributes, and the amount of attributes on each node will be different and so are their values. OKAY I SOLVED IT. BUT IN A BIT LONG way. Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1108195 Share on other sites More sharing options...
Adam Posted September 7, 2010 Share Posted September 7, 2010 I'll be honest, it sounds like there should be a better way to do it. Perhaps you'd need to use the DOM for more advanced functionality? OKAY I SOLVED IT. BUT IN A BIT LONG way. How did you solve it? Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1108208 Share on other sites More sharing options...
watsmyname Posted September 7, 2010 Author Share Posted September 7, 2010 I'll be honest, it sounds like there should be a better way to do it. Perhaps you'd need to use the DOM for more advanced functionality? OKAY I SOLVED IT. BUT IN A BIT LONG way. How did you solve it? I used fread function to read an xml file. I put content in a string. I then exploded the string with </company>. I get each opening company tags with attributes in an array, i looped it and put each value [then concated </company> in array element] in the text area. And the text area is posted to another page. Rest i have already told. Quote Link to comment https://forums.phpfreaks.com/topic/212656-getting-a-whole-xml-node/#findComment-1108212 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.