How to parse XML easily and quickly in Java

 

Does java have any class libraries for parsing XML?  For example, process the following XML:

<?xml version="1.0" encoding="utf-8" ?>

<bookstore>

 <book category="COOKING">

   <title lang="en">Everyday Italian</title>

   <author>Giada De Laurentiis</author>

   <year>2005</year>

   <quantity>850</quantity>

   <price>30.00</price>

 </book>

...

</bookstore>

Expected XML structured effect:

Java generally uses XPath to parse XML. Because it is not a set language, it will be longer to write, and it uses a lot of interfaces, and the API is more complex. The code should be written as follows:

...

File file=new File("/workspace/bookstore.xml");

Document document=new SAXReader().read(file);

public void test(){

List<Node> nodes = document.selectNodes("bookstore/book");

for(Node node:nodes){

                 System.out.println(node.getText());

        }

}

...

XML parsing, with esProc SPL will be a lot easier, it encapsulates the XPath, and has rich and powerful set computing capabilities, so it is easy to deal with. For example, the above question only needs two lines:

 

A

1

=xml@s(file("/workspace/book.xml").read()).bookstore

2

=A1.new(category,book(1).title,book(1).lang,book(2).author,book(3).year,book(4).quantity,book(5).price)

XML parsing is generally used for calculation, so SPL can be used to calculate more easily, such as: Statistics of annual book sales; only one line needs to be added on this basis:

=A2.groups(year;sum(quantity*price):amount)

In fact, there are many cases where it is not convenient to parse XML in Java, but it is very simple to use esProc SPL. Please refer to XML data parsing and calculation

It's also easy to embed esProc into Java applicationsplease refer to How to Call an SPL Script in Java

For the installation, usage, free authorization and related technical information of esProc, please refer to Getting started with esProc