简体   繁体   English

在Javascript中解析XML的问题

[英]Problems parsing XML in Javascript

I have a javascript function that makes an http_request to a php file on my server that generates an XML file (see output below). 我有一个javascript函数,可对服务器上的php文件进行http_request生成XML文件(请参见下面的输出)。 When the XML file is returned the same javascript function parses the XML (This is where my problem lies) and passes it to other functions; 当返回XML文件时,相同的javascript函数将解析XML(这是我的问题所在) ,并将其传递给其他函数。 which do the bulk of the processing. 负责大部分的处理。

So far I've been unable to parse my XML document and I can't quite figure out why. 到目前为止,我一直无法解析我的XML文档,我还不太清楚为什么。

XML XML格式

<Results><!--Root-->
  <Result_Set>
    <State>State</State>
    <Cities>
      <City>City 1</City>
      <City selected="true">City 2</City>
       ...ETC...
    </Cities>
    <Zipcodes>
       <Zipcode selected="true">Zipcode 1</Zipcode
       <Zipcode>Zipcode 2</Zipcode>
        ...ETC...
    </Zipcodes>
  </Result_Set>
</Results>

Javascript Java脚本

function GetZipInfo(zipcode){
    var xmlhttp;
    var x,resultSet,state,cities,zipcodes

    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }
    else{// code for IE6, IE5
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }

    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){  

            resultSet=xmlhttp.responseXML.documentElement.getElementsByTagName("Result_Set") //Function Crashes Here

            for(x=0;x<resultSet.length;x++){
                state=resultSet[x].getElementsByTagName("State")[0].nodeValue;
                cities=resultSet[x].getElementsByTagName("Cities");
                zipcodes=resultSet[x].getElementsByTagName("Zipcodes");

                selectState(state)
                xmlDropdown(cities, "City", "Cities")
                xmlDropdown(zipcodes, "Zipcode", "Zipcodes")
            }
        }
    }   
    xmlhttp.open("GET","GetZipInfo.php?Zipcode="+zipcode,true);
    xmlhttp.send();
}

I have never parsed an XML document in ANY language before, so I think it's safe to say that I'm completely lost as to what's wrong. 我以前从未解析过任何语言的XML文档,因此我可以肯定地说我完全错了。

Thank you in advance! 先感谢您!

EDIT: It turns out that my response is coming back as responseText instead of responseXML 编辑:原来,我的响应作为responseText而不是responseXML回来

responseText responseText

I am using php to create the XML page: 我正在使用php创建XML页面:

header("Content-Type: text/plain");
//Create the DOM
echo $xmlDoc->saveXML()

Still unsure why it's not returning as XML. 仍然不确定为什么它不以XML返回。 Could it have something to do with echo $xmlDoc->saveXML() ? 可能与echo $ xmlDoc-> saveXML()有关吗?

Edit : I agree with several comments that my problem is with my header in the XML file. 编辑 :我同意一些评论,我的问题是与我的XML文件中的标头。 I added the line "alert(xmlhttp.responseText)" to my code. 我在代码中添加了“ alert(xmlhttp.responseText)”行。 Which displays: 显示:

<?xml version="1.0"?>
<!--The Contents of my XML file-->

Does the encoding type need to be set for this to work correctly. 是否需要设置编码类型才能使其正常工作。 And if so, how can I modify my PHP code (see above) to insert that encoding? 如果是这样,如何修改我的PHP代码(请参见上文)以插入该编码?

The MIME type of your response should be text/xml , or something ending with +xml ( RFC 3023 ). 您的响应的MIME类型应为text/xml或以+xml结尾的内容( RFC 3023 )。

Also, you should add a XML declaration before the first line. 另外,您应该在第一行之前添加XML声明

And last, but not least, although getElementsByTagName is available on all elements in HTML documents, you should use document.getElementsByTagName in XML documents: 而在去年,但并非最不重要的,虽然getElementsByTagName可以用HTML文档中的所有元素,你应该使用document.getElementsByTagName在XML文档:

var resultSet = xmlhttp.responseXML.getElementsByTagName("Result_Set");

At the top of your code your missing : 在代码的顶部,您缺少的内容:

<?xml version="1.0" encoding="UTF-8" ?>

Also missing ';' 还缺少“;” after these lines : 在这些行之后:

selectState(state)
xmlDropdown(cities, "City", "Cities")
xmlDropdown(zipcodes, "Zipcode", "Zipcodes")

In XML also missing '>' after : 在XML中,之后也缺少“>”:

</Zipcode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM