0

I am having trouble parsing some returned XML using this command: XML::Parser.string(xml_string).parse

Here is the XML I am trying to parse:

<div style=\"border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;\">
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message:  Undefined index:  HTTP_USER_AGENT</p>
<p>Filename: test</p>
<p>Line Number: test</p>
</div><?xml version=\"1.0\" encoding=\"UTF-8\"?>
<response>
    <review>
        <reviewer><![CDATA[test]]></reviewer>
        <ip><![CDATA[test]]></ip>
        rating><![CDATA[test]]></rating>
        <content><![CDATA[test.]]></content>
        <date><![CDATA[test]]></date>
    </review>
</response>

I get this error:

Fatal error: XML declaration allowed only at the start of the document at :10.Fatal error: Extra content at the end of the document at :11.
LibXML::XML::Error: Fatal error: Extra content at the end of the document

What is going on here?

1
  • The PHP response told you exactly what the problem was. Commented Dec 2, 2011 at 21:52

2 Answers 2

2

Your string is not a valid XML document; it appears to be two documents concatenated together. (The first one is a "<div>" the second one is a "<response>".)

Try separating them into two strings and parsing each of them separately.

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

2 Comments

That is a hacky solution and doesn't address the root cause of the problem.
@alecgorge: it's not meant to be a solution to his problem, just an answer to his question - "why doesn't this string parse as XML?"
1

When you are fetching xml_string, I believe you need to set the user agent. You are not providing a user agent so the server serving the XML is choking.

Use this code to add a user agent to your request:

resp = http.post(path, query, {'User-Agent' => "Ruby"})

1 Comment

This is how I make the post. resp = http.post(path, query) review_response = resp.body

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.