我在这里看到了几乎所有与此问题有关的问题,但没有相关的问题。
$employees = simplexml_load_string($response)->c->q[1]['v'];
$ employees = SimpleXML对象:
<?xml version="1.0" encoding="utf-8" ?>
<rows xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "Articles_Schema.xsd" >
<row id="1">
<EmployeeID>
<![CDATA[123]]>
</EmployeeID>
<EmplopyeeName>
<![CDATA[John Doe]]>
</EmplopyeeName>
</row>
<row id="2">
<EmployeeID>
<![CDATA[456]]>
</EmployeeID>
<EmplopyeeName>
<![CDATA[Jack Mill]]>
</EmplopyeeName>
</row>
</rows>
我想找回所有员工(如果找到)。
我尝试使用以下方式访问它们:
$employees->children()
[空数组] $employees->rows
[空数组] $employees->row
[空数组] $employees->xpath('//rows')
[空数组] $employees->xpath('//row')
[空数组] $employees->registerXPathNamespace('x', 'http://www.w3.org/2001/XMLSchema-instance'); $employees->xpath('//x:row')
[空数组] var_dump($employees->getName()); //returns "v"
var_dump($employees->getNamespaces()); //returns NULL
var_dump($employees);
返回:
object(SimpleXMLElement)#26 (1) {
[0]=>
string(xxx) "
<?xml version="1.0" encoding="utf-8" ?>
<rows xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "Articles_Schema.xsd" >
<row id="1">
<EmployeeID>
<![CDATA[123]]>
</EmployeeID>
<EmplopyeeName>
<![CDATA[John Doe]]>
</EmplopyeeName>
</row>
<row id="2">
<EmployeeID>
<![CDATA[456]]>
</EmployeeID>
<EmplopyeeName>
<![CDATA[Jack Mill]]>
</EmplopyeeName>
</row>
</rows>"
}
有人可以指出导致问题的原因吗?
答案 0 :(得分:1)
这对我有用:
<?php
$employees =<<< EMP
<?xml version="1.0" encoding="utf-8" ?>
<rows xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "Articles_Schema.xsd" >
<row id="1">
<EmployeeID>
<![CDATA[123]]>
</EmployeeID>
<EmplopyeeName>
<![CDATA[John Doe]]>
</EmplopyeeName>
</row>
<row id="2">
<EmployeeID>
<![CDATA[456]]>
</EmployeeID>
<EmplopyeeName>
<![CDATA[Jack Mill]]>
</EmplopyeeName>
</row>
</rows>
EMP;
$employees = simplexml_load_string($employees);
foreach($employees->row as $row)
{
print((string) $row->EmployeeID);
print((string) $row->EmplopyeeName);
}
要针对您的情况进行尝试:
$employeeXml = simplexml_load_string((string) $employees);
foreach($employeeXml->row as $row)
{
print((string) $row->EmployeeID);
print((string) $row->EmplopyeeName);
}
答案 1 :(得分:1)
您可以这样处理
$xml = <<< EMP
<?xml version="1.0" encoding="utf-8" ?>
<rows xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "Articles_Schema.xsd" >
<row id="1">
<EmployeeID>
<![CDATA[123]]>
</EmployeeID>
<EmplopyeeName>
<![CDATA[John Doe]]>
</EmplopyeeName>
</row>
<row id="2">
<EmployeeID>
<![CDATA[456]]>
</EmployeeID>
<EmplopyeeName>
<![CDATA[Jack Mill]]>
</EmplopyeeName>
</row>
</rows>
EMP;
$xmlToArray = json_decode(json_encode((array)simplexml_load_string($xml,null,LIBXML_NOCDATA)),true);
foreach($xmlToArray['row'] as $row){
echo trim($row['EmployeeID']).'---'.trim($row['EmplopyeeName']);
}