0

我需要在 Java 中解析我的 XML 文件并根据条件检查一些支持。我尝试为此使用 DOM。

  <Config>
      <Configuration>
        <Descriptor>
          <e id="0">
            <family>Rest</family>
            <supportedList>
              <_length>5</_length>
              <_type>TypeX[]</_type>
              <e id="0">value-1</e>
              <e id="1">value-2</e>
              <e id="2">value-3</e>
              <e id="3">value-4</e>
              <e id="4">value-5</e>
            </supportedList>
          </e>
          <e id="1">
            <family>Rest1</family>
            <supportedList>
              <_length>5</_length>
              <_type>TypeX[]</_type>
              <e id="0">value1-1</e>
              <e id="1">value1-2</e>
              <e id="2">value1-3</e>
              <e id="3">value1-4</e>
              <e id="4">value1-5</e>
            </supportedList>
          </e>
          </Descriptor>
      </Configuration>
    </Config>

在上面的XML文件中,我需要遍历“e”块,然后是Descriptor块,然后搜索Family - Rest1,然后遍历supportedList。检查supportedList 是否包含用户提供的值,如果存在则返回true。

我尝试使用 DOM 解析器,但无法正确解析文件。我正在处理的实际 XML 文件有 20K 行,并且数据太多。对此有任何简单的解决方案。

4

1 回答 1

0
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xPathExpression = xpath.compile("//family[text()='Rest1']/e");

    NodeList list = (NodeList) xPathExpression
        .evaluate(xml, XPathConstants.NODESET);

在 XPath 语法上,搜索“XPath 备忘单”左右就足够了。

于 2013-04-16T11:02:51.767 回答