How to read XML generated from JAXB with C - java

I created Java object and I used JAXB to convert that object into XML. Now the problem is how I can read this XML file in C? Is there any standard way or I have to use external libraries like libxml?

Libxml2 is the "standard" way (inasmuch as there is a standard) to handle XML in C/C++. At least it has the most mindshare and best documentation and community support, AFAICT.
Unless, of course, you want to write your own XML parser, which is not recommended :-)

You can use Xerces-C APIs to read the XML. Xerces apis exists for both C/C++ and Java. Refer the project website http://xerces.apache.org/

Related

Java built-in XML

Does Java have a built in XML library for generating and parsing documents? If not, which third party one should I be using?
The Sun Java Runtime comes with the Xerces and Xalan implementations that provide the ability to parse XML (via the DOM and SAX intefaces), and also perform XSL transformations and execute XPath queries.
However, it is better to use the JAXP API to work on XML, since JAXP allows you to not worry about the underlying implementation used (Xerces or Crimson or any other). When you use JAXP, at runtime the JRE will use the service provider it can locate, to perform the needed operations. As indicated previously, Xerces/Xalan will be used since it is shipped with the Sun JRE (not others though), so you dont have to download and install a specific provider (say, a different version of Xerces, or Crimson).
A basic JAXP tutorial can be found in The J2EE 1.4 tutorial (Its from the J2EE tutorial, but it will help).
Do note that the Xerces/Xalan implementations provided by the Sun JRE, will not be found in the org.apache.xerces.* or org.apache.xalan.* packages. Instead, they will be present in the internal com.sun.org.apache.xerces.* and com.sun.org.apache.xalan.* packages.
By the way, JDOM is not an XML parser - it will use the parser provided to it by JAXP in order to provide you with an easier abstraction to work with.
Yes. It has a two options in the javax.xml package: DOM builds documents in memory, and SAX is an event-based approach.
You may also want to look at JDOM, which is a 3rd party library that offers a combination of the two, and can be easier to use.
Yes. Java contains javax.xml library. You can checkout some samples at Sun's Java API for XML Code Samples.
However, I personally like using JDOM library.
javax.xml package contains Java's native XML solution which is actually a special version of Xerces. You can do what you asked with it, however using 3rd party libraries such as JDOM makes the whole process a lot easier.
Have a look at JAX-B This is increasingly the "standard" way to do XML processing. Uses Java annotations to simplify the programming model. The reference gives sample code for reading and writing XML.
Java does come with a large set of packages and classes to handle XML. These are part of the Standard Edition JDK, and located under the javax.xml package.
Aside from reading XML and writing it with DOM or SAX, these packages also perform XSL transformations, JAX-B object marshalling and unmarshalling, XPath processing and web services SOAP handling. I advise you to read more about these online in Sun's excellent tutorials.
I can't tell you which one to use (few requirements specified, and there
are a dozen libraries), but I would seriously consider XOM (here).
Written by Eliotte Rusty Harold, it is quite complete in terms of the XML
spec, and generally excellent. I have found it very easy to use. See the
link above for Harold's motivation and criticism of other solutions.
You could have a look to the javax.xml package, which contains everything you need to work with XML documents in Java...
Java API for XML Processing (JAXP) is part of standard library JavaSE. JAXP allows you to code against standard interface and lets you pick the parser implementation later if needed.
The Java API for XML Processing, or
JAXP for short, enables applications
to parse and transform XML documents
using an API that is independent of a
particular XML processor
implementation. JAXP also provides a
pluggability feature which enables
applications to easily switch between
particular XML processor
implementations.
You can use StAX (streaming API for XML)
http://en.wikipedia.org/wiki/StAX
http://www.xml.com/pub/a/2003/09/17/stax.html
https://sjsxp.dev.java.net/
StAx is optimized to process large xml files, without causing OOM (out of memory) problem :)
As is said above... Java's SDK now comes with Xerces and Xalan. Xalan only implements version 1.0 of the XSLT API, so if you want 2.0, you should look at Saxon from Michael Kay.

Should I still be using JDOM with Java 5 or 6?

