XML DOM Utilities : DOM « XML « Java






XML DOM Utilities

     
import java.io.OutputStream;
import java.io.Writer;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

//<p></p><p></p><p></p><p></p>
public class XMLUtils {

  public static DocumentBuilder getDOMBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    return dbf.newDocumentBuilder();
  }

  public static Transformer getTransformer() throws TransformerConfigurationException {
    TransformerFactory tf = TransformerFactory.newInstance();
    return tf.newTransformer();
  }

  public static Document createDocument() throws ParserConfigurationException {
    return XMLUtils.getDOMBuilder().newDocument();
  }

  public static Element firstChildElement(Node node) {
    for (Node tempNode = node.getFirstChild(); tempNode != null; tempNode = tempNode
        .getNextSibling()) {
      if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) tempNode;
      }
    }
    return null;
  }

  public static Element nextSiblingElement(Node node) {
    for (Node tempNode = node.getNextSibling(); tempNode != null; tempNode = tempNode
        .getNextSibling()) {
      if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) tempNode;
      }
    }
    return null;
  }

  public static Element lastChildElement(Node node) {
    for (Node tempNode = node.getLastChild(); tempNode != null; tempNode = tempNode
        .getPreviousSibling()) {
      if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) tempNode;
      }
    }
    return null;
  }

  public static Element previousSiblingElement(Node node) {
    for (Node tempNode = node.getPreviousSibling(); tempNode != null; tempNode = tempNode
        .getPreviousSibling()) {
      if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) tempNode;
      }
    }
    return null;
  }

  public static Vector getAllChildElements(Node node) {
    Vector v = new Vector();
    Node child = XMLUtils.firstChildElement(node);
    while (child != null) {
      v.add(child);
      child = XMLUtils.nextSiblingElement(child);
    }
    return v;
  }

  public static Vector getChildElementsByNS(Node node, String uri, String localName) {
    Vector childElements = XMLUtils.getAllChildElements(node);
    Vector v = new Vector();
    for (int i = 0; i < childElements.size(); i++) {
      Element child = (Element) childElements.get(i);
      String ln = child.getLocalName();
      String u = child.getNamespaceURI();
      if (uri == null) {
        if (ln.equals(localName)) {
          v.add(child);
        }
      } else {
        if (ln.equals(localName) && u.equals(uri)) {
          v.add(child);
        }
      }
    }
    return v;
  }

  public static String getLocalName(String nodeName) {
    int index = nodeName.lastIndexOf(58);
    if (index == -1) {
      return nodeName;
    } else {
      return nodeName.substring(index + 1, nodeName.length());
    }
  }

  public static String getPrefix(String nodeName) {
    int index = nodeName.lastIndexOf(58);
    if (index == -1) {
      return null;
    } else {
      return nodeName.substring(0, index);
    }
  }

  public static void outDOMNode(Node node, Writer writer) throws TransformerConfigurationException,
      TransformerException {
    Transformer transformer = XMLUtils.getTransformer();
    Source source = new DOMSource(node);
    Result result = new StreamResult(writer);
    transformer.transform(source, result);
  }

  public static void outDOMNode(Node node, OutputStream os)
      throws TransformerConfigurationException, TransformerException {
    Transformer transformer = XMLUtils.getTransformer();
    Source source = new DOMSource(node);
    Result result = new StreamResult(os);
    transformer.transform(source, result);
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Parsing a Document Using JAXP
2.XML Document information by DOM
3.Using DOM for Syntax Checking
4.Using the DOM Parser to Build a Document TreeUsing the DOM Parser to Build a Document Tree
5.DOM FeaturesDOM Features
6.DOM level 2 EventsDOM level 2 Events
7.Searching through a document
8.Check a vendor's DOM implementationCheck a vendor's DOM implementation
9.Make up and write an XML document, using DOMMake up and write an XML document, using DOM
10.Creating XML Document using DOM
11.Loading an XML Document using DOM
12.Parse an XML string: Using DOM and a StringReader.
13.Create an XML document with DOM
14.Extracting an XML formatted string out of a DOM object
15.Reading an XML Document and create user-defined object from DOM
16.Visiting All the Nodes in a DOM Document
17.Generating SAX Parsing Events by Traversing a DOM Document
18.Converting an XML Fragment into a DOM Fragment
19.A utility class which provides methods for working with a W3C DOM
20.Convenience methods for working with the DOM API
21.DOM Utils
22.Utilities to read DOM
23.W3C DOM utility methods
24.Read XML as DOM
25.Utility method for parsing the XML with DOM
26.Handles DOM processing allowing the reading and writing of hierarchical structures as XML files.
27.Xml Utils for dom4j
28.DocWriter has a static method for writing XML documents with a writer