An XML file is passed to the application as input, in which the relative position of items (Item) and boxes (Box) is specified. The boxes can be empty or contain items or other boxes. Items may not be in the box. How can I parse it into DB tables in Spring?
Asked
Active
Viewed 173 times
1 Answers
0
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"
}
}
}
}
}
and put these in your desired database!

Chhavi Jajoo
- 11
- 3
-
Thanks! id in my XML file is tag attribute – m_nesterenko Oct 12 '21 at 06:54