0

When I try to convert XML(simpleXML) to JSON with json_encode, It works for XML without namesapce. For Example:

<ebpacket> 
   <head> 
      <packettype> UserAuthorization</packettype>
      <staffcode> UserName  </staffcode> 
      <pwd>  Password  </pwd> 
      <env>  Evnironment  </env> 
   </head> 
</ebpacket>

When I convert XML like below with attributes, json_encode returns an empty json:

<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/>
 <soapenv:Header />
 <soapenv:Body>
  <ser:processTrans>
     <xmlValue>
            <ebpacket> 
                <head> 
                    <packettype> UserAuthorization</packettype>
                    <staffcode> UserName  </staffcode> 
                    <pwd> Password  </pwd> 
                    <env>  Evnironment  </env> 
                </head> 
            </ebpacket>
    </xmlValue>
  </ser:processTrans>

The code block I am using is:

        $xml_str = str_replace(PHP_EOL, '', $xmlstr);
        $xml = simplexml_load_string($xml_str,'SimpleXMLElement',LIBXML_NOCDATA);
        $json_object = json_encode($xml, JSON_PRETTY_PRINT);
5
  • try with TRUE instead of JSON_PRETTY_PRINT like here: secure.php.net/manual/en/book.simplexml.php#105330 . But you also say that this is a SimpleXMLElement that will have to be processed based on the namespace and that's why you have an empty string. Commented Jan 16, 2018 at 11:47
  • Back up a step: why are you trying to convert XML into JSON? Do you actually have some code that can generically handle JSON, but can't handle XML in any way, and which needs access to the entire XML document, and don't know the structure in advance? Or, do you need some information from the XML, which SimpleXML will help you extract, and to later convert that information into JSON, or just into PHP arrays and objects? Commented Jan 16, 2018 at 12:13
  • But as Edwin says, the problem you have is not attributes, it's namespaces. See stackoverflow.com/questions/44894426/… for some background on what those are and how they work with SimpleXML. Commented Jan 16, 2018 at 12:14
  • @IMSoP, I have a generic function that converts XML to JSON and does not know the structure of XML in advance. Commented Jan 16, 2018 at 12:51
  • @dilani Then I think your function will need to invent its own conventions for how that translation works, as the support in SimpleXML for that is very basic; or you could look for a well-tested third-party library for this task. Personally, I think it's extremely rare that such a translation is useful; the types of structure the two formats can represent are too different to design a user-friendly mapping that works for all cases, and most systems that can handle one can handle the other anyway. (I use "translation" advisedly - think of it like relying on Google Translate to localise your UI) Commented Jan 16, 2018 at 15:09

1 Answer 1

1

After reading through, I figured out that you have to register your namespace to access the nodes with namespace prefix. For example:

$xml= SimpleXML_Load_String($xml_str,'SimpleXMLElement', LIBXML_NOCDATA, "http://schemas.xmlsoap.org/soap/envelope/");

This will return an XML object only with Head and Body. It will return any nodes with other prefixes. In the above example, it will not return nodes under the prefix 'ser'. Returned XML would be;

<Header></Header>
<Body></Body>

To be able to access other nodes, you have to use register the namespace and query it.

$xml->registerXPathNamespace('ser', 'http://w3c.soap.envelope.org/');
$result = $xml->xpath('//ser:*');

$result would be an array with all attributes under node 'ser:processTrans'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.