通过标签名获取 xml 个元素
Getting xml elements by tag name
我在这里看到了几乎所有关于此问题的问题,但 none 是相关的。
$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);
Returns:
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>"
}
有人可以指出可能导致问题的原因吗?
这对我有用:
<?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);
}
你可以这样处理
$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']);
}
我在这里看到了几乎所有关于此问题的问题,但 none 是相关的。
$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);
Returns:
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>"
}
有人可以指出可能导致问题的原因吗?
这对我有用:
<?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);
}
你可以这样处理
$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']);
}