Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a JavaScript API for XML binding - analog to JAXB for Java?

In Java, we work a lot with JAXB2. Object<->XML mappings are defined as annotations in Java classes:

@XmlRootElement(name="usertask", namespace="urn:test")
public class UserTask
{
    @XmlElement(namespace="urn:test")
    public String getAssignee() { ... }

    public void setAssignee(String assignee) { ... }
}

JAXB runtime can read these annotations and create unmarshaller to parse XML into an object instance or marshall an object into XML.

JAXB ships a schema compiler (XJC) which can generate annotated classes out of XML Schemas, which is another great feature.


Lately we've been working a lot with client-side JavaScript. An we also need XML processing there. For example, we need to parse WPS documents like this one. These documents also comply to different XML schemas (here's the WPS 1.0.0 schema for the sample XML). It would be great to work with JavaScript objects instead of XML, this saves really huge amount of effort. In some cases we can use JSON-based solutions like DWR, but in many cases we do have to process XML on the client-side.

My question is:

Is there some analog of JAXB for JavaScript?

Some tool which would compile an XML Schema into some XML<->object mapping and provide a runtime to convert between XML and JavaScript objects?

I could easily imagine mappings generated in a form like:

UserTask = new JSXML.XmlRootElement({
  name: "usertask",
  namespace: "urn:test",
  properties: [
    {
      assignee: new JSXML.XmlElement({
        name: "assignee",
        namespace: "urn:test",
        type: new JSXML.XSD.String()
      })
    }
  ]
});

And this should be pretty enough to build unmarshaller or marshaller.

like image 953
lexicore Avatar asked Sep 29 '10 06:09

lexicore


People also ask

How XML binding in Java can be achieved?

First Create Java Objects to be Marshalled. Create JAXBContext Object and initializing Marshaller Object. To get the formatted xml output one can set JAXB_FORMATTTED_OUTPUT to True(this Step is optional). Create xml file object by providing location of file as parameter to File Class.

Is JAXB an API?

Now developers have another Java API at their disposal that can make it easier to access XML documents: Java Architecture for XML Binding (JAXB). A Reference Implementation of the API is now available in the Java Web Services Developer Pack V 1.1.

What is the correct definition for binding the schema in Java Architecture for XML Binding JAXB )?

The Java Architecture for XML Binding (JAXB) provides an API and tools that automate the mapping between XML documents and Java objects. The JAXB framework enables developers to perform the following operations: Unmarshal XML content into a Java representation.

What can I use instead of JAXB?

XOM, JDOM, dom4j, etc. etc. Projects like Castor and Apache XMLBeans predate JAXB, so you could have a look at those. Ulf Dittmer wrote: XOM, JDOM, dom4j, etc.


1 Answers

To date, I have found nothing similar to what I need. Therefore I've deсided to implement it myself. Here's the project page:

http://confluence.highsource.org/display/MISC/Jsonix

The project is hosted on GitHub:

https://github.com/highsource/jsonix/

like image 136
lexicore Avatar answered Sep 21 '22 13:09

lexicore