Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8922005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:42:42+00:00 2026-06-15T06:42:42+00:00

Here is the code am trying to merge merge multiple XML files. public static

  • 0

Here is the code am trying to merge merge multiple XML files.

public static void mergeXml(String directory) throws Exception {
    File dir = new File(directory);
    File[] rootFiles = dir.listFiles();
    XMLEventWriter eventWriter;
    XMLEventFactory eventFactory;
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream("temp/testMerge1.xml"));
    eventFactory = XMLEventFactory.newInstance();

    // Create and write Start Tag
    StartDocument startDocument = eventFactory.createStartDocument("ISO-8859-1");
    eventWriter.add(startDocument);

    for(File rootFile : rootFiles){
        XMLEventReader test = inputFactory.createXMLEventReader(new StreamSource(rootFile));
        while(test.hasNext()){
            XMLEvent event= test.nextEvent();
            //avoiding start(<?xml version="1.0"?>) and end of the documents;
            if (event.getEventType()!= XMLEvent.START_DOCUMENT && event.getEventType() != XMLEvent.END_DOCUMENT)
                eventWriter.add(event);         

            test.close();
        }           

        eventWriter.add(eventFactory.createEndDocument());
        eventWriter.close();
    }

}

am getting two problems

  1. the output file is not having any encoding
  2. when am trying to parse the file created by this code am getting the following exception
[Fatal Error] :1:2493: The markup in the document following the root element must be well-formed.
org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at SplitMain.validateInputFile(SplitMain.java:139)
    at SplitMain.main(SplitMain.java:76)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:42:43+00:00Added an answer on June 15, 2026 at 6:42 am
    // Create and write Start Tag
    StartDocument startDocument = eventFactory.createStartDocument("ISO-8859-1");
    eventWriter.add(startDocument);
    

    This does not create a root element for the output XML document, it simply writes the <?xml declaration. After the StartDocument you also need to add a suitable StartElement:

    StartElement startRootElt = eventFactory.createStartElement("", "http://example.com", "root");
    eventWriter.add(startRootElt);
    

    The next problem is that you’re closing the eventWriter inside the for loop:

        eventWriter.add(eventFactory.createEndDocument());
        eventWriter.close();
    }
    

    You need to move this outside the for loop, and also end the root element we started above

    }
    eventWriter.add(eventFactory.createEndElement("", "http://example.com", "root"));
    eventWriter.add(eventFactory.createEndDocument());
    eventWriter.close();
    

    Additionally, if any of your XML files has a <!DOCTYPE you may run into problems. You may just be able to ignore DTD events in the same way you’re currently ignoring start and end document events, but whether or not this works depends on exactly what is declared in that DTD. You’ll have to try it and see.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get an HTML-based recursive directory listing based on code here:
I'm trying to create a custom Android compound view, here is the code: <?xml
I am trying to merge two videos, but it always throws this exception: -[NSURL
I am trying to merge audio files with video files. Here is my effort:
Here is the code i was trying.I could not comprehend the output i got.Why
Here's my code I'm trying to get working to no avail: CREATE DEFINER=`auser`@`localhost` PROCEDURE
I am trying to reduce some code here. I will explain how I have
i am trying to validate someone else's code here and they are using the
I am trying to test some of these code here http://ha.ckers.org/xss.html on my code.
Trying to truncate some code here and running into a problem: <script type=text/javascript> $(function()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.