【问题标题】:How to get root node attributes on java如何在java上获取根节点属性
【发布时间】:2011-06-11 16:46:36
【问题描述】:

我有一个如下所示的 xml 文件。我想获取药房节点的纬度和经度属性。我可以获取 chilnodes 属性但无法获取根节点属性。我是 java 和 xml 的新手。我找不到解决办法。

<pharmacies Acc="4" latitude="36.8673380" longitude="30.6346640" address="Ayujkila">
        <pharmacy name="sadde" owner="" address="dedes" distance="327.000555668" phone="342343" lat="36.8644" long="30.6345" accuracy="8"/>
        <pharmacy name="Sun " owner="" address="degerse" distance="364.450016586" phone="45623" lat="36.8641" long="30.6353" accuracy="8"/>
        <pharmacy name="lara" owner="" address="freacde" distance="927.262190129" phone="564667" lat="36.8731" long="30.6422" accuracy="8"
    <end/>
    </pharmacies>

这是我的部分代码。我从 url 地址获取 xml 文件。

                    DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
                 DocumentBuilder db = dbf.newDocumentBuilder();
                 Document doc = db.parse(new InputSource(url.openStream()));
                 doc.getDocumentElement().normalize();
                    NodeList nodeList =doc.getElementsByTagName("pharmacy");
                 for (int i = 0; i < nodeList.getLength(); i++){
                      Node node =nodeList.item(i);
                      Element fstElmnt = (Element) node;
                      NodeList pharmacyList = fstElmnt.getElementsByTagName("pharmacy");
                      Element pharmacyElement = (Element) pharmacyList.item(0);
              Element pharmacyElement = (Element) pharmacyList.item(0);

              HashMap<String,String>map=new HashMap<String,String>();                   
              map.put("name", pharmacyElement.getAttribute("name"));
              map.put("distance", pharmacyElement.getAttribute("phone"));
              list.add(map);

              latt.add(pharmacyElement.getAttribute("lat"));

                    ....

【问题讨论】:

    标签: java xml eclipse


    【解决方案1】:

    如果您需要获取根节点药房的属性,则需要使用药房而不是药房。并使用 getAttributes 方法代替。您可以在此站点上看到很多示例。 http://java.sun.com/developer/codesamples/xml.html#dom

    【讨论】:

      【解决方案2】:

      doc.getDocumentElement() 将返回根元素,您可以在其上调用 getAttribute( attrName ),就像在任何其他元素上一样。

      【讨论】:

        【解决方案3】:

        &lt;pharmacies&gt; 元素本身可以使用

        Element pharmacies = doc.getDocumentElement();
        

        你可以从中获取属性。

        【讨论】:

          【解决方案4】:

          尝试以下方法:

              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = dbf.newDocumentBuilder();
              Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
              doc.getDocumentElement().normalize();
              System.out.println(doc.getChildNodes().getLength());
              Node item = doc.getChildNodes().item(0);
              System.out.println(item.getNodeName());
              Node lat = item.getAttributes().getNamedItem("latitude");
              String s = lat.getNodeValue();
              System.out.println(s.equals("36.8673380")); // Value of /pharmacies[@latitude]/value()
          

          【讨论】:

          • 是的,它就像斯卡夫曼的回答一样工作。非常感谢你帮助我!!
          【解决方案5】:

          为我尝试它的工作,Res 是你的最终字符串:

          doc = b.parse(new ByteArrayInputStream(result.getBytes("UTF-8")));
          
              Node rootNode=doc.getDocumentElement();
          
              res = rootNode.getNodeName().toString();
          

          【讨论】:

          • 亲爱的@user3153156,这将只提供rootNode的名称,这里的要求是获取rootNode的名为“纬度”和“经度”的属性。 :)
          【解决方案6】:

          &lt;pharmacies&gt; 本身就是一个元素,可以使用

          Element pharmacies = doc.getDocumentElement();

          现在,Elements 的这个pharmacies 引用变量保存了&lt;pharmacies&gt; 元素下的所有属性。我们可以使用属性名称一一获取所需的属性:

          pharmacies.getAttribute("latitude");   // Will give 36.8673380
          pharmacies.getAttribute("longitude");  // Will give 30.6346640
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-08-05
            • 2011-11-20
            • 1970-01-01
            • 2022-07-01
            相关资源
            最近更新 更多