1

I have a java object with 10 attributes. 3-4 of the attributes come from an XML file.

The XML has one element with 100-150 attributes in it. So i need to parse my entire XML file ( I have no XSD for this) and find out the 3-4 attributes and retrieve their value.

I need suggestions as to which technology to use and in particular, does JAXB work in my case? Any help is appreciated.

user892871
  • 1,025
  • 3
  • 13
  • 28
  • Is the adjective "large" relevant to the problem? If so, we need to know how large is large. For some people 2Mb is large, others mean 20Gb. You say the XML "has one element with 100-150 attributes". This is ambiguous. It could mean the XML contains only one element, and that element has 100-150 attributes. Or it could mean that the XML has many elements, one of which has 100-150 attributes. This distinction could make a big difference to the answer. – Michael Kay Mar 31 '12 at 09:38
  • Check [Can JAXB parse large XML files in chunks](http://stackoverflow.com/questions/1134189) and [JAXB FAQ: Dealing with large documents](http://jaxb.java.net/guide/Dealing_with_large_documents.html). – dma_k Mar 31 '12 at 13:20
  • @Michael : My file is 1 MB. The file is about a configuration of a single device. It contains description of a number protocols. And each protocol has 2-3 fields inside it. I am interested in values of 2 or 3 protocols. – user892871 Mar 31 '12 at 18:30

1 Answers1

2

Yes JAXB (JSR-222) works in this use case:

I have a java object with 10 attributes. 3-4 of the attributes come from an XML file.

When less than half of your object is mapped to XML with JAXB I recommend using @XmlAccessorType(XmlAccessType.NONE). This tells JAXB that only the explicitly annotated properties are mapped to XML.

The XML has one element with 100-150 attributes in it. So i need to parse my entire XML file ( I have no XSD for this) and find out the 3-4 attributes and retrieve their value.

The JAXB implementation will use a StAX or SAX parser to process the XML document. These are quite efficient parsers and do not require much memory.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thank you for your inputs. I am a novice and i dont understand why we need a parser to process the XML document? Cant the unmarshal method of Jax-B do this for me? – user892871 Mar 31 '12 at 23:19
  • @user892871 - All you need is JAXB. What I meant is that a JAXB (JSR-222) implementation will use a lower level parser to efficiently handle any size of XML document. – bdoughan Apr 01 '12 at 00:10