java - 使用Java获取XML子元素

标签 java xml xml-parsing

我真的不太擅长操作 XML,我需要一些帮助。 这是我的 XML 文件的示例:

<?xml version="1.0"?>
<components>
    <resources>
        <resource id="House">
            <id>int</id>
            <type>string</type>
            <maxUsage>float</maxUsage>
            <minUsage>float</minUsage>
            <averageUsage>float</averageUsage>
        </resource>
        <resource id="Commerce">
            <id>int</id>
            <type>string</type>
            <maxUsage>float</maxUsage>
            <minUsage>float</minUsage>
            <averageUsage>float</averageUsage>
        </resource>
    </resources>
    <agregatorsType1>
        <agregator1 id="CSP">
            <id>int</id>
            <type>string</type>
        </agregator1>
    </agregatorsType1>
    <soagregatorsType0>
        <agregator0 id="VPP">
            <id>int</id>
            <type>string</type>
        </agregator0>
    </agregatorsType0>
</components>

我需要打印每个资源和每个聚合器的子元素(id、type、maxUsage 等)。

这是我的方法:

public static Document createXMLDocument() throws IOException, Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    Document documento = docBuilder.parse(filepath);
    documento.getDocumentElement().normalize();
    return documento;
}

public static String[] readSubElementsXML() throws IOException, Exception 
{   

    Document documento = createXMLDocument();

    //gets XML elements 
    Element root = documento.getDocumentElement();
    NodeList nListR = root.getElementsByTagName("resource");
    NodeList nListA1 = root.getElementsByTagName("agregator1");
    NodeList nListA0 = root.getElementsByTagName("agregator0");

    ArrayList<Node> allNodes = appendNodeLists(nListR, nListA1, nListA0); //this method merges the 3 NodeLists into one ArrayList

    int tam = allNodes.size();
    String[] vec = new String[tam];

    for (int i = 0; i < tam; i++) {
        Element elem = (Element) allNodes.get(i);               
        vec[i] =  elem.getAttribute("id");
        System.out.println(""+vec[i]);
    }
    return vec;
}

这样我只能获取属性 id,而我不需要它。我需要打印所有子元素,即使我将子元素添加到 XML 文件中,它也必须正常工作。

我怎样才能做到这一点?

最佳答案

Element 是 Node 的子类。请参阅Node#getChildNodes() javadoc。

A NodeList that contains all children of this node. If there are no children, this is a NodeList containing no nodes.

然后您就可以迭代子节点,例如

    NodeList childNodes = elem.getChildNodes();
    int childCount = childNodes.getLength();
    Node childNode;
    for (int i = 0; i < childCount; i++) {
        childNode = childNodes.item(i);
        // do things with the node
    }

关于java - 使用Java获取XML子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16705306/

相关文章:

java - 如何缩短嵌套的 if 表达式?

android - android studio 不识别 Activity 继承

java - 如何在一个 java 应用程序中处理不同版本的 xsd 文件?

php - 如何使用PHP修改xml文件

Python XML 解析示例

c++ - 如何计算 TinyXml 中的元素?

java - 如何返回我明确作为参数传递的类型?

java - Magnolia 富文本字段

python - Minidom - 检查 XML 中是否存在标签

java - Servlet - 如何从数据库下载多个文件