0

Hi I am trying to parse multiple xml's from a list, but every time I get same exact data instead of different data my list contain.getLatestNYData() provides me the xmls (10 suppose) and I am storng them in to. Now I need to parse all those xml's that is why I am looping through them and storing them in reqXML.

Code.

public List<NYProgramTO> getNYPPAData() throws Exception{
    this.getConfiguration();
    List<NYProgramTO> to = dao.getLatestNYData();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource source = new InputSource();

        String reqXML = "<servers>";
        for(NYProgramTO nyProgram: to){
            reqXML += nyProgram.getRequestXML();
        }
        reqXML = "</servers>";
        source.setCharacterStream(new StringReader(reqXML));

        Document document = builder.parse(source);

        NodeList list = document.getElementsByTagName("server");
        for(int iterate = 0; iterate < list.getLength(); iterate++){
            Node node = list.item(iterate);

            if(node.getNodeType() == Node.ELEMENT_NODE){
                Element element = (Element) node;

                for(NYProgramTO nyP : to ){
                    nyP.setFirstName(element.getElementsByTagName("FirstName").item(0).getTextContent());
                    nyP.setLastName(element.getElementsByTagName("LastName").item(0).getTextContent());
                    nyP.setPolicyNumber(element.getElementsByTagName("PolicyNumber").item(0).getTextContent());
                    nyP.setZipCode(element.getElementsByTagName("ZipCode").item(0).getTextContent());
                    nyP.setDateOfBirth(element.getElementsByTagName("BirthDate").item(0).getTextContent());
                }

                this.writeToExcel(to);
            }
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return to;
}

So every time reqXML is updated but then how can I achieve all those 10 xmls. Please guide.

Thanks

David
  • 257
  • 1
  • 8
  • 24

1 Answers1

0

Cause

when you do

for(NYProgramTO nyProgram: to){
    String reqXML = nyProgram.getRequestXML();
    source.setCharacterStream(new StringReader(reqXML));
}

you overwrite the source character stream at every iteration. So only the last one is written.

You need to build the whole XML in the loop and call the source.setCharacterStream outside of the loop.

Solution

Given that each XML bit is of the form

<server><requests>.......</server></request>

This should work:

String reqXML = "<servers>";
for(NYProgramTO nyProgram: to){
    reqXML += nyProgram.getRequestXML();
}
reqXML += "</servers>"
source.setCharacterStream(new StringReader(reqXML));

Regarding your parsing issue

I tried that code:

try {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource source = new InputSource();

        String reqXML =
            "<servers>" +
            "  <server>" +
            "    <FirstName>Nicolas</FirstName>" +
            "  </server>" +
            "  <server>" +
            "    <FirstName>Peter</FirstName>" +
            "  </server>" +
            "</servers>";

    source.setCharacterStream(new StringReader(reqXML));

    Document document = builder.parse(source);

    NodeList list = document.getElementsByTagName("server");
    for(int iterate = 0; iterate < list.getLength(); iterate++){
        Node node = list.item(iterate);

        if(node.getNodeType() == Node.ELEMENT_NODE){
            Element element = (Element) node;

            String firstName = element.getElementsByTagName("FirstName").item(0).getTextContent();
            System.out.println(firstName);
        }
    }

} catch (Exception ex) {
    ex.printStackTrace();
}

And it works, printing out Nicolas and Peter as expected. So I think your XML is not well formed, can you add the XML you get in reqXML? There might be unwanted characters in the XML. See this question org.xml.sax.SAXParseException: Content is not allowed in prolog

Indeed, If I use the follwoing XML (adding an unwanted character before the first tag) in my example above I get the same error as you org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.

String reqXML =
        "t<servers>" +
        "  <server>" +
        "    <FirstName>Nicolas</FirstName>" +
        "  </server>" +
        "  <server>" +
        "    <FirstName>Peter</FirstName>" +
        "  </server>" +
        "</servers>";
Bentaye
  • 9,403
  • 5
  • 32
  • 45
  • Thanks Bentaye for the response. getRequestXML() gives me a long XML payload. So basically form multiple xml files I am reading some tags so in this I expect 10 different names form 10 different xml's . Hope it is clear. – David Feb 23 '18 at 20:04
  • @David Can you try my solution, and if it does not work, tell me what you expect differently – Bentaye Feb 23 '18 at 20:06
  • Now I am getting this exception org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog. – David Feb 23 '18 at 20:10
  • I edited the example code to surround the XML with a tag. Would it work better ? – Bentaye Feb 23 '18 at 20:13
  • I get this error now: The markup in the document preceding the root element must be well-formed. Document document = builder.parse(source); in this line. I have added this lne in my question – David Feb 23 '18 at 20:18
  • I have posted the complete method I wrote. – David Feb 23 '18 at 20:26
  • I don't have time right now, but I will have a look tomorrow if you don't get the solution by then. – Bentaye Feb 23 '18 at 20:40
  • @David I added some remarks to the answer, I think your XML is simply malformed somehow. Your code works for me. – Bentaye Feb 24 '18 at 07:45