[英]Trouble Parsing XML using Ruby XML Parser
I am having trouble parsing some returned XML using this command: XML::Parser.string(xml_string).parse
我在使用以下命令解析返回的XML时遇到麻烦:
XML::Parser.string(xml_string).parse
Here is the XML I am trying to parse: 这是我要解析的XML:
<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? 这里发生了什么?
Your string is not a valid XML document; 您的字符串不是有效的XML文档; it appears to be two documents concatenated together.
看来是两个文件串联在一起。 (The first one is a "
<div>
" the second one is a " <response>
".) (第一个是“
<div>
”,第二个是“ <response>
”。)
Try separating them into two strings and parsing each of them separately. 尝试将它们分成两个字符串,然后分别解析每个字符串。
When you are fetching xml_string
, I believe you need to set the user agent. 当您获取
xml_string
,我相信您需要设置用户代理。 You are not providing a user agent so the server serving the XML is choking. 您没有提供用户代理,因此提供XML的服务器令人窒息。
Use this code to add a user agent to your request: 使用以下代码将用户代理添加到您的请求中:
resp = http.post(path, query, {'User-Agent' => "Ruby"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.