Zimlet JavaScript API Reference - AjxXmlDoc

Class AjxXmlDoc


Do not directly instantiate AjxXmlDoc, use one of the create factory methods instead.

Defined in: AjxXmlDoc.js.

Class Summary
Constructor Attributes Constructor Name and Description
 
Default constructor.
Method Summary
Method Attributes Method Name and Description
<static>  
AjxXmlDoc.create()
Creates an XML doc.
<static>  
AjxXmlDoc.createElement(name, value)
Creates an XML document with the element.
<static>  
AjxXmlDoc.createFromDom(doc)
Creates an XML doc from a document object.
<static>  
AjxXmlDoc.createFromXml(xml)
Creates an XML doc from an XML string.
<static>  
AjxXmlDoc.createRoot(rootName)
Creates an XML document with a root element.
 
Gets the document.
<static>  
AjxXmlDoc.replaceInvalidChars(s)
Replaces invalid characters in the given string.
 
toJSObject(dropns, lowercase, withAttrs)
This function tries to create a JavaScript representation of the DOM.
Class Detail
AjxXmlDoc()
Default constructor.
Method Detail
<static> {AjxXmlDoc} AjxXmlDoc.create()
Creates an XML doc.
Returns:
{AjxXmlDoc} the XML doc

<static> {AjxXmlDoc} AjxXmlDoc.createElement(name, value)
Creates an XML document with the element.
Parameters:
{string} name
the element name
{string} value
the element value
Returns:
{AjxXmlDoc} the XML document

<static> {AjxXmlDoc} AjxXmlDoc.createFromDom(doc)
Creates an XML doc from a document object.
Parameters:
{Document} doc
the document object
Returns:
{AjxXmlDoc} the XML doc

<static> {AjxXmlDoc} AjxXmlDoc.createFromXml(xml)
Creates an XML doc from an XML string.
Parameters:
{string} xml
the XML string
Returns:
{AjxXmlDoc} the XML doc

<static> {AjxXmlDoc} AjxXmlDoc.createRoot(rootName)
Creates an XML document with a root element.
Parameters:
{string} rootName
the root name
Returns:
{AjxXmlDoc} the XML document

{Document} getDoc()
Gets the document.
Returns:
{Document} the document

<static> {string} AjxXmlDoc.replaceInvalidChars(s)
Replaces invalid characters in the given string.
Parameters:
{string} s
the string
Returns:
{string} the resulting string

toJSObject(dropns, lowercase, withAttrs)
This function tries to create a JavaScript representation of the DOM. In some cases, it is easier to work with JS objects rather than do DOM lookups.

Rules:

  1. The top-level tag gets lost; only it's content is seen important.
  2. Each node will be represented as a JS object. It's textual content will be saved in node.__msh_content (returned by toString()).
  3. Attributes get discarded.
  4. Each subnode will map to a property with its tagName in the parent node parent[subnode.tagName] == subnode
  5. If multiple nodes with the same tagName have the same parent node, then parent[tagName] will be an array containing the objects, rather than a single object.
So what this function allows us to do is for instance this, starting with this XML doc:
<error>
  <code>404</code>
  <name>Not Found</name>
  <description>Page wasn't found on this server.</description>
</error>
var obj = AjxXmlDoc.createFromXml(XML).toJSObject();
alert(obj.code + " " + obj.name + " " + obj.description);
Here's an array example:
<return>
  <item>
    <name>John Doe</name>
    <email>foo@bar.com</email>
  </item>
  <item>
    <name>Johnny Bravo</name>
    <email>bravo@cartoonnetwork.com</email>
  </item>
</return>
var obj = AjxXmlDoc.createFromXml(XML).toJSObject();
for (var i = 0; i < obj.item.length; ++i) {
  alert(obj.item[i].name + " / " + obj.item[i].email);
}
Note that if there's only one <item> tag, then obj.item will be an object rather than an array. And if there is no <item> tag, then obj.item will be undefined. These are cases that the calling application must take care of.
Parameters:
dropns
lowercase
withAttrs

Documentation generated by JsDoc Toolkit 2.3.0 on Tue Jun 28 2016 21:01:30 GMT-0400 (EDT)