如何将 XML 转换为忽略父子关系的哈希映射
How to convert XML to hash map ignoring parent-child relationships
我有这个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>
我想搜索 "to" 和 "from" 并提取该数据
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 中吗?
您可能需要查看 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());
}
}
我设法使用 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;
}
}
我有这个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>
我想搜索 "to" 和 "from" 并提取该数据 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 中吗?
您可能需要查看 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());
}
}
我设法使用 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;
}
}