问题
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.
回答1:
How about JSON support for JAXB? Reuse your current JAXB annotated model classes but output JSON from your REST endpoints.
Current versions of Jersey support this (via the jersey-json module) with JSONJAXBContext.
You could also try Jackson's JAXB and JAX-RS support.
回答2:
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/
回答3:
I haven't tried this, so I'm not sure if it will work, but have you considered using GWT so that you can still use JAXB and write the whole app as a java app? I'm not sure if GWT supports JAXB (probably not), but there might be an alternative for xml parsing that it will support. If this works, you could automate the creation of your javascript models via gwt, and then include these into your app. Yes, it's a lot more cruft than you want, but beats having to write it from scratch.
回答4:
What you can do is add a generic stylesheet definition: XSLT to your XML to convert them in JSON. eg: http://code.google.com/p/xml2json-xslt/
Handling XML with Javascript is a pain compared to JSON, especially cross browser.
The stylesheet will add a small overhead to your request. Either on the server or client side, you can choose, but you have to compare this to the code complexity and speed to parse and read the XML with Javascript on different browsers.
来源:https://stackoverflow.com/questions/3819192/is-there-a-javascript-api-for-xml-binding-analog-to-jaxb-for-java