Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML file containing multiple root elements

I have a file which contains multiple sets of root elements. How can I extract the root element one by one?

This is my XML

<Person>
    <empid></empid>
    <name></name>
</Person>
<Person>
    <empid></empid>
    <name></name>
</Person>
<Person>
    <empid></empid>
    <name></name>
</Person>

How can I extract one set of Person at a time?

like image 976
Gopal2311 Avatar asked Sep 02 '25 15:09

Gopal2311


2 Answers

Use java.io.SequenceInputStream to trick xml parser:

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MultiRootXML{
    public static void main(String[] args) throws Exception{
        List<InputStream> streams = Arrays.asList(
                new ByteArrayInputStream("<root>".getBytes()),
                new FileInputStream("persons.xml"),
                new ByteArrayInputStream("</root>".getBytes())
        );
        InputStream is = new SequenceInputStream(Collections.enumeration(streams));
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
        NodeList children = doc.getDocumentElement().getChildNodes();
        for(int i=0; i<children.getLength(); i++){
            Node child = children.item(i);
            if(child.getNodeType()==Node.ELEMENT_NODE){
                System.out.println("persion: "+child);
            }
        }
    }
}
like image 81
Santhosh Kumar Tekuri Avatar answered Sep 05 '25 08:09

Santhosh Kumar Tekuri


You cannot parse your file using an XML parser because your file is not XML. XML cannot have more than one root element.

You have to treat it as text, repair it to be well-formed, and then you can parse it with an XML parser.

like image 42
kjhughes Avatar answered Sep 05 '25 09:09

kjhughes