XML Parser

Last Updated On Sunday 29th May 2022

XML Parse

  • XML can revolutionize the way that people look at and transfer information across the Web.
  • All modern browsers have a built-in XML parser that can convert text into an XML DOM object.

A DOM document is an object which contains all the information of an XML document.It is composed like a tree structure.

DOM (Document Object Model) parser

  • DOMParser has a tree based structure.
  • It is Very simple to use and the API Supports read and write operations.

SAX (Simple API for XML)parser

  • A SAX Parser implements SAX API.
  • This API that uses event-based analysis
  • It is very fast and works for huge documents.

Ælfred – Java-based parser

  • Ælfred is a nonvalidating parser, which means that it checks documents only for well-formedness.
  • It concentrates on optimizing speed and size.

expat – C-based parser

  • NonValidating XML parser written in C
  • It support all kinds of cool functionality.

Lark – Java-based parser

  • Lark, a nonvalidating parser
  • The parser is solidly built

javascript parse xml

Example 1 – DOMParser

	xmlText =
  '<article>' +
  '<title>Introduction to XML</title>' +
  '<author>CJ</author>' +
  '<published>2022-04-11</published>' +
  '<category>XML</category>' +
  '</article>';

parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlText, 'text/xml');
	

javascript xml parser

DOMParser creates a new XML DOM object using the text string.

	xmlDoc = parser.parseFromString(xmlText, 'text/xml');
	

parse xml in javascript

Example 2

	import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
public class readXML {
    static public void main(String[] argv) {
        try {
            DOMParser parser = new DOMParser();
            parser.parse("articles.xml");
            Document doc = parser.getDocument();
            for (Node node = doc.getDocumentElement().getFirstChild(); node! = null; node = node.getNextSibling()) {
                if (node instanceof Element) {
                    if (node.getNodeName().equals("article")) {
                        StringBuffer buffer = new StringBuffer();
                        for (Node subnode = node.getFirstChild(); subnode! = null; subnode =
                            subnode.getNextSibling()) {
                            if (subnode instanceof Text) {
                                buffer.append(subnode.getNodeValue());
                            }
                        }
                        System.out.println(buffer.toString());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
	

The Above Java Program returns all the text of the <article> element , including leading spaces

Example Links

javascript xml parser
js xml parser
xml parser javascript
xml javascript parser
parser xml javascript
javascript xml parser example
javascript parser xml
parsefromstring xml
xmlparser javascript
parser.parsefromstring xml parser in javascript
dom xml parser
domparser xml