Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can子节点';标记是否与根节点相同?_Java_Xml - Fatal编程技术网

Java Can子节点';标记是否与根节点相同?

Java Can子节点';标记是否与根节点相同?,java,xml,Java,Xml,我被困在一个任务中,我必须从API的XML响应中读取数据。根节点和所有子节点具有相同的标记。 例如 价值1 价值2 价值3 价值4 java代码如下所示: . if(格式==“xml”){ 试一试{ URL=新URL(“https://api.worldbank.org/v2/country/“+cc+”/指示器/“+ic+”?刻度=y&format=“+format+”&date=“+start+”:“+end”); HttpURLConnection con=(HttpURLConne

我被困在一个任务中,我必须从API的XML响应中读取数据。根节点和所有子节点具有相同的标记。 例如


价值1
价值2
价值3
价值4
java代码如下所示: .

if(格式==“xml”){
试一试{
URL=新URL(“https://api.worldbank.org/v2/country/“+cc+”/指示器/“+ic+”?刻度=y&format=“+format+”&date=“+start+”:“+end”);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
int responseCode=con.getResponseCode();
BufferedReader in=新的BufferedReader(
新的InputStreamReader(con.getInputStream());
字符串输入线;
StringBuffer响应=新的StringBuffer();
而((inputLine=in.readLine())!=null){
追加(inputLine);
}
in.close();
字符串response1=response.toString();
response1=response1.substring(1);//解决API响应中的问题。参考https://stackoverflow.com/questions/11577420/fatal-error-11-content-is-not-allowed-in-prolog 错误
Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(新输入源(新StringReader(response1));
NodeList Node=doc.getElementsByTagName(“wb:data”);
if(Node.getLength()>0){
元素ele=(元素)节点。项(0);
NodeList Node2=(NodeList)doc.getChildNodes();
布尔标志=假;
字符串spage_total=ele.getAttribute(“页面”);
int page_total=Integer.parseInt(spage_total);
字符串scont=ele.getAttribute(“每页”);
int count=Integer.parseInt(scont);
字符串stotal=ele.getAttribute(“总计”);
int total=Integer.parseInt(stotal);
如果(总数<计数)计数=总数;
if(Node2.getLength()>0){
元素ele2=(元素)节点2。项(0);
字符串svalue=ele2.getElementsByTagName(“wb:date”).toString();
System.out.println(svalue);//这里写的是com.sun.org.apache.xerces.internal.dom。DeepNodeListImpl@6b1274d2
}}
}捕获(例外e){
系统输出打印ln(e);
}
}


我的问题是,是否可以从这样的XML中读取数据?

是的,这是完全有效的XML。它在可读性方面并没有太大帮助,但很好。我似乎无法访问子节点的元素。我总是收到这个-DeepNodeListImpl@c8e4bb0(@后面的值是可变的)。在问题中添加一些Java源代码。关于
if(format==“xml”)
-请参阅,我将对此进行更改。谢谢
<a //some attributes>
      <a>
        <element1>value1</element1>
        <element2>value2<element2>
      </a>
      <a>
        <element1>value3</element1>
        <element2>value4<element2>
      </a>
</a>
if (format == "xml") {
    try {

    URL url = new URL("https://api.worldbank.org/v2/country/" + cc + "/indicator/" + ic + "?scale=y&format=" + format + "&date=" + start + ":" + end);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    int responseCode = con.getResponseCode();

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    String response1 = response.toString();

    response1 = response1.substring(1); //resolving problem in API response. referred https://stackoverflow.com/questions/11577420/fatal-error-11-content-is-not-allowed-in-prolog for error
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new InputSource(new StringReader(response1)));


    NodeList Node = doc.getElementsByTagName("wb:data");

    if (Node.getLength() > 0) {
        Element ele = (Element)Node.item(0);
        NodeList Node2=(NodeList) doc.getChildNodes();
        boolean flag=false;
        String spage_total=ele.getAttribute("pages");

        int page_total=Integer.parseInt(spage_total);
        String scount=ele.getAttribute("per_page");

        int count=Integer.parseInt(scount);
        String stotal=ele.getAttribute("total");

        int total=Integer.parseInt(stotal);
        if (total < count) count = total;
        if(Node2.getLength()>0){
        Element ele2 = (Element)Node2.item(0);
        String svalue=ele2.getElementsByTagName("wb:date").toString();
        System.out.println(svalue);//this is where it says com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@6b1274d2
    }}
} catch (Exception e) {
    System.out.println(e);
}