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?

EdXX
  • 872
  • 1
  • 14
  • 32
  • Can you use a more sophisticated XML library such as GSON or Jackson for the task? – Mick Mnemonic Jul 28 '17 at 22:11
  • If not, the [Java tutorial for XML->DOM](https://docs.oracle.com/javase/tutorial/jaxp/dom/readingXML.html) is a good starting point. – Mick Mnemonic Jul 28 '17 at 22:13
  • But in which file do you want to write the output ? In another file I assume ? – davidxxx Jul 28 '17 at 22:36
  • @davidxxx yes, the output should be in another flat file - .txt – EdXX Jul 28 '17 at 22:42
  • Try to write code first , then ask questions, there are plenty of tutorial available, refer this - https://www.tutorialspoint.com/java_xml/java_dom_parse_document.htm – Derrick Sep 06 '17 at 10:28

2 Answers2

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

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 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 ?? It's the begining of the xml file not only the nested part – EdXX Jul 28 '17 at 22:04
  • Ah I though that you had nested xml in your xml... It should easier so. – davidxxx Jul 28 '17 at 22:36
0

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

Good luck

Malakai
  • 3,011
  • 9
  • 35
  • 49