java - 在 Java 中解析此 XML 的最简单方法?

标签 java xml dom xml-parsing sax

我有以下 XML:

     <ConfigGroup Name="Replication">
        <ValueInteger Name="ResponseTimeout">10</ValueInteger>
        <ValueInteger Name="PingTimeout">2</ValueInteger>
        <ValueInteger Name="ConnectionTimeout">10</ValueInteger>
        <ConfigGroup Name="Pool">
            <ConfigGroup Name="1">
                <ValueString Encrypted="false" Name="Host">10.20.30.40</ValueString>
                <ValueInteger Name="CacheReplicationPort">8899</ValueInteger>
                <ValueInteger Name="RadiusPort">12050</ValueInteger>
                <ValueInteger Name="OtherPort">4868</ValueInteger>
            </ConfigGroup>
            <ConfigGroup Name="2">
                <ValueString Encrypted="false" Name="Host">10.20.30.50</ValueString>
                <ValueInteger Name="CacheReplicationPort">8899</ValueInteger>
                <ValueInteger Name="RadiusPort">12050</ValueInteger>
                <ValueInteger Name="OtherPort">4868</ValueInteger>
            </ConfigGroup>
        </ConfigGroup>
     </ConfigGroup>

我只是想知道在 Java 中解析此 XML 的最简单方法是什么 - 我想要来自两个主机元素的值(例如 10.20.30.40 和 10.20.30.50)。请注意,可能有两个以上的池条目(或根本没有)。

我找不到一个简单的例子来说明如何使用 Java 的各种 XML 解析器。

非常感谢任何帮助。

谢谢!

最佳答案

搜索所需内容的最简单方法是 XPath。

try {

    //Load the XML File
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document configuration = builder.parse("configs.xml");

    //Create an XPath expression
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    XPathExpression expr = xpath.compile("//ConfigGroup/ValueString[@Name='Host']/text()");

    //Execute the XPath query
    Object result = expr.evaluate(configuration, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;

    //Parse the results
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue()); 
    }
} catch (ParserConfigurationException e) {
    System.out.println("Bad parser configuration");
    e.printStackTrace();
} catch (SAXException e) {
    System.out.println("SAX error loading the file.");
    e.printStackTrace();
} catch (XPathExpressionException e) {
    System.out.println("Bad XPath Expression");
    e.printStackTrace();
} catch (IOException e) {
    System.out.println("IO Error reading the file.");
    e.printStackTrace();
}

XPath 表达式

"//ConfigGroup/ValueString[@Name='Host']/text()"

在您的 XML 中的任何位置查找 ConfigGroup 元素,然后在 ConfigGroup 元素中查找 ValueString 元素,这些元素的 Name 属性值为“Host”。 @Name=Host 类似于名称为 ValueString 的元素的过滤器。最后是text(),返回选中元素的文本节点。

关于java - 在 Java 中解析此 XML 的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11720999/

相关文章:

Java XML 解析问题

javascript - 重新配置 DOM 无法正常工作

javascript - 如何从 iframe 内部获取带有 javascript 的 iframe 的高度?具有多个 iframe 的页面呢?

java - "? extends ParentClass"使只读?

java - 删除 XML 标签但不删除数据

java - 即使在允许的 url 上也可以访问 Jwt 过滤器

jquery - 有没有办法在 jQuery 中获取类似 JSONP 的 XML 提取?

java - 我怎样才能设置这个GUI的大小?

php - 从mysql获取结果并将每个结果应用到变量并最终输出

javascript - jQuery focus.bind(domObj) 是做什么的?