Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with data in xml easily and quickly by java

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how java can simply and quickly deal with the data in xml. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Is there any convenient class library for parsing XML in Java? For example, deal with the following XML:

Everyday Italian

Giada De Laurentiis

2005

eight hundred and fifty

30.00

...

Expect the results of the structured XML:

Java generally uses XPath to parse XML, but because it is not a collection language, it is longer to write, and uses a lot of interfaces, and API is also more complex. The code should be written like this:

...

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

Document document=new SAXReader () read (file)

Public void test () {

List nodes = document.selectNodes ("bookstore/book")

For (Node node:nodes) {

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

}

}

...

XML parsing, using the aggregator SPL will be much easier, it encapsulates the XPath, and has a rich and powerful set operation ability, so it is easy to deal with. For example, the above question requires only 2 lines:

A

one

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

two

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

XML is generally parsed for calculation, so SPL can also be calculated more easily, such as counting annual book sales; just add one line to this: = A2.groups (year;sum (quantity*price): amount)

In fact, there are many situations where parsing XML with Java is not convenient, but using the aggregator SPL is very simple.

The aggregator is also easily embedded in Java applications, and there are ways to use and get it how Java invokes the SPL script.

On how java simple and fast processing of data in xml is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report