Thursday, June 19, 2008

DOM XML

DOM XML First you need a DOM document to put your XML into. Second, the XML is assembled in tree fashion with the root element at the top. The root is added to the Document object. Other elements can be added to the root, and attributes are added to any elements. Comments and Text may be added to any of the elements. Third you need to transform your XML tree into something like an OutputStream or Writer.
 import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class DomXmlExample {

/**
* Our goal is to create a DOM XML tree and then print the XML.
*/
public static void main (String args[]) {
new DomXmlExample();
}

public DomXmlExample() {
try {
/////////////////////////////
//Creating an empty XML Document
//We need a Document
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument(); ////////////////////////
//Creating the XML tree

//create the root element and add it to the document

Element root = doc.createElement("root");
doc.appendChild(root);

//create a comment and put it in the root element
Comment comment = doc.createComment("Just a thought");
root.appendChild(comment);

//create child element, add an attribute, and add to root
Element child = doc.createElement("child");
child.setAttribute("name", "value");
root.appendChild(child);

//add a text element to the child
Text text = doc.createTextNode("Filler, ... I could have had a foo!");
child.appendChild(text);

/////////////////
//Output the XML

//set up a transformer

TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");

//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();

//print xml
System.out.println("Here's the xml:nn" + xmlString);

} catch (Exception e) {
System.out.println(e);
}
}
}

To Count XML Element

This program helps to count the XML element. It takes a xml file (as a string ) at the console and checks its availability. It parses the xml document using the parse() method. After parsing the XML document it asks for element name which have to count. Create a NodeList and use the getElementByTagName() method. The getLength() method counts the occurrences of the specified element. If the asked element is not available( i.e.. the given element isn't found) it returns 0.

import org.w3c.dom.*;
import
javax.xml.parsers.*;
import java.io.*;
public class DOMCountElement{
public static
void main(String[] args) {
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter File name: ");
String xmlFile = bf.readLine();
File file =
new File(xmlFile);
if
(file.exists()){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance
();
// Create the builder and parse the file

Document doc = factory.newDocumentBuilder
().parse(xmlFile);
System.out.print("Enter element name: ");
String element = bf.readLine
();
NodeList nodes = doc.getElementsByTagName(element);
System.out.println
("xml Document Contains " + nodes.getLength() + " elements."); } else{ System.out.print("File not found!"); } } catch (Exception ex) { System.out.println(ex); } } }

No comments:

Topics