0%

  • Add, Delete, Update, Read
  • Using XPath

Add, Delete, Update, Read

XML sample

1
2
3
4
5
6
7
8
9
10
11
12
<shelf> 
<book name="Java Web">
<name>Java Web</name>
<author>Ryan</author>
<price>99.0</price>
</book>
<book name="Spring">
<name>Spring</name>
<author>AK</author>
<price>299.0</price>
</book>
</shelf>

Read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Test
public void read() throws DocumentException {

SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");

Element root = document.getRootElement();
Element book = root.elements("book").get(0);
String bookName = book.element("name").getText();
System.out.println(bookName);

String bookAttribute = book.attributeValue("name");
System.out.println(bookAttribute);
}
Read more »