[英]Trouble Parsing XML using Ruby XML Parser
我在使用以下命令解析返回的XML時遇到麻煩: XML::Parser.string(xml_string).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>
我收到此錯誤:
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
這里發生了什么?
您的字符串不是有效的XML文檔; 看來是兩個文件串聯在一起。 (第一個是“ <div>
”,第二個是“ <response>
”。)
嘗試將它們分成兩個字符串,然后分別解析每個字符串。
當您獲取xml_string
,我相信您需要設置用戶代理。 您沒有提供用戶代理,因此提供XML的服務器令人窒息。
使用以下代碼將用戶代理添加到您的請求中:
resp = http.post(path, query, {'User-Agent' => "Ruby"})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.