如何解析XML与数据到DB在春天?



一个XML文件作为输入传递给应用程序,其中指定了项(Item)和框(Box)的相对位置。这些框可以是空的,也可以包含项目或其他框。不得在盒子里。如何在Spring中将其解析为DB表?

Assuming your XML looks something like this, 

```<boxes>
<box>
<items>
<item>
<id>1</id>
<name>test</name>
</item>
</items>
</box>
</boxes>

As a first step what we can do is to parse and convert it to JSON using org.json maven dependency,

```JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);```

you can find more details here, https://stackoverflow.com/questions/1823264/quickest-way-to-convert-xml-to-json-in-java

As third step you can iterate over the JSONArray of boxes and items which looks something like this,

```{
"boxes`enter code here`": {
"box": {
"items": {
"item": {
"id": 1,
"name": "test"
}
}
}
}
}

并将它们放入所需的数据库中!