我正在尝试在 PHP 中读取带有命名空间的 xml 元素,但我遇到了一些问题,可能是因为存在多个子节点和命名空间。事实上,我对没有命名空间的 xml 文件没有任何问题。
我想找到一种优雅的方式来循环节点内的信息,例如,我想读取元素"Expense"中的所有数据(日期、商店、标识项、价格等(,并可能将它们插入数组中。这是 xml:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<Response xmlns="http://www.w3.org/2001/XMLSchema-instance">
<Result xmlns:a="http://www.w3.org/2001/XMLSchema-instance" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Success xmlns="http://www.w3.org/2001/XMLSchema-instance">OK</Success>
<a:Name>John</a:Name>
<a:Surname>Doe</a:Surname>
<a:Expenses>
<a:Expense>
<a:Date>2019-01-01</a:Date>
<a:Store>MALL1</a:Store>
<a:Items>
<a:Item>
<a:IdItem>A1</a:IdItem>
<a:Price>5</a:Price>
</a:Item>
</a:Items>
</a:Expense>
<a:Expense>
<a:Date>2019-01-02</a:Date>
<a:Store>MALL2</a:Store>
<a:Items>
<a:Item>
<a:IdItem>A2</a:IdItem>
<a:Price>1</a:Price>
</a:Item>
<a:Item>
<a:IdItem>A3</a:IdItem>
<a:Price>3</a:Price>
</a:Item>
</a:Items>
</a:Expense>
</a:Expenses>
</Result>
</Response>
我尝试了这样的事情:
$info= new SimpleXMLElement($xml);
$info->registerXPathNamespace('s', 'http://example.com');
$info->registerXPathNamespace('a', 'http://www.w3.org/2001/XMLSchema-instance');
//Example 1:
foreach($info->xpath('//a:Name') as $header)
{
echo (string) $header;
}
//Result is: John
//Example 2:
foreach($info->xpath('//a:Expenses') as $header)
{
$array = $header->xpath('//a:Expense/a:Store');
print_r($array);
}
//Result is:
/*Array
(
[0] => SimpleXMLElement Object
(
[0] => MALL1
)
[1] => SimpleXMLElement Object
(
[0] => MALL2
)
[2] => SimpleXMLElement Object
(
[0] => MALL2
)
)*/
//Example 3:
$array=$info ->xpath('/*/s:Body');
echo (string) $array[0]->Response->Result->Success;
//Result is: OK
但是,当然,这不是最好的方法,因为我一次只能得到一个元素,并且无法进行适当的循环。您将如何以更优雅和正确的方式阅读此 xml 的各种元素?谢谢。
这是 SOAP,而不仅仅是 XML。最好的选择是使用 SOAP 库。
但是,您面临的问题是,该行foreach($info->xpath('//a:Expenses') as $header)
将不同的SimpleXMLElement
实例分配给$header
变量,这些变量不知道您对$info
变量所做的注册。您需要在每个SimpleXMLElement
上重复注册。
$info= new SimpleXMLElement($xml);
$info->registerXPathNamespace('s', 'http://example.com');
$info->registerXPathNamespace('a', 'http://www.w3.org/2001/XMLSchema-instance');
foreach($info->xpath('//a:Expenses') as $header)
{
$header->registerXPathNamespace('a', 'http://www.w3.org/2001/XMLSchema-instance');
$array = $header->xpath('//a:Expense/a:Store');
print_r($array);
}
或者你开始使用 DOM。在 DOM 中,您可以创建一个单独的对象来计算 Xpath 表达式。因此,您只需要进行一次注册。此外,还可以使用返回标量值的 Xpath 表达式。
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
$xpath->registerXPathNamespace('s', 'http://example.com');
$xpath->registerXPathNamespace('a', 'http://www.w3.org/2001/XMLSchema-instance');
foreach($xpath->evaluate('//a:Expenses/a:Expense') as $expense)
{
print_r($xpath->evaluate('string(a:Store)', $expense));
}
仅使用 SimpleXML 的代码的简化版本。 基本思想是从<Result>
(我也使用 [0]
来指示使用找到的第一个元素(元素开始,该元素包含您之后的所有数据。 然后,不要继续使用 XPath,而是获取新定义的 a
命名空间中的所有子元素(使用 children("a", true)
(。然后,这将允许您在没有任何前缀的情况下访问它们,因此使用标准的 SimpleXML 表示法->
...
$info= new SimpleXMLElement($xml);
$info->registerXPathNamespace('a', 'http://www.w3.org/2001/XMLSchema-instance');
$result = $info->xpath("//a:Result")[0]->children("a", true);
//Example 1:
echo $result->Name.PHP_EOL;
//Result is: John
//Example 2:
foreach($result->Expenses->Expense as $header)
{
echo (string)$header->Store.PHP_EOL;
}
哪些输出...
John
MALL1
MALL2