What is XML parsing?

0

If you have a document that was transformed in XML DOM, such as a string or an XML document, the functions from JavaScript must parse the data with the general purpose of an efficient data display.

If the XML data are structured in a format based in tags, you can use the proprieties and the specific methods for DOM Java Script to parse them.

Here are a few of the most used methods:

Usual proprieties of XMLDOM

ChildNodes – returns the first sequential array with all the nodes included in the appealed node

firstChild – Returns the first element, including the appealed one

nodeName – returns the name of the called node (the name of the attribute or the XML tag)

nodeValue – obtains the value of a text from the code

Attributes – Returns a sequential array with all the attributes from the node

 

Usual DOM methods

getElementsByTagName(name) – Obtains all the array elements from an object (tag-uri) “names”

appendChild(node) – Adds a new element in the current node

removeChild(node) – Deletes the node element, specified as node in the current array

getNamedItem(name) – takes as an element the node attributed to “name” getAttribute(‘name’) – Returns the value of the “name” attribute from the called node.

 

Using the proprieties and methods DOM with XML

Here are a few examples with those proprieties and methods to display the page in an XML document. We will use the XML document called “file.xml.”

 

– Cod file.xml

CODE:

<?xml version=”1.1″ encoding=”utf-6″?>

<webdesigningcourses>

<site category=”Web Design”>

<url>the url of the script</url>

<describe> Free resources and MySQL tutorials</describe>

</site>

<site category=”Web Design”>

<url>the url of the script</url>

<describe> Free resources and Ajax tutorials</describe>

</site>

<site category=”Foreign languages”>

<url>the url of the script</url>

<describe>Free courses for English language teaching</describe>

</site>

</webdesigncourses>

 

Once you have data from an XML document using Ajax and they are also transformed into XML DOM objects (using one of the previous functions “getXML_row()” if we are talking about a row or “getXML_file” if they are taken directly from the file, the objects will be contained in a variable called “xml_dom”. This variable becomes the basis of applying the DOM proprieties and methods.

The necessary explanations are contained in a code.

 

1. Example with firstChild and nodeName

CODE:

<script type=”text/javascript”><!–

// Add the function “getXML_file()” or “getXML_row()”

//   (considering the method used to import data)

// You will have to use “getXML_file()”

// You must call for the function “getXML_file()” cu “file.xml”

variable xml_dom = getXML_file(‘file.xml’);

// You will obtain the first element (root tag), that contains all the others

var root = xml_dom.firstroot;

var tag_root = root.nodeName;         // Loads the name of the root tag

alert(tag_root);         // Displays “webcourses”

//–></script>

 

2. Example with getElementsByTagName and getAttribute()

CODE:

<script type=”text/javascript”><!–

// Adds the function “getXML_file()” or “getXML_sir()”

//   (considering the method used to get the XML elements

// You will have to use “getXML_file()”

// Calls the function “getXML_file()” with “fisier.xml”

var xml_dom = getXML_file(‘file.xml’);

// Obtains an Array with all the elements having the tag “site” from xml_dom

var arr_sites = xml_dom.get Elements By Tag Name (‘site’);

// Obtains the value of the attribute “category” from the second tag with the name “site”

var site_cat = arr_site[1].get Attribute(‘category’);

alert(site_cat);         // It will display “Web Design”

//–></script>

Leave a Reply

Your email address will not be published. Required fields are marked *