graham23s Posted December 3, 2009 Share Posted December 3, 2009 Hi Guys, In an xml file i am pulling from a site there is spaces between some of the nodes for example: category name has a space in the middle, does this matter at all, or is there a special way to deal with them? test code: <?php $object = simplexml_load_file("data.xml"); $xmlobj = new SimpleXMLElement($object); echo $xmlobj->root->category name; ?> thanks guys Graham Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 3, 2009 Share Posted December 3, 2009 Show us an example of the XML file. Quote Link to comment Share on other sites More sharing options...
graham23s Posted December 3, 2009 Author Share Posted December 3, 2009 Hi Mate, The most akward one is this one: <?xml version="1.0"?> <root> <bonus> <bonus bonus_id="5" qty="4" title="title1" variant="6"/><bonus bonus_id="6" qty="2" title="title2"/></bonus> <category name="Stop Smoking"><product bestseller_flag="0" brand="brandname" brand_flag="0" container="tablet" dosages="150mg" generic="genericTitle" name="productName"> <description>description goes here<description> i really just need the category name and the description values. cheers mate Graham Graham Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 3, 2009 Share Posted December 3, 2009 You need to do more reading on XML to understand what you're doing; w3schools has some fine tutorials. <category name="Stop Smoking"> The part in bold is an element. The element is named category. The part in italics is an attribute. The attribute's name is name and its value is Stop Smoking. You'll have to double check the SimpleXML documentation on how to access attributes, but I believe you do it as if they were array elements. The following may work: <?php $object = simplexml_load_file("data.xml"); //$xmlobj = new SimpleXMLElement($object); // <-- PRETTY SURE THIS IS REDUNDANT AND UNNECESSARY echo $object->root->category['name']; ?> Quote Link to comment Share on other sites More sharing options...
graham23s Posted December 3, 2009 Author Share Posted December 3, 2009 Hi Roopurt, Thanks a lot mate, i managed to get access to the data i wanted and i'm a little wiser on xml, the only other part is how would i go about accessing the data on all the products, i know i need a foreach like: <?php //$xmlFile = "data.xml"; //$xmlobj = new SimpleXmlElement(file_get_contents($xmlFile)); //print_r($xmlobj); $object = simplexml_load_file("data.xml"); foreach($object as $prodName) { //$xmlobj = new SimpleXMLElement($object); // <-- PRETTY SURE THIS IS REDUNDANT AND UNNECESSARY //echo $object->category['name']; echo "Product Name: " . $object->category->product['name']; echo "<br />"; //echo $object->category->product->description; //echo $prodName; } ?> any pointer son the foreach would be great, thanks for your help mate Graham Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 3, 2009 Share Posted December 3, 2009 I'll need to see more of the xml file as the one you've posted doesn't seem to be organized correctly. Quote Link to comment 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.