Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP5 and XML

Featured Replies

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! :clapping: ,

Chris

  • 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

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 by ElanMan

  • Author

Thats great, thanks guys :)

 

Ill give it a whirl!

 

Thanks,

Chris

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.