0

I need to parse am XML file in Java and check some support based on the condition. I tried using the DOM for this.

  <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>

In the above XML file,I need to go through the "e" block and then Descriptor block, and then search for Family - Rest1 and then iterate through supportedList. Check if supportedList contains user provided value if it exists then return true.

I tried using a DOM parser but I am not able to parse the file properly. The actual XML file that I am working on, is of 20K lines and has too much data. Any easy solution on this.

4
  • 1
    Show us what you have tried so far. Commented Apr 16, 2013 at 10:47
  • 1
    XPath would be better than DOM for this Commented Apr 16, 2013 at 10:51
  • the actual file has quite a sensitive data and the same thing I have used it in my code as well so could not share the code. Commented Apr 16, 2013 at 10:51
  • possible duplicate of How can I parse XML using Java? Commented Apr 16, 2013 at 11:47

1 Answer 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);

And on XPath syntax, it suffices to search for "XPath cheat sheet" or so.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.