I've been using JDOM for general XML parsing for a long time, but get the feeling that there must be something better, or at least more lightweight, for Java 5 or 6.
There's nothing wrong with the JDOM API, but I don't like having to include Xerces with my deployments. Is there a more lightweight alternative, if all I want to do is read in an XML file or write one out?
The best lightweight alternative is, in my opinion, XOM, but JDOM is still a very good API, and I see no reason to replace it.
It doesn't have a dependency on Xerces, though (at least, it doesn't need the Apache Xerces distro, it works alongside the Xerces that's packaged into the JRE).
I've used the javax.xml.stream package (XMLStreamReader/XMLStreamWriter) to read and write XML using xml pull/push techniques. It's worked for me so far.
We use JAXB - it generates the classes based on the schema. You can generate your files without a schema, and just annotate how you want the xml to be.
There was recently a fork of JDOM for java 5 called coffeeDOM. You should check it out.
You should check out Commons Digester (see the answer I've given here). It provides a very lightweight mechanism for parsing XML.
JDOM is very good and simple. There has been many new ways to parse XML after release of JDOM, but those has have different focus than simplicity. JAXB makes things simple in some cases when you have well known XML document has your schema does not get updated daily basis.
New push parsers are very good and even mandatory for very large XML files (hundreds of MBs).
Speed benefit for SAX parser can be ten fold.
Use one of the XML APIs that come standard with Java, so that you don't have to include any third-party libraries.
XML in the Java Platform Standard Edition (Java SE) 6
I would like to think JAXP is a good choise for you.
It's standard, included in JDK, it provides clear interface and allows to hook up any implementations..
If all what you need in is to read and write not very large and overcomplicated xml files, JAXP DOM api embedded in JDK will cover you requirements.

XML to Java Object only using Java5 (no external libs)

normally I would use JaxB, XMLBeans or Simple to convert a XML file to a Java Object.
In this case I can however only use Java5 and no external libraries (for several reasons).
What is the best way to do that? My XML input is very simple. What is the most flexible and elegant way to get the XML into a Java-Object (I don't really need real JavaBeans, since I just need GETTER).
Thanks!
Well, you can do that using DOM implementation.
Java5 provides JaxP which includes DOM and SAX.
Which one to use depends largely on how big the XML document is and how fast you need to access elements. DOM will put the whole XML structure into memory, while SAX provides a serial streaming approach.
The most flexible way to do data binding is by using XPath see the article below
http://onjava.com/pub/a/onjava/2007/09/07/schema-less-java-xml-data-binding-with-vtd-xml.html

Using a DiffGram in Java

I'm trying to build a web portal in Java that supports incremental changes to an XML document. I really like the diffgram technology in .Net, but I must use Java for this project. Is there a Java library that can modify an XML document object using diffgrams? We will likely be using JAXB, but I can always marshal data to build the original XML document.
I don't know anything like this in Java, at least, not based on diffgram. Maybe have a look at Open Source XML Diff Written in Java for xml diff solutions in Java. Or Google for more alternatives.
Actually, this project looks interesting: fc-xmldiff (Fuego Core XML Diff and Patch Tool). Never tested personally though.

How to read an XML file using Java?

I need to read an XML file using Java. Its contents are something like
<ReadingFile>
<csvFile>
<fileName>C:/Input.csv</fileName>
<delimiter>COMMA</delimiter>
<tableFieldNamesList>COMPANYNAME|PRODUCTNAME|PRICE</tableFieldNamesList>
<fieldProcessorDescriptorSize>20|20|20</fieldProcessorDescriptorSize>
<fieldName>company_name|product_name|price</fieldName>
</csvFile>
</ReadingFile>
Is there any special reader/JARs or should we read using FileInputStream?
Check out Java's JAXP APIs which come as standard. You can read the XML in from the file into a DOM (object model), or as SAX - a series of events (your code will receive an event for each start-of-element, end-of-element etc.). For both DOM and SAX, I would look at an API tutorial to get started.
Alternatively, you may find JDOM easier/more intuitive to use.
Another suggestion: Try out Commons digester. This allows you to develop parsing code very quickly using a rule-based approach. There's a tutorial here and the library is available here
I also agree with Brian and Alzoid in that JAXB is great to get you up and running quickly. You can use the xjc binding compiler that ships with the JDK to auto generate your Java classes given an XML schema.
xstream would do very nicely here. Check out the one page tutorial
You can user external libraries like
Castor https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-1046622.html
I have used castor in past. Here are few other links that might help.
http://www.xml-training-guide.com/e-xml27.html
http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/XMLReader.html
http://www.cafeconleche.org/books/xmljava/chapters/ch07.html
There are two major ways to parse XML with Java. The first is to use a SAX parser see here
which is fairly simple.
The second option is to use a DOM parser see here
which is more complicated but gives you more control.
JAXB is another technology that might suit your needs.

Categories

Resources