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
Related
I need to create some XML programmatically, sometimes with programming calls, sometimes starting by reading in some XML (a string) and then revising it with calls. And then write the XML out to a stream.
The writing out I assume is best done with codehaus stax (if there's a newer way, please tell me what). But for the rest, at present I am using dom4j and that is old and abandoned. So I need to upgrade to something that is supported and has no security issues.
Is there something in the Java runtime? Or is there a commonly used library?
We are on Java 1.8 so a solution cannot require 1.9 or later.
You have a lot of options here.
Parsers
Parsers take the whole document (SAX parsed is exception here) and create a tree structure from some common classes like Document, Node etc.. You can load any XML and work with it. No need to have some class that would represent the file.
Pros
you can load any file
you have the power of all the standard XML tooling, like XSLT transformations, xpath, etc..
Cons
hard to create new file programaticaly
harder to extract info from it
Tools
DOM Parsers
SAX Parser
Object serialization
Second approach you can take is by Object parsing. You basically have a class that represents you data structure and you serialize it to/from XML.
Pros
super easy to use
fast to just read/write data from existing data structures
Cons
not easy to use XSLT, etc..
cannot really read random XML files
Tools
JAXB
XStream
Jackson
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
I need a Java library that can handle some sort of file configuration. I need to easily be able to store both arrays and strings to it, using the simplest code possible. I don't care if it's JSON or XML or YAML, it just needs to be stored in a text file. I'm not concerned with speed or library file size, just datatype versatility and API simplicity. Any suggestions? Thanks!
Apache commons configurations is what you need.
I used the XMLEncoder for this on something I wrote. It allows you to store entire Arrays in xml format. Apache commons configurations may be a good fit for you too as mentioned above. The XMLEnocder is nice because you can just take a java object and write it to a file. Here's a link to an example.
I am looking for a validator to validate tree structure based configuration files.
e.g.
a.student.name joe
a.student.class arts
Can you suggest any ideas on validating such config. So far I have searched and I could find validator for xml files only.
Unfortunately, schema validation for configuration files is rare outside of XML. The only other option I am aware of is Config4J (which I wrote).
If you visit the website, then you should scroll down to the bottom of the main web page to access the complete set of manuals (available in both PDF and HTML versions). I recommend you have a look at the following parts of the manuals to get an overview of Config4J and decide if it satisfies your needs for validation.
Chapters 2 and 3 of the "Getting Started Guide" provide an overview of the configuration syntax and the API. In particular, Section 3.10 provides a quick example of the schema language.
Chapter 9 of the "Getting Started Guide" provides a complete definition of the schema language.
Chapter 3 of the "Java API Guide" discusses the API for writing your own schema types to extend the functionality of the schema language.
Update: I discovered from the answer by kiran.kumar M that the Java and Ruby implementations of YAML have a schema validator called Kwalify.
Update: There is now a schema language for JSON.
Try www.yaml.org . Yaml supports tree structures.
Here is the list of few parsers,
jYaml, SnakeYaml , YamlBeans
Yaml is a file format and supports complex hirarchial structures. Most of the structural validations can be performed automatically by the parsers listed above. You may need addtional code to validate your business needs.
Also few online validators are also available
see :
http://yaml-online-parser.appspot.com/
http://instantyaml.appspot.com/
Also see https://stackoverflow.com/questions/450399/which-java-yaml-library-should-i-use
Validation https://stackoverflow.com/questions/287346/yaml-validation
If the structure is the same as a Java properties file, you can read properties from it. Then, you need to decide what you mean by "validate". If applicable, you probably have an easy way to solve your problem.
Without seeing any form of sample.
If you want to validate the structure of something, and semantics is not an issue you could use lex/yacc (read flex/bison).
Depending on the problem one could venture out and use ox after that. Basically starting to write a mini-compiler.
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.