一种将任何XML文件转换为灵活Java对象的方法



我知道要将XML文件转换为对象,我们需要首先创建对象,然后将XML文件转化为这个固定对象。有没有可能有一种方法将任何XML文件转换为以前没有固定和定义的Java对象?或者,是否有可能首先读取并获取XML文件中的标头和节点,然后自动写入对象?那么,我可以自动从任何文件创建对象吗?

稍后我想用对象来分析内容。

想象一下,我们正在讨论一种可以将以下XML数据转换为Java对象的方法:

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>
Belgian waffles covered with assorted fresh berries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>
Thick slices made from our homemade sourdough bread
</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>
Two eggs, bacon or sausage, toast, and our ever-popular hash browns
</description>
<calories>950</calories>
</food>
</breakfast_menu>
<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<item_number>QWZ5671</item_number>
<price>39.95</price>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
</catalog_item>
<catalog_item gender="Women's">
<item_number>RRX9856</item_number>
<price>42.50</price>
<size description="Small">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Large">
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Extra Large">
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
</catalog_item>
</product>
</catalog>

您可以使用Jackson库Jackson数据格式xml。

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.13.1</version>
</dependency>

示例:

public static void main(String[] args) throws JsonProcessingException {
XmlMapper objectMapper = new XmlMapper();
Object readValue = objectMapper.readValue("<A><id>1</id><name>Jeff</name> <id>2</id><name>John</name> </A>",
Object.class);
LinkedHashMap x = (LinkedHashMap) readValue;
x.entrySet().stream()
.forEach(x1 -> System.out.println(ReflectionToStringBuilder.toString(x1, ToStringStyle.JSON_STYLE)));
}

结果:

{"后":"name=[Jeff,John]"在":null;hash":3355;键":"id"下一个":null;值":["1","2"]}{"after":null、"before":"id=[1,2] "hash":3373752;键":"name"下一个":null;值":["Jeff","John"]}

如果您想保存或显示解析结果,我建议如下。

public class Parsing {
private static String XML_PATH = "";
public static void main(String[] args) {
List<Xml> list = new ArrayList<Xml>();
try {
// read
File xml = new File(XML_PATH);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document document = dBuilder.parse(xml);
document.getDocumentElement().normalize();
// parse
parse(list, document.getDocumentElement(), "/");
} catch (Exception e) {
e.printStackTrace();
}
for (Xml xml : list) {
System.out.println("nodeName: " + xml.getNodeName());
System.out.println("xPath: " + xml.getxPath());
if (xml.getContents() != null) {
System.out.println("contents: " + xml.getContents());
}
System.out.println();
}
}
private static void parse(List<Xml> list, Node node, String xpath) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
list.add(new Xml(node.getNodeName(), xpath, null));
NodeList childNode = node.getChildNodes();
for (int i = 0; i < childNode.getLength(); i++) {
Node n = childNode.item(i);
parse(list, n, xpath + node.getNodeName() + "/");
}
} else if (node.getNodeType() == Node.TEXT_NODE && !node.getTextContent().trim().isEmpty()) {
list.add(new Xml(node.getParentNode().getNodeName(), xpath, node.getTextContent().trim()));
}
}
}
class Xml {
private String nodeName;
private String xPath;
private String contents;
public Xml(String nodeName, String xPath, String contents) {
this.nodeName = nodeName;
this.xPath = xPath;
this.contents = contents;
}
public String getNodeName() {
return nodeName;
}
public String getxPath() {
return xPath;
}
public String getContents() {
return contents;
}
}

最新更新