Showing posts with label XML Parser. Show all posts
Showing posts with label XML Parser. Show all posts

XML and using Xerces parser for Java to generate and parse XML

his small tutorial introduces you to the basic concepts of XML and using Xerces parser for Java to generate and parse XML.
The intended audience are XML beginners with knowledge of Java.
DOM (Document Object Model ) parser - Tree Structure based API:
    The Dom parser implements the dom api and it creates a DOM tree in memory for a XML document
5.2 SAX (Simple API For XML ) parser - Event Based API
    The SAX parser implements the SAX API and it is event driven interface. As it parses it invokes the callback methods
5.3 When to use DOM parser
  • Manipulate the document
  • Traverse the document back and forth
  • Small XML files
Drawbacks of DOM parser
    Consumes lot of memory 5.4 When to use SAX parser
  • No structural modification
  • Huge XML files
5.5 Validating And Non Validating
DOM and SAX can either be a validating or a non validating parser.
    A validating parser checks the XML file against the rules imposed by DTD or XML Schema.
    A non validating parser doesn't validate the XML file against a DTD or XML Schema.
Both Validating and non validating parser checks for the well formedness of the xml document

View Complete Tutorial here

How to create XML document using java,Java Architecture for XML Binding (JAXB)

In this section, you will  learn to create a XML document using the DOM APIs. This XML document uses  1.0 version  and UTF-8 encoding. 
To work with an XML document it is easy to do so, if you have a document object in place and the XML document loaded in it. Yes, java too has APIs for working with an XML document. With this API, you can navigate through the XML document, or create an XML document from the scratch.
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder bd = fact.newDocumentBuilder();
doc = bd.newDocument();
Element rt = (Element) doc.createElement("rtElement");
document.appendChild(root);
rt.appendChild( doc.createTextNode("Some") );
. . .
. . .
Proper imports are to be done to work with the classes and the methods needed for working with an XML document. The above code sample would give you an idea on how to go about it. Methods like createElement of the Element object are used to create elements.
And these elements are added to the root node using the method appendChild. Lots of sample codes are available in the internet on this topic.
 Here is Java File: CreatXMLFile.java