A convenient library for reading XML. Click here for a detailed overview (https://qiita.com/bocc/items/5d7213431e5a5e3a67e9)
It runs under Spring MVC, so it's not versatile at all. I don't know such a special environment! People please go here
For Spring users It seems that Spring IO manages dependencies, so let's use it immediately.
pom.xml
<!-- JDOM -->
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
</dependency>
As always, version control is Spring IO.
Assuming a web service (although it doesn't have to be) Let's read the XML.
This is the XML used for the sample
sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee>
<name>Yamamoto</name>
<class>Sales Section1</class>
<sex>male</sex>
<age>10</age>
</employee>
<employee>
<name>Hanako</name>
<class>Sales Section2</class>
<sex>female</sex>
<age>20</age>
</employee>
<employee>
<name>Taniguchi</name>
<class>Develop Section2</class>
<sex>male</sex>
<age>30</age>
</employee>
</employees>
Let's get this in ** src / main / resource **.
DomController.java
@Autowired
ResourceLoader resourceLoader;
@RequestMapping(value="/readable")
public String readable() {
//File acquisition
Resource resource = resourceLoader.getResource("classpath:sample.xml");
Document doc = null;
Element root = null;
//Get Xml
try {
doc = new SAXBuilder().build(resource.getFile());
root = doc.getRootElement();
} catch (JDOMException e) {
System.out.println(e);
} catch (IOException e) {
//TODO auto-generated catch block
System.out.println(e);
}
From now on, the Spring code will be mixed. If it is a normal sample, ** resource.getFile () ** will be ** new File ("hogehoge.xml") **
What you are doing
DomController.java
doc = new SAXBuilder().build(resource.getFile());
root = doc.getRootElement();
I'm just getting the root element here.
DomController.java
//Get child elements from Xml (not grandchild elements)
List<Element> list = root.getChildren();
This time there are 3 child elements, so it will be a List.
Try to get the value of name under employee. You can also get the value of the attribute by changing ** getValue () ** to ** getAttribute ("hoge_name") **. The number of loops increases as the number of grandchildren and great-grandchildren increases. (I think it's a technique for searching for "trees" from around here.)
DomController.java
List<String> result = new ArrayList<String>();
//Loop to get grandchild element from Xml
for(Element e : list) {
if(e.getName().compareTo("employee") == 0) {
result.add(e.getChild("name").getValue());
}
}
//Loop to spit out the retrieved element to the console
for(String name : result) {
System.out.println(name);
}
DomController.java
@RestController
public class DomController {
@Autowired
ResourceLoader resourceLoader;
@RequestMapping(value="/readable")
public String readable() {
//File acquisition
Resource resource = resourceLoader.getResource("classpath:sample.xml");
Document doc = null;
Element root = null;
//Get Xml
try {
doc = new SAXBuilder().build(resource.getFile());
root = doc.getRootElement();
} catch (JDOMException e) {
System.out.println(e);
} catch (IOException e) {
//TODO auto-generated catch block
System.out.println(e);
}
//Get child elements from Xml (not grandchild elements)
List<Element> list = root.getChildren();
List<String> result = new ArrayList<String>();
//Loop to get grandchild element from Xml
for(Element e : list) {
if(e.getName().compareTo("employee") == 0) {
result.add(e.getChild("name").getValue());
}
}
//Loop to spit out the retrieved element to the console
for(String name : result) {
System.out.println(name);
}
return null;
}
There is no problem with @Controller even if it is not @RestController.
Let's access it from a browser and check the operation.
Console screen
You can see that the value of name is obtained from XML.
Recommended Posts