java - 使用java进行Xml条件解析

标签 java xml

我想从 XML 文件中提取仅选定的标签:

<Shape>
    <ShapeType>H2</ShapeType>
    <Annotation>
        <Properties>
            <PropertyValue PropertyName="field_label">label.modelSeriesCd</PropertyValue>
            <PropertyValue PropertyName="ContainerType">conditionContainer</PropertyValue>
        </Properties>
    </Annotation>
    <FootnoteNumber>1</FootnoteNumber>
    <Name>label.modelSeriesCd</Name>
    <Rectangle>
        <Rectangle X="14" Y="94" Width="43" Height="12" />
    </Rectangle>
</Shape>
<Shape>
    <ShapeType>H2</ShapeType>
    <Annotation>
        <Properties>
            <PropertyValue PropertyName="field_label">label.modelSeriesMd</PropertyValue>
            <PropertyValue PropertyName="ContainerType">mContainer</PropertyValue>
        </Properties>
    </Annotation>
    <FootnoteNumber>1</FootnoteNumber>
    <Name>label.modelSeriesCd</Name>
    <Rectangle>
        <Rectangle X="14" Y="94" Width="43" Height="12" />
    </Rectangle>
</Shape>

我只想提取那些将“conditionContainer”作为“propertyValue”值的标签以及标签内的所有标签 我正在尝试下面的代码:

private static void visitChildNodes(NodeList nList)
{
    for (int index = 0; index < nList.getLength(); index++)
    {
        Node node = nList.item(index);
        if (node.getNodeType() == Node.ELEMENT_NODE)
        {
            if(node.getNodeName().equalsIgnoreCase("shape"))
                System.out.println("Node Name = " + node.getNodeName() + "; Value = " + node.getTextContent());

请建议我一种方法来做到这一点。

最佳答案

我建议通过您的 Document 对象进行递归搜索,因为您要查找的内容有几层深度。

创建一个函数,递归地调用自身,传递您当前所在的节点以及您正在查找的标签以及该标签必须具有的值。

类似...

public static void main(String[] args) throws Exception {
    String xml
            = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<Shapes>\n"
            + " <Shape>\n"
            + "     <ShapeType>H2</ShapeType>\n"
            + "     <Annotation>\n"
            + "         <Properties>\n"
            + "             <PropertyValue PropertyName=\"field_label\">label.modelSeriesCd</PropertyValue>\n"
            + "             <PropertyValue PropertyName=\"ContainerType\">conditionContainer</PropertyValue>\n"
            + "         </Properties>\n"
            + "     </Annotation>\n"
            + "     <FootnoteNumber>1</FootnoteNumber>\n"
            + "     <Name>label.modelSeriesCd</Name>\n"
            + "     <Rectangle>\n"
            + "         <Rectangle X=\"14\" Y=\"94\" Width=\"43\" Height=\"12\" />\n"
            + "     </Rectangle>\n"
            + " </Shape>\n"
            + " <Shape>\n"
            + "     <ShapeType>H2</ShapeType>\n"
            + "     <Annotation>\n"
            + "         <Properties>\n"
            + "             <PropertyValue PropertyName=\"field_label\">label.modelSeriesMd</PropertyValue>\n"
            + "             <PropertyValue PropertyName=\"ContainerType\">mContainer</PropertyValue>\n"
            + "         </Properties>\n"
            + "     </Annotation>\n"
            + "     <FootnoteNumber>1</FootnoteNumber>\n"
            + "     <Name>label.modelSeriesCd</Name>\n"
            + "     <Rectangle>\n"
            + "         <Rectangle X=\"14\" Y=\"94\" Width=\"43\" Height=\"12\" />\n"
            + "     </Rectangle>\n"
            + " </Shape>\n"
            + "</Shapes>";

    Document xmlDocument = DocumentBuilderFactory
            .newInstance()
            .newDocumentBuilder()
            .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

    Node node = findPropertyTagAndValue(xmlDocument.getFirstChild(), "PropertyValue", "conditionContainer");
    if (node != null) {
        System.out.println("Node Name = " + node.getNodeName() + "; Value = " + node.getTextContent());
    }
}

public static Node findPropertyTagAndValue(Node node, String propertyTag, String propertyValue) {
    if (node == null) {
        // The node we're looking for does not exist
        return null;
    } else if (node.getNodeType() != Node.ELEMENT_NODE) {
        // Move to the next sibling node
        return findPropertyTagAndValue(node.getNextSibling(), propertyTag, propertyValue);
    } else if (node.getNodeName().equalsIgnoreCase(propertyTag) && node.getTextContent().equalsIgnoreCase(propertyValue)) {
        // We found the node we are looking for
        return node;
    } else if (node.hasChildNodes()) {
        // Check into the child nodes
        Node childNode = findPropertyTagAndValue(node.getFirstChild(), propertyTag, propertyValue);
        if (childNode == null) {
            // Nothing found in child node, so move to next sibling
            childNode = findPropertyTagAndValue(node.getNextSibling(), propertyTag, propertyValue);
        }
        return childNode;
    } else {
        // Move to the next sibling
        return findPropertyTagAndValue(node.getNextSibling(), propertyTag, propertyValue);
    }
}

结果:

Node Name = PropertyValue; Value = conditionContainer

关于java - 使用java进行Xml条件解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31137777/

相关文章:

java - JSONObject ["locationInfo"]不是字符串

java - 在 Java 中将 DOM 节点呈现为字符串而不指定 namespace

java - 线程 "main"中的异常 org.springframework.jdbc.CannotGetJdbcConnectionException

java - 为android webview java添加进度条

c# - 解析包含非法字符的 XML

Java、GTFS Realtime、protocol buffers,简单搞定?

ruby-on-rails - 如何使用 Nokogiri 验证 XML?

css - 如果在 openerp 7 中使用 css 不为空,如何将字段设置为只读?

c++ - C++/C 中用于 Windows 的 XMLRPC

java - 使用谷歌gson的hashmap中的json数组