3

I have a .xml file like below:

    <?xml version="1.0"?>
    <Event>
    <Issue>ggg</Issue>
    <City>Athen</City>   
      <Group>
      <AlternateIdentification>
        <AlternateID>DG800</AlternateID>
        <AlternateIDType>GoA</AlternateIDType>
      </AlternateIdentification>
      <AlternateIdentification>
        <AlternateID>SS500</AlternateID>
        <AlternateIDType>SDD</AlternateIDType>
      </AlternateIdentification>
      <AlternateIdentification>
        <AlternateID>TY158</AlternateID>
        <AlternateIDType>YTU</AlternateIDType>
      </AlternateIdentification>
      </Group>
    </Event>

And I would like to parse .xml file and write the output to the flat .txt file with lines like this:

ggg Athen DG800
ggg Athen SS500
ggg Athen TY158

Can you help me and tell me how to do this with javax DOM parser? I have no idea how to start :( This common part confuses me the most because I need to iterate this file in this case 3 times to get 3x "ggg Athen" and then additional tag AlternateID?

5
  • Can you use a more sophisticated XML library such as GSON or Jackson for the task? Commented Jul 28, 2017 at 22:11
  • If not, the Java tutorial for XML->DOM is a good starting point. Commented Jul 28, 2017 at 22:13
  • But in which file do you want to write the output ? In another file I assume ? Commented Jul 28, 2017 at 22:36
  • @davidxxx yes, the output should be in another flat file - .txt Commented Jul 28, 2017 at 22:42
  • Try to write code first , then ask questions, there are plenty of tutorial available, refer this - tutorialspoint.com/java_xml/java_dom_parse_document.htm Commented Sep 6, 2017 at 10:28

2 Answers 2

2

Java - parse nested xml file and write to the file

A simple way :

  1. Read line by line with BufferedReader.readLine() until finding the start of the nested xml part.
    For example : <?xml version="1.0"?>

  2. When you identified this line, add each read line in a StringBuilder instance until you encounter the end of the xml part that you want to analyse. For example the end tag of the root element of it.
    Here : </Event>

  3. Create a org.w3c.dom.Document from the String contained in the StringBuilder :

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader( stringBuilder.toString())));

  4. Use your preferred way to find data in the document : dom, jdom, xpath, etc...

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

2 Comments

I don't even understand the first point from your answer :( Read line by line until the start of the nested xml part. For example <?xml version="1.0"?> ?? It's the begining of the xml file not only the nested part
Ah I though that you had nested xml in your xml... It should easier so.
0

You definitely need to look at Sax XML Parser. Tutorial for that can be found here

Good luck

1 Comment

But how to do this with javax DOM parser??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.