如何从列表中解析多个xmls?

时间:2018-02-23 19:42:15

标签: java xml parsing

您好我正在尝试从列表中解析多个xml,但每次我得到相同的精确数据而不是不同的数据我的列表包含.getLatestNYData()为我提供了xmls(假设为10)并且我将它们存入。现在我需要解析所有那些xml,这就是我循环遍历它们并将它们存储在reqXML中的原因。

代码。

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;
}

所以每次更新reqXML,然后我怎样才能实现所有10 xmls。请指导。

由于

1 个答案:

答案 0 :(得分:0)

<强>原因

当你做

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

在每次迭代时都会覆盖源字符流。所以只写了最后一篇。

您需要在循环中构建整个XML并在循环外调用source.setCharacterStream

<强>解决方案

鉴于每个XML位的格式为

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

这应该有效:

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

关于您的解析问题

我试过那段代码:

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();
}

它有效,按照预期打印出尼古拉斯和彼得。所以我认为您的XML格式不正确,您可以添加reqXML中的XML吗? XML中可能存在不需要的字符。请参阅此问题org.xml.sax.SAXParseException: Content is not allowed in prolog

的确,如果我在上面的示例中使用了下面的XML(在第一个标记之前添加了不需要的字符),我会得到与您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>";