YUI 3.x Home -

Site Search (YDN & YUIBlog):

# YUI Library Examples: DataType Utility: DataType.XML.parse()

## DataType Utility: DataType.XML.parse()

The XML module of the DataType Utility allows you to take a string and convert it to an XML document.

View example in new window.

String to parse: <myroot><item type='foo'><name>Abc</name><rank>1</rank></item><item type='bar'><name>Def</name><rank>2</rank></item><item type='bat'><name>Ghi</name><rank>3</rank></item></myroot>

To convert a string to an XML document, simply call the `parse()` function of the DataType.Number class:

    YUI().use("datatype-xml", function(Y) {    var output = Y.DataType.XML.parse("&lt;myroot&gt;&lt;item type='foo'&gt;&lt;name&gt;Abc&lt;/name&gt;&lt;rank&gt;1&lt;/rank&gt;&lt;/item&gt;&lt;item type='bar'&gt;&lt;name&gt;Def&lt;/name&gt;&lt;rank&gt;2&lt;/rank&gt;&lt;/item&gt;&lt;item type='bat'&gt;&lt;name&gt;Ghi&lt;/name&gt;&lt;rank&gt;3&lt;/rank&gt;&lt;/item&gt;&lt;/myroot&gt;");    // output is an XML document});

    YUI().use("datatype-xml", function(Y) {
        var output = Y.DataType.XML.parse("&lt;myroot&gt;&lt;item type='foo'&gt;&lt;name&gt;Abc&lt;/name&gt;&lt;rank&gt;1&lt;/rank&gt;&lt;/item&gt;&lt;item type='bar'&gt;&lt;name&gt;Def&lt;/name&gt;&lt;rank&gt;2&lt;/rank&gt;&lt;/item&gt;&lt;item type='bat'&gt;&lt;name&gt;Ghi&lt;/name&gt;&lt;rank&gt;3&lt;/rank&gt;&lt;/item&gt;&lt;/myroot&gt;");
        // output is an XML document
    });

YUI().use("datatype-xml", function(Y) { var output = Y.DataType.XML.parse("<myroot><item type='foo'><name>Abc</name><rank>1</rank></item><item type='bar'><name>Def</name><rank>2</rank></item><item type='bat'><name>Ghi</name><rank>3</rank></item></myroot>"); // output is an XML document });

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings