[ACCEPTED]-XML parser for JavaScript-parsing
Disclaimer: I am the author if the open-source Jsonix library 27 which may be suitable for the task.
A couple 26 of years ago I was also looking for a good 25 XML<->JSON parsing/serialization library 24 for JavaScript. I needed to process XML 23 documents conforming to rather complex XML 22 Schemas. In Java, I routinely use JAXB for the 21 task so I was looking for something similar:
Is there a JavaScript API for XML binding - analog to JAXB for Java?
I 20 failed to find such a tool back then.
So 19 I wrote Jsonix which I consider to be a JAXB analog 18 for JavaScript.
You may find Jsonix suitable, if 17 you're interested in the following features:
- XML<->JSON conversion is based on a declaraive mapping between XML and JSON structures
- This mapping can be generated from an XML Schema or written manually
- Bidirectional - supports parsing as well as serialization (or unmarshalling/marshalling in other terms).
- Support elements, attributes and also considers namespaces defined in the XML document.
- Strictly typed.
- Strictly structured.
- Support almost all of the XML Schema built-in types (including special types like
QName
). - Works in browsers as well as Node.js, also compatible to RequireJS/AMD (also to
amdefine
in Node.js) - Has extensive documentation.
However, Jsonix 16 may be an overkill, if your XML is rather simple, does not 15 have an XML Schema or if you're not interested 14 in strict typing or structures. Check your 13 requirements.
Example
Try it in JSFiddle.
You can take a purchase order schema and 12 generate a mapping for it using the following 11 command:
java -jar node_modules/jsonix/lib/jsonix-schema-compiler-full.jar
-d mappings -p PO purchaseorder.xsd
You'll get a PO.js
file which describes 10 mappings between XML and JavaScript structures. Here's 9 a snippet from this mapping file to give 8 you an impression:
var PO = {
name: 'PO',
typeInfos: [{
localName: 'PurchaseOrderType',
propertyInfos: [{
name: 'shipTo',
typeInfo: 'PO.USAddress'
}, {
name: 'billTo',
typeInfo: 'PO.USAddress'
}, {
name: 'comment'
}, {
name: 'orderDate',
typeInfo: 'Calendar',
type: 'attribute'
}, ...]
}, {
localName: 'USAddress',
propertyInfos: [ ... ]
}, ...],
elementInfos: [{
elementName: 'purchaseOrder',
typeInfo: 'PO.PurchaseOrderType'
}, ... ]
};
Having this mapping file 7 you can parse the XML:
// First we construct a Jsonix context - a factory for unmarshaller (parser)
// and marshaller (serializer)
var context = new Jsonix.Context([PO]);
// Then we create a unmarshaller
var unmarshaller = context.createUnmarshaller();
// Unmarshal an object from the XML retrieved from the URL
unmarshaller.unmarshalURL('po.xml',
// This callback function will be provided
// with the result of the unmarshalling
function (unmarshalled) {
// Alice Smith
console.log(unmarshalled.value.shipTo.name);
// Baby Monitor
console.log(unmarshalled.value.items.item[1].productName);
});
Or serialize your JavaScript 6 object as XML:
// Create a marshaller
var marshaller = context.createMarshaller();
// Marshal a JavaScript Object as XML (DOM Document)
var doc = marshaller.marshalDocument({
name: {
localPart: "purchaseOrder"
},
value: {
orderDate: { year: 1999, month: 10, day: 20 },
shipTo: {
country: "US",
name: "Alice Smith",
street: "123 Maple Street",
city: "Mill Valley",
state: "CA",
zip: 90952
},
billTo: { /* ... */ },
comment: 'Hurry, my lawn is going wild!',
items: { /* ... */ }
}
});
You can try it in JSFiddle to see how 5 it works in practice.
Additional disclaimer: this answer is high-voted 4 because of the following discussion on meta. So please be aware of the "meta-effect". High votes here 3 do not necessarily mean that Jsonix is good, applicable 2 or recommended by the community. Do not 1 be mislead by the high votes.
I use jQuery for this. Here is a good example:
(EDIT: Note 7 - the following blog seems to have gone 6 away.)
http://blog.reindel.com/2007/09/24/jquery-and-xml-revisited/
There are also lots and lots of good 5 examples in the jQuery documentation:
http://www.webmonkey.com/tutorial/Easy_XML_Consumption_using_jQuery?oldid=20032
EDIT: Due 4 to the blog for my primary example going 3 away, I wanted to add another example that 2 shows the basics and helps with namespace 1 issues:
If your XML is in a simple format you may 2 look at jQuery and the XML to JSON plugin or the xmlObjectifier.
For a straight 1 parser you may want to look at XML for <SCRIPT>.
Have you tried XML for SCRIPT. I have to admit, that I 4 have never used it personally, but I have 3 heard/read a few good things about it.
Give 2 it a try and maybe share your experience 1 here?
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.