0

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.

  • 1
    read this: https://stackoverflow.com/questions/595946/parse-xml-with-namespace-using-simplexml – ZiTAL Oct 05 '18 at 13:13
  • Use `$xml = (array)simplexml_load_string($string);`, Put your XML into string, then use that to convert it to an array, use `echo '
    ; print_r($string); echo '
    ';` to display it, and then use it as an array.
    – Thomas J. Oct 05 '18 at 13:14
  • @CommunityIsridiculous When i try this, it skips everything inside the content tag. –  Oct 05 '18 at 13:18
  • @FlubberBeer then I would recomend readin what ZiTAL mentioned.. – Thomas J. Oct 05 '18 at 13:39
  • 1
    Possible duplicate of [Parse XML with Namespace using SimpleXML](https://stackoverflow.com/questions/595946/parse-xml-with-namespace-using-simplexml) – Parfait Oct 05 '18 at 14:08
  • @Parfait I haven't privilege to do this – ZiTAL Oct 05 '18 at 14:13

1 Answers1

-1

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'];
ABHI
  • 122
  • 1
  • 9
  • 1
    Are you recommending to treat the XML as a string and strip away the namespace prefixes? SimpleXML and DOMDocument are capable DOM handlers and can handle namespaces. – Parfait Oct 05 '18 at 14:07
  • @Parfait You are right. We should not treat xml as a string. It was just a workaround. I am editing my post. – ABHI Oct 05 '18 at 18:19