簡體   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