I have this XML code:
<?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>
My question is how can i get the data from d:Code and d:Description and store the data in a php variable.
Use below code
$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];
}
[Workaround] You can achieve this way also but this is not the correct way. Store Xml string in a variable and go through below code.
$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'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.