0

the xml i am recieving looks like:

<?xml version="1.0" encoding="utf-8"?>
<ChangePassPhraseRequestResponse 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="www.envmgr.com/LabelService">
    <Status>2012</Status>
    <ErrorMessage>Account Error encountered (Log ID: 11845)</ErrorMessage>
</ChangePassPhraseRequestResponse>

i am attempting to encode to json as such (where xml = above response)

$data =  simplexml_load_string($xml);
echo json_encode($data);

error i am getting is:

simplexml_load_string(): namespace warning : xmlns: URI www.envmgr.com/LabelService is not absolute 
SimpleXMLElement::__construct(): Entity: line 4: parser error : Start tag expected, '&lt;' not found

2 Answers 2

0

The problem is the xmlns URL (www.envmgr.com/LabelService) does not have scheme (http:// OR https://) specified. if possible add that to the xmlns url or try to suppress warnings using forllowing code:

$data =  simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOWARNING );
echo json_encode($data);

Check here : https://eval.in/532347

1
  • i went with this except using as such: new SimpleXMLElement($xml,LIBXML_NOWARNING);
    – mithereal
    Mar 8, 2016 at 6:29
0

You need to use preg_replace() for removing namespace.

$xml_string = preg_replace('/xmlns[^=]*="[^"]*"/i', '', $xml);
$data =  simplexml_load_string($xml_string);
echo json_encode($data);

Result:

{"Status":"2012","ErrorMessage":"Account Error encountered (Log ID: 11845)"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.