Class
caplin.core

XmlParser

Utility class that provides methods for parsing XML strings into Document Objects. The object is a singleton so caplin.core.XmlParser should be used to obtain an instance of an XmlParser.

Example:

var oXmlParser = caplin.core.XmlParser;
var oDocument = oXmlParser.parse("<test />");
alert(oDocument.tagName);

The caplin.core.XmlUtility singleton provides further helper methods to manipulate XML.

Constructor Summary

Attributes Name and Description
caplin.core.XmlParser()

Constructs a XmlParser.

Method Summary

Attributes Name and Description
<static> caplin.core.XmlParser getInstance()

Gets an instance of XmlParser.

<static> DOMDocument parse(String sXml)

Parses an XML string, stripping out any whitespace and comments, and returns a document object representation.

<static> DOMDocument parseString(String sXml)

Parses an XML string and returns a document object representation.

<static> String stripWhitespace(String sXml)

Strips out all of the unnecessary whitespace characters from the specified XML string.

Constructor Detail

caplin.core.XmlParser()

Constructs a XmlParser. This has been included for backwards compatibility.

It is recommended that the caplin.core.XmlParser#getInstance method is used to get an instance instead of generating a new XmlParser every time.

Method Detail

<static> caplin.core.XmlParser getInstance()

Gets an instance of XmlParser. This is intended to allow a single instance to be used throughout the application.

Deprecated
This method is no longer needed as XmlParser is now a singleton.
Returns
An instance of XmlParser.

<static> DOMDocument parse(String sXml)

Parses an XML string, stripping out any whitespace and comments, and returns a document object representation.

This method supersedes #parseString as it will return an XML DOM that is consistent across different browsers. All comments and unnecessary whitespace characters will be stripped out of the XML DOM.

Any encoded characters within the specified string (such as &) will be decoded in the returned DOMDocument. Please see caplin.core.XmlUtility for more information on character encoding in XML.

Parameters
String sXml The XML string to be parsed.
Returns
An XML DOM representing the specified XML. If the XML failed to be parsed an XML DOM with the root node parsererror will be returned.

<static> DOMDocument parseString(String sXml)

Parses an XML string and returns a document object representation.

Parameters
String sXml The XML string to be parsed.
Deprecated
This method produces different XML DOM representations for the specified XML depending on which browser it is run within. For example, IE strips out whitespace whilst Firefox keeps it. It is recommended that the #parse method is used instead.
Returns
An XML DOM representing the specified XML. If the XML failed to be parsed an XML DOM with the root node parsererror will be returned.

<static> String stripWhitespace(String sXml)

Strips out all of the unnecessary whitespace characters from the specified XML string. These are the whitespace characters stripped at the beginning and end of the string, and in between each of the tags. This makes parsing of the XML easier in Firefox which includes the whitespace within the DOM tree.

Parameters
String sXml The XML to have the whitespace stripped from.
Returns
The XML with all unnecessary whitespace characters stripped out.
See
#parse