如何从Java中的XML中的名称 - 值对属性中获取值

时间:2017-06-19 18:14:10

标签: java xml

以下是本地xml文件的内容     

<configuration>


<property>

<name>test</name>

<value>A</value>

</property>

</configuration>

我如何阅读价值&#34; A&#34;在java? 这是我在Java中测试的代码

    Properties properties = new Properties();
    FileInputStream fis = new FileInputStream("path to file");
    properties.load(fis);
    String test = properties.getProperty("test");
    System.out.println(test);

java代码有问题吗?我似乎无法将名称与值相关联。此外,我无法读取属性值,因为真实文件包含多对名称值。

 File file = new File("path to file");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
            .newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document dom = documentBuilder.parse(file);
    Element docEle = dom.getDocumentElement();
    NodeList nl = docEle.getChildNodes();
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> values = new ArrayList<String>();
    if (nl != null) {
        int length = nl.getLength();
        for (int i = 0; i < length; i++) {
            if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element el = (Element) nl.item(i);
                if (el.getNodeName().contains("configuration")) {
                    names.add( el.getElementsByTagName("name").item(0).getTextContent());
                    System.out.println(el.getElementsByTagName("name").item(0).getTextContent());
                    values.add( el.getElementsByTagName("value").item(0).getTextContent());
                    System.out.println(el.getElementsByTagName("value").item(0).getTextContent());
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

你可以做这样的事情:

    File file = new File("path to file");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
        .newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(file);
    document.getDocumentElement().normalize();
    Element docEle = document.getDocumentElement();
    NodeList nl = docEle.getElementsByTagName("property");
    ArrayList names = new ArrayList();
    ArrayList values = new ArrayList();
    if (nl != null) {
        int length = nl.getLength();
        //System.out.println(length);
        for (int i = 0; i < length; i++) {
            if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element el = (Element) nl.item(i);
                if (el.getNodeName().contains("property")) {
                    names.add( el.getElementsByTagName("name").item(0).getTextContent());
                    //System.out.println(el.getElementsByTagName("name").item(0).getTextContent());
                    values.add( el.getElementsByTagName("value").item(0).getTextContent());
                    //System.out.println(el.getElementsByTagName("value").item(0).getTextContent());
                }
            }
        }
    }

这会将所有值添加到ArrayLists中,然后您可以根据需要访问它们。或者您可以像我留下的评论一样打印它们。