2

I'm having a difficult time parsing the following XML string. I receive numerous errors when using SimpleXMLElement or simplexml_load_string. I'm running PHP version 5.5.20.

<?xmlversion="1.0"encoding="utf-8"?>
<Responsetype="NAK">
<ResponseCode>231</ResponseCode>
<Description>Billingstate/provinceisrequired.</Description>
<Reference>VSTMUAS:060215CJM-12</Reference>
<TransactionID>1433251975406510979</TransactionID>
<ProcessingTime>0.590634</ProcessingTime>
</Response>

When I run the following code:

$xml = simplexml_load_string($myXMLData);
print_r($xml);

I get the following errors:

Warning: simplexml_load_string(): Entity: line 1: parser warning : xmlParsePITarget: invalid name prefix 'xml' in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): <?xmlversion="1.0"encoding="utf-8"?> in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): ^ in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): Entity: line 1: parser error : ParsePI: PI xmlversion space expected in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): <?xmlversion="1.0"encoding="utf-8"?> in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): ^ in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): Entity: line 2: parser error : error parsing attribute name in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): <Responsetype="NAK"> in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): ^ in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): Entity: line 2: parser error : attributes construct error in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): <Responsetype="NAK"> in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): ^ in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): Entity: line 2: parser error : Couldn't find end of Start Tag Responsetype line 2 in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): <Responsetype="NAK"> in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): ^ in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): Entity: line 2: parser error : Extra content at the end of the document in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): <Responsetype="NAK"> in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Warning: simplexml_load_string(): ^ in /home/pmotrad/public_html/content/CalvinsTesting.php on line 18

Please help me undertand what I am doing wrong.

Thanks,

Calvin

3
  • Well, it doesn't appear well-formed. Where's the opening <Response> tag? Commented Jun 2, 2015 at 14:55
  • I agree, it is not well formed. This is a response from an outside source, and unfortunately I cannot control what they send back to me. I can however use some php string manipulation in order to get it in the correct form. I am what some would call an XML newb, so if someone could show me how the form should be I can make those changes in my code. On a side note, i did add the <Response> tag before my post and I did not notice any changes to the errors shown above, so I did not think that was the problem. Commented Jun 2, 2015 at 15:02
  • You're not doing anything wrong and this is how you create SimpleXMLElement object from a string. However the string is not good to get converted to an XML object, you should clean it and to do so read carefully the errors returned, they are quite straightforward Commented Jun 2, 2015 at 15:07

1 Answer 1

2

The string contains not well formatted XML.

Here is your XML after fixing it

<?xml version="1.0" encoding="utf-8"?>
<Response type="NAK">
    <ResponseCode>231</ResponseCode>
    <Description>Billingstate/provinceisrequired.</Description>
    <Reference>VSTMUAS:060215CJM-12</Reference>
    <TransactionID>1433251975406510979</TransactionID>
    <ProcessingTime>0.590634</ProcessingTime>
</Response>

Try with this xml and you'll get:

 SimpleXMLElement Object ( [@attributes] => Array ( [type] => NAK ) [ResponseCode] => 231 [Description] => Billingstate/provinceisrequired. [Reference] => VSTMUAS:060215CJM-12 [TransactionID] => 1433251975406510979 [ProcessingTime] => 0.590634 )
Sign up to request clarification or add additional context in comments.

4 Comments

This is great and it works, Thanks. I would upvote if I had the reputation.
I have a follow up question for you. Can you tell me how to access the <Response>'s attribute value ( I need to get the "NAK" )?
Figured it out, to get the attribute: $xml['type'].

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.