Как конвертировать xml в java список строк - PullRequest
0 голосов
/ 25 февраля 2020

У меня есть строка xml в java, которую нужно разбить на более мелкие строки. Например, с учетом следующего:

<?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>"

Любые идеи приветствуются. Спасибо!

Ответы [ 2 ]

0 голосов
/ 25 февраля 2020

Вы можете сделать это, используя 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;
}
0 голосов
/ 25 февраля 2020

Вы можете проанализировать XML в DOM, выполнить итерации дочерних узлов элемента root (после добавления одного к 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>'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...