Java: Ways to parse XML in E4X? - java

I was wondering if there was a way to parse XML using E4X, or something similar to E4X.
Does such a framework / library exist?
Thanks!

You can use JavaScript engine Rahino with Java which can handle E4X.
http://blogs.oracle.com/sundararajan/entry/desktop_scripting_applications_with_netbeans
http://www.ibm.com/developerworks/library/ws-ajax1/

Java cannot support dynamically defined members, as JavaScript can.
However, with design-time generation, you can get Java whose members reflect the XML. E.g., JAXB

E4X is a language extension, XML is treated like a primitive. E4X is not just for parsing XML, it's using XML as real types.
This can't be simulated or done with a Java 'framework', it would require a language extension for Java.

There is no parsing XML with E4X. It is a specification that makes XML a native data type. Among browsers, only Firefox supports it as of now.
Here's a list of all known implementations of the spec.
A framework can only mimic making XML access easier, but will not fundamentally change the way we use XML. For example, the SimpleXML extension in PHP simplifies things a lot, but under the hood it converts elements to objects using reflection.
So to have something like E4X, it has to be implemented in the language itself and there is no other non-ECMAScript based language that has this as of now.

Related

Differences between Java Serialization, JSON, JAXB?

Is an object's implementation of the Serializable interface in any way related to that object's ability to be serialized into JSON or XML?
Is there a name for the text format that Java serialization uses?
If not, should we not use the word "serialization" to describe exporting an object to JSON or XML, to avoid confusion?
At AO, what uses are typical for each of these three serialization methods?
I know that JAXB is usually used to convert XML to Java, not the other way around, but I heard that the reverse is possible too.
Serialization simply refers to exporting an object from a process-specific in-memory format to an inter-process format that can be read and understood by a different process or application. It may be text, or it may be binary, it doesn't matter. It's all serialization. The reverse processes (reading and parsing a serialized inter-process format into an in-memory, in-process format) is called deserialization.
In that sense, serializing an object into an ObjectStream is just as much serialization as serializing it to JSON or XML. ObjectStream serialization is very difficult to understand/parse by non-java (including humans. It is not "human-readable"), but is used because it can be done on pretty much any object without any special markup.
JSON/XML on the other hand require extra work to tell the parser how to map them to and from JSON/XML, but are very portable - pretty much every language can understand JSON/XML, including humans - it is "human-readable".
One purpose of serialization of Java objects is being able to write them to a (binary) file from which some Java program can read them back, getting the same objects into its memory. This usage is usually limited to Java applications, writing and reading, although some non-Java app might be written to understand the binary format.
Another frequently used serialization of Java objects is to write them to a text (or binary) file from which some (note the absence of: Java) program can read and reconstruct an object or data structure equivalent to the POJO. This, of course, also works in the reverse direction. (I'm adding "binary", because there are some binary formats not defined by Java that are architecture-independent, e.g., ASN.1.)
And, yes, JAXB works either way, but there are some difficulties if the XML is rather "outlandish", i.e., far away from what JAXB can handle or handle easily. But if you can design either the XML Schema or the Java classes, it works very well. JAXB being part of the JDK, you might prefer using it over other serializations if you need to go from Java to X or back. There are other languange binding for XML.

Import Java Custom Method in Xquery

I am using Weblogic Integration framework. While transforming one XML format to another using .xq file, I want to apply some logic written in a custom Java Class.
For example, XML1 has tag: <UnitCode>XYZ</UnitCode>
Custom Java Class:
public class unitcodemapper{
public static String getMappedUnitCode(String unitCode){
if(unitCode=="XYZ")
return <<value from DB table>>
else
return unitCode;
}
}
XML2 will have a tag: <UnitCode>unitcodemapper.getMappedUnitCode(XML1/UnitCode)</UnitCode>
I cannot find any documentation or example to do this. Can someone please help in understanding how this can be done?
This is known as an "extension function". The documentation for your XQuery implementation should have a section telling you how to write such functions and plug them into the processor. (The details may differ from one XQuery processor to another, which is why I'm referring you to the manual.)
Whilst #keshlam mentions Extension Functions, which are indeed supported by many implementations each with their own API.
I think perhaps what you are looking for instead is Java Binding from XQuery. Many implementations also support this and tend to use the same approach. I do not know whether WebLogic supports this or not! If it does, the trick is to use java: at the start of your namespace URI declaration, you can then use the fully qualified Java class name of a static class, each static method you may then call directly from that namespace.
You can from two examples of implementations that offer the same Java Binding from XQuery functionality here:
http://exist-db.org/exist/apps/doc/xquery.xml#calling-java
http://docs.basex.org/wiki/Java_Bindings
These could serve as examples for you to try on WebLogic to see if it is supported in the same way. However, I strongly suggest you check their documentation as they may take a different approach.

Which JVM-based language should I use for mapping of one type to another?

I'm currently working with Java to write a program that does an EAI between two applications. One application comes with HL7, which I parse with HAPI. So I get a Java object structure. I want to transform this structure to my own structure that I want to use to generate XML files with JAXB after doing some other work.
In my opinion my current solution is not very nice, because the source code gets very complex:
public NaturalPerson convertPID(PID pid) {
NaturalPerson person = new NaturalPerson();
NameNaturalPerson personsname = new NameNaturalPerson();
name.setFamilyName(pid.getPatientName().getFamilyName().getValue());
...
}
Which language is an appropiate Language to do such type mappings? (http://en.wikipedia.org/wiki/List_of_JVM_languages)
I think Java is not the best language for doing that. I don't have much time for learning, so I need a language that is easy to learn and which has a low begin-of-learning-peek. I already have some experience in the functional languages Haskell and F#. First I thought Groovy would be a good language, but then I found other opinions that suggest Scala.
Which language would you suggest for doing such type mappings?
Did you look at Dozer? It is a Java library that recursively copies data from one Java object to another. There are several ways to configure the mapping:
XML
Java API providing a DSL
Java annotations
Data in forms of Maps and Vectors handling are superbly handled on the JVM using Clojure
See all the core functions available and this SO Question on which tutorials are good to learn Clojure.

Tutorials/FAQs/Samples/Basics for JSON

I am trying to create and parse JSON, and I get by with some samples found on Google/SO or trial-and-error. But I need some help with JSON basics, parsing, creating arrays inside JSON strings, and so on. I read about the JSONStringer and such, but I need information about parsing and creating complex JSON.
EDIT: I use Java.
Thanks.
First step typically is to look beyond bare-bones Java lib from org.json; other related questions therefore are, for example:
https://stackoverflow.com/questions/338586/a-better-java-json-library
https://stackoverflow.com/questions/1668862/good-json-java-library
The reason for this is that there is no point in worrying too much about low-level details; rather you usually want to operate either with Java collections (List, Maps, wrapper types) or with basic Java objects. Other libraries can offer such abstractions.
My personal favorite is Jackson, and its tutorial is found here.
which language-script?
for example, if you are using javaScript jQuery offers you few functions for json (http://api.jquery.com/jQuery.parseJSON/)..
There isn't much to it. You got objects, arrays and primitives such as string, number, boolean and null. The syntax can be picked up by googling JSON.
The handling of JSON is more down to frameworks and server - are you translating a server side domain model to JSON? What server technology?
Client side pretty much any decent framework has helper methods for parsing JSON to get around certain browser differences (native JSON parsing being one). Check out jQuery.getJSON.
You can learn about JSON here.
In the Java side, you should actually not be writing/parsing JSON yourself. That's only a lot of tedious work and a waste of effort since there are plenty of libraries for this. Just pick a library which is able to convert a complex Java object to a JSON string (and vice versa) in a single call. This way you can concentrate on writing clean Java code, not on fiddling with JSON syntax in plain Java strings.
See also:
Converting complex JSON to Java
How to use Ajax/JSON in JSP/Servlet

How to create java object by reflection by reading field names and types from an xml file?

I want to create a class dynamically at run time by reading field names and its types from an xml file.For example my xml file looks like this:
<person>
<name type="String">abc</name>
<age type="Integer">30</age>
</person>
I also want to have getter and setters methods for each field.
Any examples or best approaches available for this?
Take a look at XStream, it is extremely easy to serialize to/from XML.
This is technically possible, but (unless someone can point out an existing solution) it would be a lot of work. (You can do a lot of clever things by generating source code and compiling it at runtime ... for example.)
But to be honest, this is probably not a useful thing to do. Once you've loaded your XML object as an instance of a brand new Java class, you'll have great difficulty using it. For a start, your existing statically compiled application will only be able to access the fields and methods of the new class reflectively.
IMO, you'd be better of loading the XML into generic Map objects or Properties objects. Or, just use a DOM created by an off-the-shelf XML parser.
Alternatively, get hold of a DTD, XSD, or some other kind of "schema" for the XML and generate Java classes from that. Then can write and statically compile your application to call those classes.
Java is not a dynamic language, so you cannot create classes dynamically, but the term 'create' is not well defined in your question.
If you mean instantiate and initialize, that can be done very easily through serialization with libraries like:
jaxb.dev.java.net/
www.castor.org/
jibx.sourceforge.net/
http://x-stream.github.io/
etc.
If you mean you want to actually create a class file within the JVM at runtime, you might want to look at more dynamic langauges capable of running in a JVM like Groovy, or JRuby, etc. Groovy has some pretty cool dynamic capabilities.

Categories