日期:2014-05-17  浏览次数:20987 次

ASP访问远程XML错误:文档必须有一个顶层元素
VBScript code
Set http=Server.CreateObject("MSXML2.ServerXMLHTTP")
      http.Open "post","http://www.yodao.com/smartresult-xml/search.s?type=ip&q=221.123.123.123",false
      
      http.send
      Set xml = Server.CreateObject("Microsoft.XMLDOM") 
      xml.Async = false 
      xml.ValidateOnParse = true
      xml.setProperty "ServerHTTPRequest",true
      xml.load(http.ResponseXML)
       if xml.ParseError.errorCode<>0 then   
          Response.Write("Error:   "   &   xml.parseError.reason   &   "<br>")   
          Response.Write("Code:   0x"   &   hex(xml.parseError.errorCode)   &   "<br>")   
          Response.Write("At   Line:   "   &   xml.parseError.line   &   "<br>")   
          Response.Write("At   pos:   "   &   xml.parseError.linePos   &   "<br>")   
                                    
     else   

      set objRootElement = xml.documentElement  
      Response.Write objRootElement.childnodes(0).text 
      end if


------解决方案--------------------
VBScript code

<%
     Set http=Server.CreateObject("MSXML2.ServerXMLHTTP")
     http.Open "GET","http://www.yodao.com/smartresult-xml/search.s?type=ip&q=221.123.123.123",false
     http.send
     Set xml = Server.CreateObject("Microsoft.XMLDOM") 
     xml.Async = false 
     xml.ValidateOnParse = true
     xml.setProperty "ServerHTTPRequest",true
     xml.loadXML(http.responseText)
     if xml.ParseError.errorCode<>0 then   
          Response.Write("Error:   "   &   xml.parseError.reason   &   "<br>")   
          Response.Write("Code:   0x"   &   hex(xml.parseError.errorCode)   &   "<br>")   
          Response.Write("At   Line:   "   &   xml.parseError.line   &   "<br>")   
          Response.Write("At   pos:   "   &   xml.parseError.linePos   &   "<br>")   
     else
          set oNode = xml.selectSingleNode("//ip") 
          Response.Write oNode.text
          set oNode = xml.selectSingleNode("//location") 
          Response.Write oNode.text
      end if
%>