[英]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.