thomasgrant Posted January 7, 2008 Share Posted January 7, 2008 I've searched Google for a bit this morning for examples without finding anything that applies to what I'm trying to accomplish. Here is a sample of XML to get the idea out there. <?xml version="1.0" encoding="UTF-8"?> <newsletter> <year name="2007"> <content>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</content> </year> <year name="2008"> <content>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</content> </year> </newsletter> I've got the basics down of using SimpleXML in pulling XML data out and displaying on screen. But what I'm trying to do now is pull the content from a specific "year" based on the what I choose (ie. 2007, 2008, etc.) The examples I've seen thus far for using attributes() are typically while displaying everything in an array (ie. all years at once). Can anyone provide some insight into my request? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/84876-solved-help-with-simplexml-attributes/ Share on other sites More sharing options...
Daniel0 Posted January 7, 2008 Share Posted January 7, 2008 Try this: <?php $doc = new SimpleXMLElement('/path/to/file.xml', null, true); $node = $doc->xpath("year[@name='2007']"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/84876-solved-help-with-simplexml-attributes/#findComment-432716 Share on other sites More sharing options...
thomasgrant Posted January 7, 2008 Author Share Posted January 7, 2008 I appreciate the quick response Daniel0. Unfortunately, that did not yield a positive result. The output I get using your example is: "Array" Then when I print the array, $node = $doc->xpath("year[@name='2007']"); print_r ($node); I get: Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => 2007 ) [content] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ) ) Quote Link to comment https://forums.phpfreaks.com/topic/84876-solved-help-with-simplexml-attributes/#findComment-432730 Share on other sites More sharing options...
Daniel0 Posted January 7, 2008 Share Posted January 7, 2008 SimpleXMLElement::xpath() always returns an array. You'll have to get the first item in the array. You can do it like this: list($node) = $doc->xpath("year[@name='2007']"); or $node = $doc->xpath("year[@name='2007']"); $node = $node[0]; Then you'll have an instance of SimpleXMLElement. Then you can do echo $node->content . If you wish the content node directly then you can use the following XPath query instead: year[@name='2007']/content Quote Link to comment https://forums.phpfreaks.com/topic/84876-solved-help-with-simplexml-attributes/#findComment-432773 Share on other sites More sharing options...
thomasgrant Posted January 7, 2008 Author Share Posted January 7, 2008 Thanks for the follow up Daniel0. Fantastical! Quote Link to comment https://forums.phpfreaks.com/topic/84876-solved-help-with-simplexml-attributes/#findComment-432829 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.