Reputation: 2376
I need to read the following XML data:
<?xml version="1.0" encoding="UTF-8"?>
<client xmlns="http://www.blinksale.com/api" uri="https://clientname.blinksale.com/clients/17419" created_at="2014-03-15T17:00:20Z" updated_at="2014-10-06T13:56:54Z">
<name>Widgets Supply</name>
<address1>Protea House</address1>
<address2>50 Gooseberry Street</address2>
<city>Honeydew</city>
<state>GT</state>
<zip>2040</zip>
<country>ZA</country>
<url>http://www.domain.com</url>
<phone></phone>
<fax></fax>
<token>512de253e5b</token>
<people uri="https://clientname.blinksale.com/clients/17419/people">
<person uri="https://clientname.blinksale.com/clients/17419/people/17420">
<first_name>Alminda</first_name>
<last_name>Smith</last_name>
<email>[email protected]</email>
</person>
<person uri="https://clientname.blinksale.com/clients/3347419/people/17421">
<first_name>Liza</first_name>
<last_name>Buchler</last_name>
<email>[email protected]</email>
</person>
</people>
<invoices uri="">
<invoice uri="https://clientname.blinksale.com/invoices/1223295"/>
<invoice uri="https://clientname.blinksale.com/invoices/1228974"/>
<invoice uri="https://clientname.blinksale.com/invoices/1230066"/>
<invoice uri="https://clientname.blinksale.com/invoices/1241476"/>
<invoice uri="https://clientname.blinksale.com/invoices/1252582"/>
<invoice uri="https://clientname.blinksale.com/invoices/1263211"/>
<invoice uri="https://clientname.blinksale.com/invoices/1273335"/>
<invoice uri="https://clientname.blinksale.com/invoices/1282340"/>
<invoice uri="https://clientname.blinksale.com/invoices/1284381"/>
<invoice uri="https://clientname.blinksale.com/invoices/1287383"/>
<invoice uri="https://clientname.blinksale.com/invoices/1295650"/>
<invoice uri="https://clientname.blinksale.com/invoices/1305739"/>
<invoice uri="https://clientname.blinksale.com/invoices/1306958"/>
<invoice uri="https://clientname.blinksale.com/invoices/1315324"/>
</invoices>
</client>
I can read the data with the following code, but it will not loop through the person and invoice data.
<?php
$myclient=simplexml_load_file("client.xml") or die("Error: Cannot open client.xml");
echo "Name: " . $myclient->name[0] . "<br>";
echo "Address 1: " . $myclient->address1[0] . "<br>";
echo "Address 2: " . $myclient->address2[0] . "<br>";
echo "City: " . $myclient->city[0] . "<br>";
echo "State: " . $myclient->state[0] . "<br>";
echo "Postal Code: " . $myclient->zip[0] . "<br>";
echo "Country: " . $myclient->country[0] . "<br>";
echo "URL: " . $myclient->url[0] . "<br>";
echo "Phone: " . $myclient->phone[0] . "<br>";
echo "Fax: " . $myclient->fax[0] . "<br>";
echo "<br>";
echo "Token: " . $myclient->token[0] . "<br>";
echo "<br>";
foreach($myclient->people as $clientinfo) {
$uri2 = $clientinfo->person['uri'];
echo "URI: $uri2 <br>";
$first_name = $clientinfo->person->first_name;
$last_name = $clientinfo->person->last_name;
$email = $clientinfo->person->email;
echo "First Name: $first_name <br>";
echo "Surname: $last_name <br>";
echo "Email: $email <br>";
}
echo "<br>";
foreach($myclient->invoices as $invoiceinfo) {
$uri3 = $invoiceinfo->invoice['uri'];
echo "URI: $uri3 <br>";
}
echo "End<br>";
?>
This is the answer I get:
Name: Widgets Supply
Address 1: Protea House
Address 2: 50 Gooseberry Street
City: Honeydew
State: GT
Postal Code: 2040
Country: ZA
URL: http://www.domain.com
Phone:
Fax:
Token: 512de253e5b
URI: https://clientname.blinksale.com/clients/17419/people/17420
First Name: Alminda
Surname: Smith
Email: [email protected]
**Missing data here**
URI: https://clientname.blinksale.com/invoices/1223295
**Missing data here**
End
I think once we have an answer it will be a useful answer for people to read XML data in various formats.
Upvotes: 0
Views: 71
Reputation: 108
You are looping through the people
elements, and because there's only one people
element you also see only one result. The reason you do get person
data is because with $clientinfo->person
you access the first person
element inside the people
element.
Try foreach($myclient->people->person as $clientinfo)
to loop through the person
elements instead and then you can use $clientinfo->first_name
to access your person data. The same goes for invoices, use foreach($myclient->invoices->invoice as $invoiceinfo)
and then $invoiceinfo['uri']
.
<?php
$myclient=simplexml_load_file("client.xml") or die("Error: Cannot open client.xml");
echo "Name: " . $myclient->name[0] . "<br>";
echo "Address 1: " . $myclient->address1[0] . "<br>";
echo "Address 2: " . $myclient->address2[0] . "<br>";
echo "City: " . $myclient->city[0] . "<br>";
echo "State: " . $myclient->state[0] . "<br>";
echo "Postal Code: " . $myclient->zip[0] . "<br>";
echo "Country: " . $myclient->country[0] . "<br>";
echo "URL: " . $myclient->url[0] . "<br>";
echo "Phone: " . $myclient->phone[0] . "<br>";
echo "Fax: " . $myclient->fax[0] . "<br>";
echo "<br>";
echo "Token: " . $myclient->token[0] . "<br>";
echo "<br>";
foreach($myclient->people->person as $clientinfo) {
$uri2 = $clientinfo['uri'];
echo "URI: $uri2 <br>";
$first_name = $clientinfo->first_name;
$last_name = $clientinfo->last_name;
$email = $clientinfo->email;
echo "First Name: $first_name <br>";
echo "Surname: $last_name <br>";
echo "Email: $email <br>";
}
echo "<br>";
foreach($myclient->invoices->invoice as $invoiceinfo) {
$uri3 = $invoiceinfo['uri'];
echo "URI: $uri3 <br>";
}
echo "End<br>";
?>
Upvotes: 2
Reputation: 17289
$clients = $myclient->people->person;
foreach($clients as $client) {
echo $client->first_name.'\n';
}
$invoices = $myclient->invoices->invoice;
foreach($invoices as $invoice) {
echo $invoice['uri'].'\n';
}
Upvotes: 2