sprynmr Posted February 11, 2008 Share Posted February 11, 2008 Hey everybody! Having tons of fun with PHP, but I'm trying to figure something out with XML. I've decided to drive my online portfolio with XML instead of typing all the info into a super long switch statement. I have found plenty of documentation on how to iterate through a whole slew of elements using their index id, but very little on how to access a specific element via name. I have a beautiful XML document that has the basic structure of media->category->slide where slide contains a bunch of child nodes with information about that particular image/movie/website or whatever, along with an image url and such. The category and slide ID are coming from _GET. Basically I want to access a particular slide slide[$mediaID] which is under a particular category category[$categoryID] and display that information on the page. Unfortunately I can't figure out a way to access that particular category without using a control structure and conditional checking... example below: $xml=simplexml_load_file("media.xml"); while(!$catfound) { $i=0; if ((string)$xml->category[$i]->name==$category) { $catfound=$category; } $i++; } I did find a way to access it via the name using XPath, but it appears the object or array or whatever xpath returns doesn't maintain any of the attributes belonging to XML elements, just the elements themselves. I know there is a much easier way because this is something people must do every day. Second question: Once I do access the correct category and slide $xml->category[$catfound]->slide[$mediaID]; is there a way I can copy that particular slide to another array or reference it without having to provide that long path everytime? Basically I want to access elements in it by typing $slide->title; for the rest of my script. Sorry if these are slightly obvious questions. I'm trying to do things correctly instead of just doing them. Appreciate any help! Best, Robert Quote Link to comment https://forums.phpfreaks.com/topic/90466-help-parsing-xml-document-using-simplexml/ Share on other sites More sharing options...
sprynmr Posted February 12, 2008 Author Share Posted February 12, 2008 Little help? Quote Link to comment https://forums.phpfreaks.com/topic/90466-help-parsing-xml-document-using-simplexml/#findComment-464511 Share on other sites More sharing options...
sprynmr Posted February 12, 2008 Author Share Posted February 12, 2008 Problem solved. I was wrong when I said Xpath doesn't maintain attributes... they just don't show up in var_dump. Code below works and shows accessing an element and an attribute. $slide=$xml->xpath('/media/category[@name="'.$category.'"]/slide["'.$mediaID.'"]'); echo "<br/>".$slide[0]->title; echo "<br/>".$slide[0]->bigimage['file']; Hey how do I mark this thread solved? I don't see any link for that. Quote Link to comment https://forums.phpfreaks.com/topic/90466-help-parsing-xml-document-using-simplexml/#findComment-464624 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.