Example: Y.XML.parse()

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


Report nodeName and nodeType of parsed XML
Node Name:
Node Type:

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

YUI().use("datatype-xml", function(Y) {
    var output = Y.XML.parse("<myroot><item type='foo'>...</item></myroot>");
    // output is an XML document
});

Full example source:

<form id="demo">
   <fieldset>
      <label for="demo_input">String to parse:</label>
      <textarea type="text" id="demo_input" style="width:100%;"><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></textarea>
      <br>
      <a href="#" id="demo_btn">Report nodeName and nodeType of parsed XML</a>
  </fieldset>
</form>
   <div id="output">
       Node Name: <span id="nodename"></span>
       <br>
       Node Type: <span id="nodetype"></span>
   </div>

<script>
YUI().use("node", "datatype-xml", function (Y) {
    Y.on("click", function(e){
   e.preventDefault();
   var input = Y.one("#demo_input").get("value"),
       output = Y.XML.parse(input);
           
   if(output === "") {
       output += "(Invalid value)"
   }
   
   Y.one("#nodename").set("text", output.nodeName);     
   Y.one("#nodetype").set("text", output.nodeType);
    }, "#demo_btn");
});
</script>