sentence
stringlengths 1
1.38k
| label
stringclasses 3
values |
---|---|
SAX Parser: Solely to read a XML document.
|
o
|
The Sax parser runs through the document and calls callback methods of the user.
|
o
|
There are methods for start/end of a document, element and so on.
|
o
|
They're defined in org.xml.sax.ContentHandler and there's an empty helper class DefaultHandler.
|
o
|
CODESNIPPET_JAVA2 .
|
o
|
StAx Reader/Writer: This works with a datastream oriented interface.
|
p
|
The program asks for the next element when it's ready just like a cursor/iterator.
|
o
|
You can also create documents with it.
|
o
|
Read document: CODESNIPPET_JAVA3 .
|
o
|
Write document: CODESNIPPET_JAVA4 .
|
o
|
JAXB: The newest implementation to read XML documents: Is part of Java 6 in v2.
|
o
|
This allows us to serialize java objects from a document.
|
o
|
You read the document with a class that implements a interface to javax.xml.bind.Unmarshaller (you get a class for this from JAXBContext.newInstance).
|
o
|
The context has to be initialized with the used classes, but you just have to specify the root classes and don't have to worry about static referenced classes.You use annotations to specify which classes should be elements (@XmlRootElement) and which fields are elements(@XmlElement) or attributes (@XmlAttribute, what a surprise!
|
n
|
) CODESNIPPET_JAVA5 .
|
o
|
Write document: CODESNIPPET_JAVA6 .
|
o
|
Examples shamelessly copied from some old lecture slides ;-) Edit: About "which API shoild I use?".
|
o
|
Well it depends - not all APIs have the same capabilities as you see, but if you have control over the classes you use to map the XML document JAXB is my personal favorite, really elegant and simple solution (though I haven't used it for really large documents, it could get a bit complex).
|
p
|
SAX is pretty easy to use too and just stay away from DOM if you don't have a really good reason to use it - old, clunky API in my opinion.
|
p
|
I don't think there are any modern 3rd party libraries that feature anything especially useful that's missing from the stl and the standard libraries have the usual advantages of being extremely well tested, documented and stable.
|
p
|
I want to note one thing: old doesn't mean bad.
|
n
|
2005 is not Jurassic period and I think you shouldn't dismiss a library (in Java, at least) because it hasn't been updated for 5 years.
|
o
|
In fact, the last major update of Java we had approximately at the same time.
|
o
|
And when was XML standard finalized?
|
o
|
I bet more than 10 years ago.
|
o
|
I'm not saying it is bad.. but there may be other libraries which are worth noticing :).
|
n
|
+1 - Just because a standard is 5 years old doesn't mean implementations of that standard don't continue to release new versions which are faster, more robust, and contain extensions.
|
p
|
I would be more concerned about using a proprietary library that hasn't released so much as a patch in a couple of years.
|
o
|
Nikita's point is an excellent one: don't confuse mature with bad.
|
p
|
XML hasn't changed much.
|
o
|
JDOM would be another alternative to DOM4J.
|
o
|
Which one will you choose and why?
|
o
|
It doesn't really much matter.
|
o
|
Both are wrappers of the SAX and DOM parsers built into the JDK.
|
o
|
The W3C Document hierarchy is verbose and hard to use, so both DOM4J and JDOM try to make it easier.
|
n
|
I like Elliott Rusty Harold, so I tend to reach for JDOM first.
|
n
|
Java supports two methods for XML parsing out of the box.
|
o
|
You can use this parser if you want to parse large XML files and/or don't want to use a lot of memory.
|
n
|
URL_http://download.oracle.com/javase/6/docs/api/javax/xml/parsers/SAXParserFa ctory.html [ URL_http://download.oracle.com/javase/6/docs/api/javax/xml/parsers/SAX ParserFactory.html] Example: URL_http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax- parser/ [ URL_http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/ ]
You can use this parser if you need to do XPath queries or need to have the complete DOM available.
|
o
|
URL_http://download.oracle.com/javase/6/docs/api/javax/xml/parsers/DocumentBui lderFactory.html [ URL_http://download.oracle.com/javase/6/docs/api/javax/xml/parse rs/DocumentBuilderFactory.html] Example: URL_http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom- parser/ [ URL_http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/ ] .
|
o
|
For folks interested in using JDOM, but afraid that hasn't been updated in a while (especially not leveraging Java generics), there is a fork called CoffeeDOM which exactly addresses these aspects and modernizes the JDOM API, read more here: URL_http://cdmckay.org/blog/2011/05/20/introducing-coffeedom-a-jdom-fork-for- java-5/ [ URL_http://cdmckay.org/blog/2011/05/20/introducing-coffeedom-a-jdom-fork - for-java-5/] and download it from the project page at: URL_http://coffeedom.googlecode.com/ [ URL_http://coffeedom.googlecode.com/ ] .
|
o
|
Sax parser is a good one, it is simple with their event-driven API, Have a look at: URL_http://www.asjava.com/java-xml/how-to-read-xml-file-via-sax- parser-in-java/ [ URL_http://www.asjava.com/java-xml/how-to-read-xml-file-via-sax - parser-in-java/] .
|
p
|
You don't need an external library for parsing XML in Java.
|
o
|
Java has come with built-in implementations for SAX and DOM for ages.
|
o
|
Look at vtd-XML, DOM and SAX are somewhat ancient APIs that has not been updated/improved for a while.
|
o
|
If you want a DOM-like API - that is, one where the XML parser turns the document into a tree of Element and Attribute nodes - then there are at least four to choose from: DOM itself, JDOM, DOM4J, and XOM.
|
o
|
The only possible reason to use DOM is because it's perceived as a standard and is supplied in the JDK: in all other respects, the others are all superior.
|
p
|
My own preference, for its combination of simplicity, power, and performance, is XOM.
|
o
|
And of course, there are other styles of processing: low-level parser interfaces (SAX and StAX), data-object binding interfaces (JAXB), and high- level declarative languages (XSLT, XQuery, XPath).
|
n
|
Which is best for you depends on your project requirements and your personal taste.
|
o
|
DOM is a W3C standard ( URL_http://www.w3.org/DOM/) .
|
p
|
The Java implementation of this standard is covered by the JAXP standard ( URL_http://jcp.org/en/jsr/detail?id=206) .
|
p
|
JAXP is then implemented by different providers such as: Oracle, Apache, etc.
|
o
|
XML serialization in Java?
|
o
|
Is there a (preferably free) Java analogue of .NET's XML serialization?
|
o
|
URL_http://xstream.codehaus.org/ [XStream] is pretty good at serializing object to XML without much configuration and money!
|
p
|
(it's under BSD license).
|
o
|
We used it in one of our project to replace the plain old java-serialization and it worked almost out of the box.
|
o
|
Very useful, it can have problems on complecated tree structures such as JGraph with non string node objects though.
|
o
|
Simpler and better then other solutions.
|
o
|
I like XStream.
|
o
|
Only thing is that I don't understand why a character is added before the actual XML.
|
o
|
The "Official" Java API for this is now JAXB - Java API for XML Binding.
|
o
|
The reference implementation lives at URL_http://jaxb.java.net/ [ URL_http://jaxb.java.net/ ] .
|
o
|
That's right, JAXB is definitely the best option!.
|
p
|
You may want to look at URL_http://simple.sourceforge.net/ [Simple] , its the closest thing I've found to the System.Xml.Serialization in .Net.
|
p
|
It does however require mapping annotations for each field.
|
o
|
Not true, I doesn't require.
|
o
|
You can change default behaviour and it will use only present fields.
|
o
|
JAXB is part of JDK standard edition version 1.6+.
|
p
|
So it is CODETERM1 and no extra libraries to download and manage.A simple example can be found URL_ht tp://www.java2s.com/Code/Java/JDK-6/MarshalJavaobjecttoxmlandoutputtoconsole.h tm [here] XStream seems to be dead.
|
p
|
Last update was on Dec 6 2008.
|
o
|
CODETERM2 seems as easy and simpler as JAXB but I could not find any licensing information to evaluate it for enterprise use.
|
p
|
XStream is not dead, it is just mature and stable -- meaning there isn't much to add to core functionality.
|
o
|
Same is actually true for JAXB reference implementation, not much activity for past couple of years.
|
o
|
URL_http://xmlbeans.apache.org/ [XMLBeans] works great if you have a schema for your XML.
|
p
|
It creates Java objects for the schema and creates easy to use parse methods.
|
p
|
Worth mentioning that since version 1.4, Java had the classes java.beans.XMLEncoder and java.beans.XMLDecoder.
|
p
|
These classes perform XML encoding which is at least very comparable to XML Serialization and in some circumstances might do the trick for you.
|
o
|
If your class sticks to the JavaBeans specification for its getters and setters, this method is straightforward to use and you don't need a schema.
|
p
|
With the following caveats:
As with normal Java serialization
coding and decoding run over a InputStream and OutputStream
the process uses the familar writeObject and readObject methods
In contrast to normal Java serialization
the encoding but also decoding causes constructors and initializers to be invoked
encoding and decoding work regardless if your class implements Serializable or not
transient modifiers are not taken into account
works only for public classes, that have public constructors For example, take the following declaration: CODESNIPPET_JAVA1 .
|
p
|
Executing this code: CODESNIPPET_JAVA2 .
|
o
|
Would result in the following file: CODESNIPPET_JAVA3 .
|
o
|
Usually I use URL_http://jaxb.java.net/ [jaxb] or URL_http://xmlbeans.apache.org/ [XMLBeans] if I need to create objects serializable to XML.
|
o
|
Now, I can see that URL_http://xstream.codehaus.org/ [XStream] might be very useful as it's nonintrusive and has really simple api.
|
p
|
I'll play with it soon and probably use it.
|
o
|
The only drawback I noticed is that I can't create object's id on my own for cross referencing.
|
o
|
@Barak Schiller Thanks for posting link to XStream!
|
o
|
Problem is jaxb and xmlbeans require a mapping schema and arent auto ...
|
o
|
Don't forget URL_http://jibx.sourceforge.net/ [JiBX] .
|
o
|
if you want a
solution** (like ORM) then JAXB2 is a good solution.
|
p
|
If you want a serialization like DOT NET then you could use URL_http://java.sun.com/products/jfc/tsc/articles/persistence3/ [Long-Term- Persistence-of-JavaBeans-Components] The choice depends on use of serialization.
|
o
|
If you're talking about automatic XML serialization of objects, check out URL_http://www.castor.org/ [Castor] :
Castor is an Open Source data binding framework for Java[tm].
|
p
|
It's the shortest path between Java objects, XML documents and relational tables.
|
o
|
Castor provides Java-to-XML binding, Java-to-SQL persistence, and more.
|
o
|
Rhino print function.
|
o
|
I'm using Rhino 1.7R4 and env.js 1.2 to run Javascript code inside Java I want to print from my Javascript code a string to the Java console.
|
o
|
According to: URL_http://evilroundabout.blogspot.com.au/2009/11/javascript- printing-rhino.html [ URL_http://evilroundabout.blogspot.com.au/2009/11/javascript - printing-rhino.html] I should use: print("Hello world"); but when I do I get: CODESNIPPET_JAVA1 .
|
o
|
If I use document.write I don't see any output.
|
o
|
Bring your javascript and java code.
|
o
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.