【问题标题】:how to get the attributes from xml file?如何从xml文件中获取属性?
【发布时间】:2012-05-03 12:32:37
【问题描述】:

我的 Xml 文件如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pointList SYSTEM "point.dtd">
<pointList>
<point unit="mm">
<x>2</x>
<y>3</y>
</point>

<point unit="cm">
<x>9</x>
<y>3</y>
</point>

<point unit="px">
<x>4</x>
<y>7</y>
</point>

</pointList>

当我尝试获取标签点的属性时:

import java.io.File;
import java.io.IOException;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class TryXml {
    public static void main (String [] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document doc = null;
        try {
            builder = factory.newDocumentBuilder();
        }
        catch (ParserConfigurationException e){
            System.out.println(e.getMessage());
        }
        File f = new File("p1.xml");
        try {
            doc=builder.parse(f);
        }
        catch (SAXException e){
            e.printStackTrace();
        }
        catch (IOException e){
            e.printStackTrace();
        }

        Element root = doc.getDocumentElement();
        System.out.println(root.getTagName());

        System.out.println("****************");


        NodeList nList = root.getChildNodes();
        for (int i=0;i<nList.getLength();i++) {
        if(nList.item(i) instanceof Element)

        System.out.println(nList.item(i).getAttributes());
    }

    }
    }

我得到的只是地址:

com.sun.org.apache.xerces.internal.dom.AttributeMap@3214512e
com.sun.org.apache.xerces.internal.dom.AttributeMap@53ddbcb1
com.sun.org.apache.xerces.internal.dom.AttributeMap@28f337b

谁能给我一个关于如何获取点属性以及其他内部标签的提示?

【问题讨论】:

    标签: java xml dom


    【解决方案1】:

    您可以将 println 替换为以下内容:

    System.out.println(((Element) nList.item(i)).getAttribute("unit"));
    

    这应该为您提供当前元素的“单位”属性。

    【讨论】:

    • 但是如果我使用“单位”我如何获得内部标签的值?我需要结果看起来像这样:point 23mm, point 93cm, point 47px
    • 您现在拥有了Element,并且可以从那里开始通过孩子:((Element) nList.item(i)).getChildNodes() 会给您另一个NodeList,您可以通过它完全像您在nList 中所做的那样进行迭代你的代码。
    【解决方案2】:

    使用,

    "元素根 = doc.getElementByTagName("pointList"); "

    而不是,

    "元素根 = doc.getDocumentElement();"

    【讨论】:

      【解决方案3】:

      Element.getAttributes() 将给出Element 中所有属性的列表。如果您事先知道属性的名称并且只想获取其值,请改用Element.getAttribute(String)

      如果需要获取子元素,请使用Element.getChildNodes()。要获取Element 或更具体地Node 中的文本,请使用getNodeValue()

      例如:

              NodeList nList = root.getChildNodes();
              String out = "";
              for (int i=0;i<nList.getLength();i++) {
                  // Assuming all first-level tags are <point>
                  Element point = (Element) nList.item(i);
                  String unit = point.getAttribute("unit");
                  out += "point ";
                  for (int y=0;y<point.getChildNodes().getLength();y++){
                      Node child = point.getChildNodes().item(y);
                      // String nodeName = child.getNodeName();
                      String nodeValue = child.getNodeValue();
                      out += nodeValue;
                  }
              out += unit;
              out += ", ";
              }
      

      将输出point 23mm, point 93cm, point 47px,

      【讨论】:

      • 我在 System.out.println() 中放了什么?我应该把它放在代码的什么地方?还有`元素点=(元素)nList.item(i); ` 表示不能转换为 org.w3c.dom.Element
      【解决方案4】:

      这篇文章getting xml attribute with java 完成了您想要实现的目标。它教授常规的 xml 解析和操作。它还检索属性。

      祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-18
        • 2013-01-31
        • 2023-01-10
        • 1970-01-01
        • 2010-11-18
        • 2011-10-19
        相关资源
        最近更新 更多