我需要阅读以下XML数据:
<?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>almindas@domain.com</email>
</person>
<person uri="https://clientname.blinksale.com/clients/3347419/people/17421">
<first_name>Liza</first_name>
<last_name>Buchler</last_name>
<email>lizab@domain.com</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>
我可以使用以下代码读取数据,但不会遍历人员和发票数据。
<?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>";
?>
这是我得到的答案:
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: almindas@domain.com
**Missing data here**
URI: https://clientname.blinksale.com/invoices/1223295
**Missing data here**
End
我认为一旦得到答案,人们就可以阅读各种格式的XML数据。
答案 0 :(得分:2)
您正在遍历people
元素,因为只有一个people
元素,您也只能看到一个结果。您获得person
数据的原因是因为$clientinfo->person
您访问了person
元素中的第一个people
元素。
尝试使用foreach($myclient->people->person as $clientinfo)
循环遍历person
元素,然后您可以使用$clientinfo->first_name
来访问您的个人数据。发票也是如此,使用foreach($myclient->invoices->invoice as $invoiceinfo)
然后$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>";
?>
答案 1 :(得分:2)
$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';
}