繁体   English   中英

如何将xml转换为java字符串列表

[英]How to convert xml to java string list

我在java中有一个xml字符串,我需要分解成更小的字符串。 例如,给定以下内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <employee id="111">    
        <firstName>Lokesh</firstName>    
        <lastName>Gupta</lastName>     
        <location>India</location>   
    </employee> 
    <employee id="222">    
        <firstName>Alex</firstName>    
        <lastName>Gussin</lastName>    
        <location>Russia</location>    
    </employee> 
    <employee id="333">    
        <firstName>David</firstName>    
        <lastName>Feezor</lastName>    
        <location>USA</location>    
    </employee>

我如何在没有任何明显分隔符的情况下解析以获得:

string1 = "<employee id="111">    <firstName>Lokesh</firstName>    <lastName>Gupta</lastName>    <location>India</location>   </employee>"
string2 = "<employee id="222">    <firstName>Alex</firstName>    <lastName>Gussin</lastName>    <location>Russia</location>    </employee>"
string3 = "<employee id="333">    <firstName>David</firstName>    <lastName>Feezor</lastName>    <location>USA</location>    </employee>"

任何想法表示赞赏。 谢谢!

您可以将 XML 解析为 DOM,迭代根元素的子节点(一旦向 XML 添加一个),并将每个元素呈现为 XML。

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> \r\n" + 
             "<employee id=\"111\">    \r\n" + 
             "    <firstName>Lokesh</firstName>    \r\n" + 
             "    <lastName>Gupta</lastName>     \r\n" + 
             "    <location>India</location>   \r\n" + 
             "</employee> \r\n" + 
             "<employee id=\"222\">    \r\n" + 
             "    <firstName>Alex</firstName>    \r\n" + 
             "    <lastName>Gussin</lastName>    \r\n" + 
             "    <location>Russia</location>    \r\n" + 
             "</employee> \r\n" + 
             "<employee id=\"333\">    \r\n" + 
             "    <firstName>David</firstName>    \r\n" + 
             "    <lastName>Feezor</lastName>    \r\n" + 
             "    <location>USA</location>    \r\n" + 
             "</employee>";

// Add missing root element
xml = xml.replaceAll("^(<\\?xml.*?\\?>)?", "$1<X>") + "</X>";

// Prepare parser
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document document = domBuilder.parse(new InputSource(new StringReader(xml)));

// Prepare renderer
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

// Iterate top-level elements and render them to individual strings
List<String> list = new ArrayList<>();
for (Node node = document.getDocumentElement().getFirstChild(); node != null; node = node.getNextSibling()) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        StringWriter buf = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(buf));
        String elementXml = buf.toString();
        elementXml = elementXml.replaceAll("\\R", " ").trim(); // Eliminate line separators
        list.add(elementXml);
    }
}

// Print the result
for (String s : list)
    System.out.printf("'%s'%n", s);

输出

'<employee id="111">         <firstName>Lokesh</firstName>         <lastName>Gupta</lastName>          <location>India</location>    </employee>'
'<employee id="222">         <firstName>Alex</firstName>         <lastName>Gussin</lastName>         <location>Russia</location>     </employee>'
'<employee id="333">         <firstName>David</firstName>         <lastName>Feezor</lastName>         <location>USA</location>     </employee>'

您可以使用 StAX 来做到这一点:

private ArrayList <String> getEmployees(String input) throws XMLStreamException {
    ArrayList <String> employees = new ArrayList <>();

    XMLEventReader xmlEventReader = XMLInputFactory.newInstance().createXMLEventReader(new StringReader(input));
    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();

    XMLEventWriter xmlEventWriter = null;
    StringWriter sw = null;
    while (xmlEventReader.hasNext()) {
        XMLEvent xmlEvent = xmlEventReader.nextEvent();
        if(xmlEvent.isStartElement() && xmlEvent.asStartElement().getName().getLocalPart().equals("employee"))  {
            sw = new StringWriter();
            xmlEventWriter = xmlOutputFactory.createXMLEventWriter(sw);
        }

        if(xmlEventWriter != null) {
            if(xmlEvent.isCharacters() && xmlEvent.asCharacters().isWhiteSpace()) {
                continue;
            }

            xmlEventWriter.add(xmlEvent);
        }

        if(xmlEvent.isEndElement() && xmlEvent.asEndElement().getName().getLocalPart().equals("employee")) {
            xmlEventWriter.close();
            employees.add(sw.toString());
            xmlEventWriter = null;
            sw = null;
        }
    }

    return employees;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM