January 4, 201016 yr Hi there, I am wondering if someone could give me a pointer.. Im using the simplexml_load_file funciton to load an xml file into an array, to get the contents of each item in the xml document im using a For Loop but with some of the items in the XML file it has the following: <categories> <range category="Category1">Subcategory 1</range> </categories> How can I retrieve both the Category1 and Subcategory while doing my loop? I can only seem to get the Subcategory1 within the "Range" tag.. Ive had a look on google but im not sure what to search for to get my answer? Help! Happy New Year! , Chris
January 4, 201016 yr Author Here is a sample schema of the XML file The <Product> Tag is a repeating tag.. <allproducts> <timestamp>04/01/2010 00:30:49</timestamp> <version>2.0</version> <products> <product type="main"> <code>Code101</code> <name>Product Name</name> <description>Description</description> <measurments> <imperial>na</imperial> <metric>na</metric> </measurments> <power> <powertype>None Required</powertype> </power> <price> <wholesale>3.34</wholesale> <rrp>10.49</rrp> </price> <categories> <range category="product category1">Sub-Category 1</range> <range category="product category1">Sub-Category 2</range> <range category="product category1">Sub-Category 3</range> </categories> </product> <product type="main"> <code>Code102</code> <name>Product Name</name> <description>Description</description> <measurments> <imperial>na</imperial> <metric>na</metric> </measurments> <power> <powertype>None Required</powertype> </power> <price> <wholesale>3.34</wholesale> <rrp>10.49</rrp> </price> <categories> <range category="product category1">Sub-Category 1</range> <range category="product category1">Sub-Category 2</range> <range category="product category1">Sub-Category 3</range> </categories> </product> </products> </allproducts> Thanks, Chris
January 4, 201016 yr As a really quick example, you could do it like this: <?php $source = 'your.xml'; $products = new SimpleXmlElement($source,null,true); foreach($products->children() as $product) { $output = ''; $productCount = 1; foreach($product->children() as $a) { $output .= '<h2>Product'.$productCount.'</h2>'; $rangeCount = 1; foreach ($a->categories->range as $attr) { $splitAttr = explode(' ',$attr->attributes()); $output .= '<h4>Range ' . $rangeCount.'</h4>'; $output .= '<ul>'; $output .= '<li>Attribute 1: '.$splitAttr[0].'</li>'; $output .= '<li>Attribute 2: '.$splitAttr[1].'</li>'; $output .= '<li>Node Content: '.$attr.'</li>'; $output .= '</ul>'; $rangeCount++; } $productCount++; } } echo $output; ?> In case you have multiple attributes, I've used explode(). Hopefully that makes some sort of sense. The editor has mangled the formatting of the code Edit: This should then give you something like the following Product1 Range 1 Attribute 1: product Attribute 2: category1 Node Content: Sub-Category 1 Range 2 Attribute 1: product Attribute 2: category1 Node Content: Sub-Category 2 Range 3 Attribute 1: product Attribute 2: category1 Node Content: Sub-Category 3 Product2 Range 1 Attribute 1: product Attribute 2: category2 Node Content: Sub-Category 1 Range 2 Attribute 1: product Attribute 2: category2 Node Content: Sub-Category 2 Range 3 Attribute 1: product Attribute 2: category2 Node Content: Sub-Category 3 Edited January 4, 201016 yr by ElanMan
Create an account or sign in to comment