DOM helper for root element : Node « XML « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » XML » Node 
33. 29. 28. DOM helper for root element
/**
 
 */
//org.ajax4jsf.builder.xml;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;


import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * This class must read XML file from input stream and can extract body of root
 * element for include into target in generation.
 
 @author shura
 
 */
public class XMLBody {
  private Document xmlDocument;

  private Element rootElement;

  /**
   * Load XML document and parse it into DOM.
   
   @param input
   @throws ParsingException
   */
  public void loadXML(InputStream inputthrows Exception {
    try {
      // Create Document Builder Factory
      DocumentBuilderFactory docFactory = DocumentBuilderFactory
          .newInstance();

      docFactory.setValidating(false);
      // Create Document Builder
      DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
      
      docBuilder.isValidating();
      
      // Disable loading of external Entityes
      docBuilder.setEntityResolver(new EntityResolver() {
        // Dummi resolver - alvays do nothing
        public InputSource resolveEntity(String publicId, String systemId)
            throws SAXException, IOException {
          return new InputSource(new StringReader(""));
        }

      });

      // open and parse XML-file
      xmlDocument = docBuilder.parse(input);

      // Get Root xmlElement
      rootElement = xmlDocument.getDocumentElement();
    catch (Exception e) {
      throw new Exception("Error load XML ", e);
    }

  }

  /**
   * Check name of root element is as expected.
   
   @param name
   @return
   */
  public boolean isRootName(String name) {
    return rootElement.getNodeName().equals(name);
  }

  public String getDoctype() {
    DocumentType doctype = xmlDocument.getDoctype();
    if (null != doctype) {
      return doctype.getName();
    }
    return null;
  }

  public String getPiblicId() {
    DocumentType doctype = xmlDocument.getDoctype();
    if (null != doctype) {
      return doctype.getPublicId();
    }
    return null;
  }

  public String getRootTypeName() {
    return rootElement.getSchemaTypeInfo().getTypeName();
  }

  public String getContent() throws Exception {
    NodeList childNodes = rootElement.getChildNodes();
    return serializeNodes(childNodes);
  }

  private String serializeNodes(NodeList childNodesthrows Exception {
    DocumentFragment fragment = xmlDocument.createDocumentFragment();
    for (int i = 0; i < childNodes.getLength(); i++) {
      fragment.appendChild(childNodes.item(i).cloneNode(true));
    }
    try {
      TransformerFactory transformerFactory = TransformerFactory
          .newInstance();
      Transformer transformer = transformerFactory.newTransformer();
      transformer.setOutputProperty("omit-xml-declaration""yes");
      StringWriter out = new StringWriter();
      StreamResult result = new StreamResult(out);
      transformer.transform(new DOMSource(fragment), result);
      return out.toString();

    catch (Exception e) {
      throw new Exception(e);
    }
  }
  
  public String getContent(String xpaththrows Exception{
    XPath path = XPathFactory.newInstance().newXPath();
    NodeList childNodes;
    try {
      childNodes = (NodeListpath.evaluate(xpath, xmlDocument,XPathConstants.NODESET);
    catch (XPathExpressionException e) {
      throw new Exception("Error evaluate xpath",e);
    }
    return serializeNodes(childNodes);
  }
}
33. 29. Node
33. 29. 1. Add a text node to the element
33. 29. 2. Add a text node to the beginning of the element
33. 29. 3. Add a text node before the last child of the element
33. 29. 4. Add a text node in front of the new item element
33. 29. 5. Adding a Text Node to a DOM Document
33. 29. 6. Removing a Node from a DOM Document
33. 29. 7. Remove All nodes
33. 29. 8. Get the text node
33. 29. 9. Change a particular node in XML
33. 29. 10. Add another element after the first child of the root element
33. 29. 11. Create a new element and move the middle text node to it
33. 29. 12. Move all children of the element in front of the element
33. 29. 13. Split the node at the beginning of the word
33. 29. 14. Compare two DOM Nodes from JBoss
33. 29. 15. Convert NodeList To Node Array
33. 29. 16. Convert node element To String
33. 29. 17. Search our next siblings for a given node
33. 29. 18. Search earlier siblings for a given node
33. 29. 19. Returns a first child DOM Node of type ELEMENT_NODE for the specified Node
33. 29. 20. Search up the tree for a given node
33. 29. 21. Get the first text node associated with this element
33. 29. 22. Simplified implementation of a Node from a Document Object Model (DOM)
33. 29. 23. Returns a list of value for the given node
33. 29. 24. Returns the value of the child node with the given name
33. 29. 25. Remove this node from its parent.
33. 29. 26. Find the first text descendent node of an element
33. 29. 27. Copies the source tree into the specified place in a destination tree.
33. 29. 28. DOM helper for root element
33. 29. 29. Compare two DOM Nodes
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.