开发者

Yet another Java XML library question

开发者 https://www.devze.com 2022-12-20 09:33 出处:网络
I never liked XML, and always tried to avoid it. And the day finally has come. When I tried to parse XML it was really hard. I used DOM parser, when I called getChildNodes() of a 开发者_开发知识库Nod

I never liked XML, and always tried to avoid it. And the day finally has come.

When I tried to parse XML it was really hard. I used DOM parser, when I called getChildNodes() of a 开发者_开发知识库Node, it returned NodeList. I had to use casting while using XPath.

Is there any Java XML parsing library that is similar to WebDriver's WebElement mechanism, where getChildNodes() returns List<Node> (or Collection, Iterable etc.), I don't need to do casting.In other words, is there a XML parser library that is elegant and simple.

The library can be read only, I don't need manipulation.


I would investigate JDOM as a much more usable API.

e.g. Element.getChildren() returns a list of Element objects (unfortunately it's not been genericized but the API doc is clear).

dom4j is another alternative (again, not genericised).


It really depends on what you're using the XML for. For example, on a project I was just on we wrote the backend functionality for an application. We sent/received messages via web services and SOAP. When I set this up I used Spring-WS and JAXB annotations. Because of this it automatically transformed the SOAP XML into Java objects coming to/from the backend. This meant that while the rest of the team knew conceptually what was going on they were insolated from the actual SOAP request unless they wanted to view them for debugging purposes and such. None of our code dealt with XML directly, only with the objects that were generated via JAXB.

Make sense?


I would also use JAXB. But if you have a schema associated with your XML (or can create one), you can use the jaxb compiler xjc to build Java classes to represent your XML structure. It's then easy to parse and navigate the XML. This is an alternative to the above approach because you don't have any annotations in your code.


As mentioned previously, it really depends on what you intend to do.

If you want to map your XML schema with Java beans, and automatically parse your XML file into instances of these beans, you could use JAXB, XMLBeans.

If you just want to parse simple XML files, and work on them on the fly, you could use JDOM, an alternative to DOM and SAX.

It gives you the ability to retrieve a List of element:

Iterator itr = (currentElement.getChildren()).iterator();
   while(itr.hasNext()) {
     Element oneLevelDeep = (Element)itr.next();
     List twoLevelsDeep = oneLevelDeep.getChildren();
     // Do something with these children
   }


You may find the JDOM API more intuitive and simpler to use than the JDK embedded DOM API. Unfortunately, the API is not using generics for the returned collections/lists, so you will still have to do some casting on the return values.


dom4j started using generics, and Collection with version 2.0.

The documentation is not very good for 2.0 but the old documentation serves well.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号