Parsing xml from a website Java [duplicate] - java

This question already has answers here:
parse an xml string in java?
(4 answers)
Closed 9 years ago.
How can I parse xml from website using Java ?
For example this one http://rates.fxcm.com/RatesXML
Anyone knows how is it done?

Here's the Oracle Java/XML tutorial. That will give you an overview of the most common XML APIs for Java, together with their pros/cons.

There are several XML libraries which you can use for this. jdom has always worked well for me.

You can use XML parser,there are so many resources on the web.
Additionally read those.
http://www.w3schools.com/xml/xml_parser.asp
http://www.perlmonks.org/?node_id=62782

I think simplest option is to use "xStream". All you need is to define a "Rates/Rate" POJO classes with appropriate member attributes and use something like below:
Rates rates = (Rates) xstream.fromXML(xml);
Use two minute tutorial for further reference.

My personal preference is dom4j. Powerful and easy to work with.

Your example is simple enough to parse manually using the DOM or SAX API, but I'd still suggest using an XML serialization API such as JAXB, XStream, or Simple instead because writing your own XML serialization/deserialization code is a drag.
Note that the XStream FAQ erroneously claims that you must use generated classes with JAXB:
How does XStream compare to JAXB (Java API for XML Binding)?
JAXB is a Java binding tool. It generates Java code from a schema and
you are able to transform from those classes into XML matching the
processed schema and back. Note, that you cannot use your own objects,
you have to use what is generated.
It seems this was true was true at one time, but JAXB 2.0 no longer requires you to use Java classes generated from a schema.
If you go this route, be sure to check out the side-by-side comparisons of the serialization/marshalling APIs I've mentioned:
http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-xstream.html
http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-simple.html

Related

Generate XSD files in Java

Is there any way (not from classes/JAXB) to create XSD schemas in Java? I can parse it with the help of some libraries ie XSOM, Jdom etc. But could not find anything to create.
I don't know about any easy to use way.
I considered using dom4j (because I use it for other purposes, but any other generic xml manipulation library is equivalent) and manualy create it (1).
But then I realized I can use JAXB to generate object model of xml schema, populate it with what i wanted (turned out to be much less comfortable than I hoped for) and marshall it(2). Its via JAXB, but without creating classes for your schema, so maybe it can be usefull to you.
via 2 is hard (but not impossible) to create invalid schema, but its sometimes hard to find out how to create schema I wanted. So I ended creating it in editor, then unmarshalling it and exploring its object representation. In the end, creating it via 1) and then validating it (which I had to do anyway) would be less chore.
Take a look at apache xerces
http://xerces.apache.org/xerces2-j/xml-schema.html
still there's no out of the box solution to handle xsd files
You could use the Eclipse XSD project, part of the Eclipse Model Development Tools. It provides a data model and API for programatically creating schemas.
To create XSD is to use java classes/coding (is inevitable):
Just use Java DOM and create a document then the main node and create other nodes to attach to it and voila!

multiple XML digestion in Java

I am looking for a easy to implement xml to java binding. The problem I am facing that there is more than one xml file, and I need to create one object tree from these files.
JAXB is not helping for two reason : the xmls are not usually have any schema, and second JAXB does not offer any solution for combining them.
I tried smooks too, but it also doesn't offer any multiple XML digestion system.
Does anybody have any idea?
You can do this in JAXB using an initialized XmlAdapter. Below is a link to answer I gave to a similar question:
Using JAXB to cross reference XmlIDs from two XML files
Note:
JAXB implementations (Metro, EclipseLink MOXy, Apache JaxMe) do not require an XML schema:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted
If you use JAXB, I suggest creating your own XSDs for the XML files your need to use. That'll help you document what you thought the schema was at the time, and help identify any future issues due to change at the source.
Then, create a class or classes that deserialize the individual documents into the JAXB-generated classes and then build the object you want from those objects.
Even if you don't use JAXB, I'd still recommend using this kind of pattern to isolate the transformation from XML to Java and keep the part of your app that knows about XML in one place, away from your business logic.
I've used XMLBeans before. Really easy and flexible to use, should be able to help you a great deal.

what is yaml file in java and use of it?

i saw some site like this http://jyaml.sourceforge.net/ for yaml in java.
but i can't to use of that.
how can i use form yaml files?
if is it possible to use it in javafx 2.0?
thanks.
What is YAML
You should see the Wikipedia page for YAML at least. The official YAML website defines it as
[...] a human friendly data serialization
standard for all programming languages.
Use with Java
It depends on what you want to use it for - the most common use (I'd imagine, since I haven't used it myself) would be for storing application configuration, as an alternative to XML or JSON. Essentially, you'll have a simple text file that contains data in a structured format as defined by the YAML spec. Here is an article that discusses the use of YAML with Java.
To avoid reinventing the wheel, you should make use of a library that performs the serialization and deserialization for you i.e. it can read from and write to the text file and parse the data in it and hand it over to your application in an easier to use object form. The business logic, of course, must be written by you. There are several Java libraries that are available and this question on SO talks about which one to use and why: https://stackoverflow.com/questions/450399/which-java-yaml-library-should-i-use.
Yaml is a file format*, and jYaml is a Java library for working with that file format.
So you may use it to read or write information into this format.
How can i use form yaml files?
You write one, and use it with this library.
If is it possible to use it in javafx 2.0?
Can you use this library in JavaFX 2.0? If you can then yes. :)
* See comment

Is there a declarative way to parse XML to Java objects?

