我有以下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变量中。
答案 0 :(得分:-1)
使用以下代码
$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'];