繁体   English   中英

使用SimpleXML使用Namspace解析XML

[英]Parse XML with Namspace using SimpleXML

我有以下XML代码:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="https://test/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Items</title>
<id>https://test</id>
<updated>2018-10-05T11:26:16Z</updated>
<link rel="self" title="Items" href="Items" />
<entry>
    <id>https://test')</id>
    <title type="text"></title>
    <updated>2018-10-05T11:26:16Z</updated>
    <author>
        <name />
    </author>
    <link rel="edit" title="Item" href="Items(guid'076c856c-aa45-403a-82f7-004fe0de8c27')" />
    <category term="Exact.Web.Api.Models.Item" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
        <m:properties>
            <d:Code>123566546</d:Code>
            <d:Description>32132131</d:Description>
        </m:properties>
    </content>
</entry>
<link rel="next" 
 href="https://test'" />
</feed>

我的问题是如何从d:Code和d:Description获取数据并将数据存储在php变量中。

使用以下代码

$xmlString = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="https://test/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Items</title>
<id>https://test</id>
<updated>2018-10-05T11:26:16Z</updated>
<link rel="self" title="Items" href="Items" />
<entry>
    <id>https://test</id>
    <title type="text"></title>
    <updated>2018-10-05T11:26:16Z</updated>
    <author>
        <name />
    </author>
    <link rel="edit" title="Item" href="Items(guid 076c856c-aa45-403a-82f7-004fe0de8c27)" />
    <category term="Exact.Web.Api.Models.Item" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
        <m:properties>
            <d:Code>123566546</d:Code>
            <d:Description>32132131</d:Description>
        </m:properties>
    </content>
</entry>
<link rel="next" 
 href="https://test" />
</feed>';

$xml = new SimpleXMLElement($xmlString);

foreach($xml->xpath('//m:properties') as $event) {
  echo $event->xpath('d:Code')[0];
  echo $event->xpath('d:Description')[0];
}

[解决方法]您也可以实现这种方法,但这不是正确的方法。 将Xml字符串存储在变量中,并通过下面的代码。

$inputXmlXtring = '';

$inputXmlXtring =   str_replace('m:','',$inputXmlXtring);
$inputXmlXtring =   str_replace('d:','',$inputXmlXtring);

$xmlParsed  =   simplexml_load_string($inputXmlXtring,"SimpleXMLElement");
$jsonXml    =   json_encode($xmlParsed);
$xmlArray   =   json_decode($jsonXml,TRUE);

// For Code and desription you can access through this.
$codeValue = $xmlArray['entry']['content']['properties']['Code'];
$descriptionValue = $xmlArray['entry']['content']['properties']['Description'];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM