Function
$.xmlToJSON(xml)
xml - an instance of DOMDocument or a DOMNode object
Returns - JavaScript Object populated with data from xml input parameter
Note: Returned object itself represents a root node of your XML document and is NOT an array, however all children(nested) objects of the root element are. Array child elements contain special data processing functions such as sorting and various checks.
Important: XML nodes and attributes that contain dashes in the name (i.e. node-name) are automatically converted to underscores (i.e. node_name) to overcome JavaScript naming restrictions. This conversion does not impact any data values inside those elements.
Object Model
Returned object has the following methods and fields:
- Fields
- .typeOf (String) - hard coded value "JSXBObject" (present only at the root level)
Used for checking if object is a product of xmlToJSON processing
- ._children (Array) - lists all child node element names (if child nodes are present)
- ._attributes(Array) - lists all attribute names belonging to this node
- RootName(String) - name oof the root node
- .Text (String) - Contains a text node value or a CDATA within a current node
- Methods
- .getNodeByAttribute(attr, obj) - Returns a JS object that represents a node element where one of the attributes matches specified value
This method is used to lookup an individual node within nodes array
- attr - String name of the attribute
- obj - Value you are looking for inside an attribute specified in attr parameter
- Returns - Object that matches a criterea or null
- .contains(attr, obj) - Returns True if matching attribute/value pair is found in the array of nodes
- attr - String name of the attribute
- obj - Value you are looking for inside an attribute specified in attr parameter
- Returns - Boolean
- .indexOf (attr, obj) - Returns an index position of a node with matching attribute/value pair or -1 if nonne found
- attr - String name of the attribute
- obj - Value you are looking for inside an attribute specified in attr parameter
- Returns - Integer position or -1 if no match found
- .SortByAttribute(attr, dir) - Sorts an array of nodes by specified attribute
(example)
- attr - String name of the attribute
- dir - String direction of the sort options are "ASC" | "DESC"
- .SortByValue(dir) - Sorts all the nodes in the array by node value
(example)
- dir - String direction of the sort options are "ASC" | "DESC"
- .SortByNode(node, dir) - Sorts all nodes by their child node names (example)
- node - String name of the child node
- dir - String direction of the sort options are "ASC" | "DESC
- BETA Methods
- getNodesByAttribute(attr, obj) - Returns an array of all node elements that match attribute value
- attr - String name of the attribute
- obj - Value you are looking for inside an attribute specified in attr parameter
- Returns - Array of objects that matches a criterea or null
- getNodesByValue(obj) - Returns an array of all node elements that match node value
- obj - Value you are looking for inside a node
- Returns - Array of objects that matches a criterea or null
Note: All Sort functions automatically determine sort type Numeric | Text
$.textToXML(textXML)
textXML - String containing valid XML
Returns - DOMDocument object if text String was successfully parsed or false if not
This simple and useful utility converts XML String into a DOMDocument object so it can be used to convert to JSON using xmlObjectifier or for any other purpose. One useful way to take advantage of this function is to include some XML data directly on your HTML page inside an invisible element and then use it for any type of data persistance.