I'm writing an import function of XML files to my Java application. I am using XOM to parse the XML files. The code for parsing the XML is not easy to understand, it is some hardcoded .getChild(3) and so on. It is hard to follow the code compared to the declarative XML document.
Isn't there a more maintainable way to parse XML documents to Java objects? I would like to have it in a more declarative way, where I can specify what tags corresponds to what Java classes.
Have a look at JAX/B - fairly simple annotation-based approach. It's a standard Java API.
There are tools to generate Annotated Java classes from XSDs or sample XML files. I describe my use of it in my blog
I really like Simple for converting XML to Java.
Have a look at Apache Commons Digester.
Agreed JAXB (JSR-222) is the best solution. Note that JAXB is a spec meaning you have a choice of implementations:
http://bdoughan.blogspot.com/2010/07/jaxb-xml-binding-standard.html
Standard JAXB allows you to specify the mappings by means of annotations, MOXy JAXB also allows you to specify your metadata via XML:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML
If you want a maintainable solution you need to break the one-to-one relationship between XML elements found in almost all XML binding solutions, and use XPath based mapping used in MOXy:
http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
parse google geocode with xstream
The Simple XML framework uses annotations on field and method declarations as well as on class definitions to map XML to Java and back. Its many times more lightweight than JAXB (which pulls in tonnes of dependencies). In fact it has no external dependencies at all. And its faster too. I tried JAXB many times, but found the annotations and functionality awkward and cumbersome. Check out the Tutorial.
Check Castor XML Mapping
Here is documentation for same : http://www.castor.org/xml-mapping.html
I finally found XStream that was easy to use and parses the XML in a declarative way.

Saving Java Object Graphs as XML file

What's the simplest-to-use techonlogy available to save an arbitrary Java object graph as an XML file (and to be able to rehydrate the objects later)?
The easiest way here is to serialize the object graph.
Java 1.4 has built in support for serialization as XML.
A solution I have used successfully is XStream (http://x-stream.github.io/)- it's a small library that will easily allow you to serialize and deserialize to and from XML.
The downside is you can only very limited define the resulting XML; which might not be neccessary in your case.
Apache digester is fairly easy: http://commons.apache.org/digester/
JAXB is newer and comes with annotation goodness: https://jaxb.dev.java.net
XStream by the folks at Thoughtworks has a simple API and even deals with things like duplicate and circular references. It seems to be actively developed and is well documented.
http://x-stream.github.io/
Use java.beans.XMLEncoder. Its API is very simple (actually a little too simple; it'd be nice to wire it to a SAX ContentHandler), but it works on many graphs out of the box, and it's easy to create your own persistence delegate for any odd-ball classes you might encounter.
The syntax used by XMLDecoder allows
you to invoke any method, instance
or static, including constructors,
so it's extremely flexible.
Other encoders name
elements and attributes after class
and field names, so there's no fixed schema for the result. The XMLEncoder's
XML follows a simple DTD and can
easily be validated or transformed,
even when you've never seen the
types it uses.
You can assign objects an
identifier, and reference them
throughout the graph.
You can refer to constants defined
in classes or interfaces.
And, it's built into Java SE, so you don't need to ship an extra library.
Simple
Although XStream and JAXB can serialize an some object graphs succssfully they can not handle very complex graphs. The most powerful solution for large complex graphs is Simple XML Serialization. It can handle any graph. Also, it’s fast and simple to use without any dependencies.
To quote the Simple project page:
Simple is a high performance XML serialization and configuration framework for Java. Its goal is to provide an XML framework that enables rapid development of XML configuration and communication systems. This framework aids the development of XML systems with minimal effort and reduced errors. It offers full object serialization and deserialization, maintaining each reference encountered. In essence it is similar to C# XML serialization for the Java platform, but offers additional features for interception and manipulation.
The Simple API is, well, simple! It's really good. http://simple.sourceforge.net/
You can also use XStream: http://www.ibm.com/developerworks/library/x-xstream/index.html
JAX-B is part of the standard APIs and really easy to use.
If you need control over the XML that gets generated, I recommend taking a look at Betwixt (http://commons.apache.org/betwixt/) - it adds a lot of functionality to Apache's digester (Digester is good for building object graphs from XML, but is not so good for generating them).
If you really don't care about the XML that gets generated (just that it can be deserialized in the future), then the XMLEncoder/Decoder classes built into Java or good - as long as the objects you are serializing follow the JavaBean specification. The biggest area I've run into problems with the XMLEncoder/Decoder solution is if you have a bean that returns an immutable list for one of it's properties - the encoder doesn't handle that situation very well.
If you need to control the structure of the XML, the XStream is a good choice. You can use annotations to define precisely the structure/mapping of the XML and your objects.
I'd second (or third) XStream. It reads and writes XML without needing any special binding configuration or placing lots of extraneous syntax in the XML.
I put together a list with a lot of xml serialization libraries and its license
XStream is very simple http://x-stream.github.io/
XStream is a simple library to serialize objects to XML and back again.
java.beans.XMLEncoder perhaps?
Jackson
The Jackson Project is a processing and binding library for XML, JSON, and some other formats.
… Jackson is a suite of data-processing tools for Java (and the JVM platform), including the flagship streaming JSON parser / generator library, matching data-binding library (POJOs to and from JSON) and additional data format modules to process data encoded in Avro, BSON, CBOR, CSV, Smile, (Java) Properties, Protobuf, XML or YAML; and even the large set of data format modules to support data types of widely used data types such as Guava, Joda, PCollections and many, many more…
If you are really only interested in serializing your objects to a file and then deserializing them later, then you might check out YAML instead of XML. YAML is much easier to work with than XML and the output files are very human-readable (which may or may not be a requirement). Check out yaml.org for more information. I've used JYAML successfully on a recent project.

Categories

Resources