Leon Anavi
IT Tips && Tricks

Java

Created: 28.07.2009 01:27 Last Modified: 04.09.2009 18:38 Views: 5819
Keywords: Attribute, Document, Element, Node, Parsing, XML

Parsing XML

Introduction

XML (Extensible Markup Language) is a general-purpose markup language used for data serialization. It has been developed by World Wide Web Consortium. Parsing is the process of analyzing and validation of XML and building appropriate data structures. XML schema is used to describe the structure of a XML document. The parser must follow the regulations of the XML schema.

Parsing XML with Java

Packages javax.xml.parsers.DocumentBuilder, javax.xml.parsers.DocumentBuilderFactory and org.w3c.dom.* should be imported. An instance of a Document should be created With the assistance of DocumentBuilderFactory and DocumentBuilder. The XML content should be processed with the method parse which can be used with a File or a text stream. On error SAXException or IOException exception will be thrown.

Processing a File

try
{
	String sFile = "/path/to/file";
	DocumentBuilderFactory DocFactory = DocumentBuilderFactory.newInstance();
	DocumentBuilder DocBuilder = DocFactory.newDocumentBuilder();
	//Do not forget to import java.io.File
	Document DocToParse = DocBuilder.parse ( new File( sFile ) );
}
catch(Exception Ex)
{
	//...
}

Processing a Text Stream

try
{
	String  sInput = "<example>\n\t<foo name=\"foo1\">value1</foo>\n";
	sInput += "\t<foo name=\"foo2\">value2</foo>\n</example>";
	ByteArrayInputStream ssInput = new ByteArrayInputStream(sInput.getBytes());
	DocumentBuilderFactory DocFactory = DocumentBuilderFactory.newInstance();
	DocumentBuilder DocBuilder = DocFactory.newDocumentBuilder();
	Document DocToParse = DocBuilder.parse(ssInput);
}
catch(Exception Ex)
{
	//...
}

Sample Application for XML Parsing

The application will parse input XML:
<example>
    <foo name="foo1">value1</foo>
    <foo name="foo2">value2</foo>

</example>
package parsexmlconsole;

import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;

/**
 *
 * @author Leon Anavi
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Document DocToParse = null;
	try
	{
            String  sInput = "<example>\n\t<foo name=\"foo1\">value1</foo>\n";
            sInput += "\t<foo name=\"foo2\">value2</foo>\n</example>";
            ByteArrayInputStream ssInput = new ByteArrayInputStream(sInput.getBytes());
            DocumentBuilderFactory DocFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder DocBuilder = DocFactory.newDocumentBuilder();
            DocToParse = DocBuilder.parse(ssInput);
	    // normalize text representation
            DocToParse.getDocumentElement().normalize();
            
            //get all elements with tag foo
            NodeList FooNodes = DocToParse.getDocumentElement().getElementsByTagName("foo");
            int nNodes = FooNodes.getLength();
            //loop through elements
            for (int nIter = 0; nIter < nNodes; nIter++)
            {
                //read the name which is an attribute
                Node SelectedNode = FooNodes.item(nIter);
                Node AttrName = SelectedNode.getAttributes().getNamedItem("name");
                String sName = AttrName.getTextContent();
                //get value
                String sValue = SelectedNode.getTextContent();

                //Show parsed content
                String sText = "Name: '";
                sText += sName;
                sText += "' Value: '";
                sText += sValue;
                sText += "'.";
                System.out.println(sText);
            }
	}
	catch(Exception Ex)
	{
	    System.out.println(Ex.getMessage());
	}
    }
}

Output

Name: 'foo1' Value: 'value1'.
Name: 'foo2' Value: 'value2'.

External Links

XML 1.0 (Fifth Edition)

Class Reference

Class DocumentBuilder
Class DocumentBuilderFactory
Class ByteArrayInputStream
Interface Document


  Home | About | Contact | Disclaimer | Sitemap 2009-2016 Leon Anavi. All rights reserved.