0

How to parse xml file, so that it would write the same nodes to in another file several times, one after the other? For example:

1.Xml file to be parsed:
 <?xml version="1.0" encoding="UTF-8"?>
  <ALL>
    <VER>7.0</VER>
    <NATIONALITY>FIN</NATIONALITY>
    <DATA>
    <USER>ED</USER>
    <PLZ>XXX</PLZ>
    <BEGIN>2015-05-16</BEGIN>
    <CURRENCY>90</CURRENCY>
    <MARKE>KIA</MARKE>
    <DATA>
  </ALL>

2.As a result, I want to do so that was saved in another xml file, several times:

<?xml version="1.0" encoding="UTF-8"?>
<ALL>
  <VER>7.0</VER>
   <NATIONALITY>FIN</NATIONALITY>
    <DATA>
    <USER>ED</USER>
    <PLZ>XXX</PLZ>
    <BEGIN>2015-05-16</BEGIN>
    <CURRENCY>90</CURRENCY>
   <MARKE>KIA</MARKE>
  <DATA>
 </ALL> 

<ALL>
 <VER>7.0</VER>
  <NATIONALITY>FIN</NATIONALITY>
  <DATA>
    <USER>ED</USER>
    <PLZ>XXX</PLZ>
    <BEGIN>2015-05-16</BEGIN>
    <CURRENCY>90</CURRENCY>
    <MARKE>KIA</MARKE>
    <DATA>
  </ALL>
  ...

This is my previous question: Copy nodes in the same output xml file -java

1

2 Answers 2

1

The second example is not an XML file.

Add an external tag and appendChild to the root and save. The file will remain valid then.

<?xml version="1.0" encoding="UTF-8"?>
  <ALLs>
    <ALL>
    <VER>7.0</VER>
    <NATIONALITY>FIN</NATIONALITY>
    <DATA>
    <USER>ED</USER>
    <PLZ>XXX</PLZ>
    <BEGIN>2015-05-16</BEGIN>
    <CURRENCY>90</CURRENCY>
    <MARKE>KIA</MARKE>
    <DATA>
  </ALL>
  </ALLs>

You may use random access files, jump to the end, position back before the write the new XML snippet and write again the ALLs. This should be fast.

Another option, which is useful with streams, to write the XML header always.

In this case you have to parse one-by-one, taking care that the InputStream what you pass for parsing does not close the inputStream (make your own subclass with a close that, indeed, does not close the file, and close externally; otherwise the XML parser will close the file).

<?xml version="1.0" encoding="UTF-8"?>
<ALL>
  <VER>7.0</VER>
   <NATIONALITY>FIN</NATIONALITY>
    <DATA>
    <USER>ED</USER>
    <PLZ>XXX</PLZ>
    <BEGIN>2015-05-16</BEGIN>
    <CURRENCY>90</CURRENCY>
   <MARKE>KIA</MARKE>
  <DATA>
 </ALL> 

<?xml version="1.0" encoding="UTF-8"?>
<ALL>
 <VER>7.0</VER>
  <NATIONALITY>FIN</NATIONALITY>
  <DATA>
    <USER>ED</USER>
    <PLZ>XXX</PLZ>
    <BEGIN>2015-05-16</BEGIN>
    <CURRENCY>90</CURRENCY>
    <MARKE>KIA</MARKE>
    <DATA>
  </ALL>

Another option to use, e.g. JSON; convert your XML to JSON and append to a file; when reading, read line by line and convert the JSON to XML as you wish

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

Comments

0

Since your output doesn't contain a single root tag, it won't be valid XML. It sounds like you just want to output first line of file and then repeat subsequent lines an arbitrary number of times. If that's the case, you don't really care about fact that input file is [valid] XML. You could try something like the following:

final int cnt = 2;
Files.write(
    Paths.get("out.txt"),
    new AbstractList<String>() {
        private final List<String> mStrs = Files.readAllLines(Paths.get("in.xml"));

        public int size() {return 1 + (mStrs.size() - 1) * cnt;}

        public String get(int pIdx) {
            return
             pIdx == 0 ?
             mStrs.get(0) :
             mStrs.get((pIdx - 1) % (mStrs.size() - 1) + 1);
        }
    }
);

Note that this solution assumes that input XML header is present.

Comments

Your Answer

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