inogen Posted December 7, 2010 Share Posted December 7, 2010 Hi all, I'm sure I'm making this far more difficult than I need to... I've got an xml document... <library id="1"> <lname>Lib1</name> <book id="1"> <title>book 1</title> <author>Author 1</author> </book> <book id="2"> <title>book 2</title> <author>Author 2</author> </book> </library> I can access the nested elements using xpath like : $result = $xml->xpath('//book'); foreach($result as $books) { echo $books->title; } How would I do this for the attributes? If I specify I want the attributes in the xpath query I can't get at the elements... I'm trying to turn this xml file into a simple relational database... A library table and a book table. So I need the library id (attribute) and the name(nested element) for one insert query - and the book id, title, author and the library id for the other. Anyone got any idea? Cheers John Quote Link to comment https://forums.phpfreaks.com/topic/220910-xpath-elements-and-attributes/ Share on other sites More sharing options...
salathe Posted December 7, 2010 Share Posted December 7, 2010 You've marked this as solved... is it? Quote Link to comment https://forums.phpfreaks.com/topic/220910-xpath-elements-and-attributes/#findComment-1143937 Share on other sites More sharing options...
inogen Posted December 7, 2010 Author Share Posted December 7, 2010 Yes - I solved it thanks. I tried to post but didn't work. you can access the attributes like echo library->book{id}; ta John Quote Link to comment https://forums.phpfreaks.com/topic/220910-xpath-elements-and-attributes/#findComment-1143939 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.