我有这个XML:
<XmlParent>
<name>koraytugay</name>
<bar>
<baz>
<to>Tove</to>
<qux>00000001</qux>
</baz>
</bar>
<note>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</XmlParent>
我想搜索“至”和“从”并提取数据 XML格式并不总是相同的,但是始终会出现“ from”和“ to”(要查找的实际节点将在属性文件中配置)
我打算执行此操作的方法是将其转换为json文件,因此我希望它看起来像这样
{
to:Tove
qux:00000001
from:Jani
heading:Reminder
body:Don't forget me this weekend!
}
目前,我正在这样
XmlParent:
{
name:koraytugay
bar:
{
baz:
{
to:Tove
qux:00000001
}
}
note:
{
from:Jani
heading:Reminder
body:Don't forget me this weekend!
}
}
}
我尝试了多种不同的方法。 我有一个看起来像这样的XSD(已从模式标签中删除了实际数据,但是工作正常)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns=""
targetNamespace=""
elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:annotation>
<xs:documentation xml:lang="en">
Initial Draft
</xs:documentation>
</xs:annotation>
<xs:element name="XMLExample">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
这是我目前正在做的
private static final Log LOGGER = LogFactory.getLog(XmlConverter.class);
public Map<String, Object> xmlStringToHashMap(String xml) {
JSON json = xmlStringToJSON(xml);
return jsonToMap(json.toString(2));
}
public Map<String, Object> jsonToMap(String jsonString) {
ObjectMapper mapper = new ObjectMapper();
try {
Map<String, Object> jsonInMap = mapper.readValue(jsonString, new TypeReference<Map<String, Object>>() {
});
LOGGER.info("JSON Map created: " + jsonInMap);
return jsonInMap;
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
return null;
}
public JSON xmlStringToJSON(String xml) {
XMLSerializer xmlSerializer = new XMLSerializer();
return xmlSerializer.read(xml);
}
有什么主意如何将元素及其数据添加到一个JSON文件或HashMap中,而忽略父标签?
答案 0 :(得分:1)
我设法使用XPath表达式//to/text()
这是我的解决方法:
private String[] elements;
public Map<String, Object> xPathParser(String xmlString) {
try {
Document xmlDoc = xmlStringToDocument(xmlString);
XPath xpath = XPathFactory.newInstance().newXPath();
String expression;
Map<String, Object> elements = new HashMap<>();
for (String element : elements) {
expression = MessageFormat.format("//{0}/text()", element);
Object xpathValue = xpath.compile(expression).evaluate(xmlDoc, XPathConstants.STRING);
elements.put(element, xpathValue);
}
return elements;
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
LOGGER.error(e.toString());
return null;
}
}
答案 1 :(得分:0)
您可能想看看jsoup,这是一个用于解析HTML或XML的Java库。当我想完成工作而不必编写大量代码时,经常使用它。对于您的示例:
import java.util.HashMap;
import java.util.Map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
public class NewClass3 {
public static void main(String[] args) {
String xml = "<XmlParent>\n" +
" <name>koraytugay</name>\n" +
" <bar>\n" +
" <baz>\n" +
" <to>Tove</to>\n" +
" <qux>00000001</qux>\n" +
" </baz>\n" +
" </bar>\n" +
" <note>\n" +
" <from>Jani</from>\n" +
" <heading>Reminder</heading>\n" +
" <body>Don't forget me this weekend!</body>\n" +
" </note>\n" +
"</XmlParent>";
Document doc = Jsoup.parse(xml, "", Parser.xmlParser());
Element to = doc.selectFirst("to");
Element from = doc.selectFirst("from");
Element qux = doc.selectFirst("qux");
Element heading = doc.selectFirst("heading");
Element body = doc.selectFirst("body");
//print put in map or whatever...
System.out.println("to: " + to.text());
System.out.println("from: " + qux.text());
Map<String,String> map = new HashMap<>();
map.put("to", to.text());
}
}