Java by API/org.w3c.dom/Document

Материал из Java эксперт
Перейти к: навигация, поиск

Document: createCDATASection(String data) throws DOMException

   <source lang="java">
 

import java.io.File; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.CDATASection; import org.w3c.dom.CharacterData; import org.w3c.dom.rument; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; public class Main {

 public static void main(String[] argv) throws Exception {
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   factory.setValidating(true);
   factory.setExpandEntityReferences(false);
   Document doc = factory.newDocumentBuilder().parse(new File("filename"));
   Element element = doc.getElementById("key1");
   CDATASection cdataNode = doc.createCDATASection("");
   Comment commentNode = doc.createComment("");
   Text textNode = doc.createTextNode("");

   CharacterData cdata = cdataNode;
   cdata = commentNode;
   cdata = textNode;
   cdata.setData("some data");
   int len = cdata.getLength();
 }

}


 </source>
   
  
 
  



Document: createComment(String data)

   <source lang="java">
 

import java.io.File; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.rument; import org.w3c.dom.Document; public class Main {

 public static void main(String[] argv) throws Exception {
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   factory.setValidating(true);
   factory.setExpandEntityReferences(false);
   Document doc = factory.newDocumentBuilder().parse(new File("filename"));
   Comment comment = doc.createComment("invalid -- comment");
   boolean validComment = comment.getNodeValue().indexOf("--") < 0;
 }

}


 </source>
   
  
 
  



Document: createElement(String tagName)

   <source lang="java">

/*

<?xml version="1.0" encoding="UTF-8"?>
<book id="javanut4">
   <chapter>
      <title>Chapter 1</title>
      <para/>
   </chapter>
   <chapter>
      <title>Chapter 2</title>
      <para/>
   </chapter>
   <chapter>
      <title>Chapter 3</title>
      <para/>
   </chapter>
</book>
*/

import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; 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; public class MainClass {

 public static void main(String[] args) throws Exception{
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   DocumentBuilder db = dbf.newDocumentBuilder();
   Document document = db.newDocument();
   Element book = document.createElement("book"); 
   book.setAttribute("id", "javanut4");           
   document.appendChild(book);                    
   for(int i = 1; i <= 3; i++) {                  
       Element chapter = document.createElement("chapter");
       Element title = document.createElement("title");
       title.appendChild(document.createTextNode("Chapter " + i));
       chapter.appendChild(title);
       chapter.appendChild(document.createElement("para"));
       book.appendChild(chapter);
   }
   TransformerFactory tf = TransformerFactory.newInstance();
   Transformer transformer = tf.newTransformer();
   DOMSource source = new DOMSource(document);          
   StreamResult result = new StreamResult(System.out);  
   transformer.transform(source, result);
 }

}


 </source>
   
  
 
  



Document: createProcessingInstruction(String target, String data) throws DOMException

   <source lang="java">
 

import java.io.File; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.w3c.dom.ProcessingInstruction; public class Main {

 public static void main(String[] argv) throws Exception {
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   factory.setValidating(true);
   factory.setExpandEntityReferences(false);
   Document doc = factory.newDocumentBuilder().parse(new File("filename"));
   // Add a PI at the beginning of the document
   Element element = doc.getDocumentElement();
   ProcessingInstruction pi = doc.createProcessingInstruction("target", "instruction");
   element.getParentNode().insertBefore(pi, element);
 }

}


 </source>
   
  
 
  



Document: getDoctype()

   <source lang="java">

/* DOCTYPE node: <!ELEMENT address (buildingnumber,street,city,state,zip)> <!ATTLIST address xmlns CDATA #IMPLIED> <!ELEMENT buildingnumber (#PCDATA)> <!ELEMENT street (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT state (#PCDATA)> <!ELEMENT zip (#PCDATA)>

Document body contents are:

Node: address
Node Type: Element
Child Nodes of address are:
  Node: buildingnumber
  Node Type: Element
  Child Nodes of buildingnumber are:
    Node: #text
    Node Type: Text
    Content is:  29 
  Node: street
  Node Type: Element
  Child Nodes of street are:
    Node: #text
    Node Type: Text
    Content is:  South Street
  Node: city
  Node Type: Element
  Child Nodes of city are:
    Node: #text
    Node Type: Text
    Content is: Vancouver
  Node: state
  Node Type: Element
  Child Nodes of state are:
    Node: #text
    Node Type: Text
    Content is: BC
  Node: zip
  Node Type: Element
  Child Nodes of zip are:
    Node: #text
    Node Type: Text
    Content is: V6V 4U7
*/

import static org.w3c.dom.Node.ATTRIBUTE_NODE; import static org.w3c.dom.Node.CDATA_SECTION_NODE; import static org.w3c.dom.Node.ruMENT_NODE; import static org.w3c.dom.Node.DOCUMENT_TYPE_NODE; import static org.w3c.dom.Node.ELEMENT_NODE; import static org.w3c.dom.Node.ENTITY_NODE; import static org.w3c.dom.Node.ENTITY_REFERENCE_NODE; import static org.w3c.dom.Node.NOTATION_NODE; import static org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE; import static org.w3c.dom.Node.TEXT_NODE; import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.DocumentType; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class MainClass {

 public static void main(String args[]) {
   DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
   builderFactory.setNamespaceAware(true);       // Set namespace aware
   builderFactory.setValidating(true);           // and validating parser feaures
   builderFactory.setIgnoringElementContentWhitespace(true); 
   
   DocumentBuilder builder = null;
   try {
     builder = builderFactory.newDocumentBuilder();  // Create the parser
   } catch(ParserConfigurationException e) {
     e.printStackTrace();
   }
   Document xmlDoc = null;
   try {
     xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));
   } catch(SAXException e) {
     e.printStackTrace();
   } catch(IOException e) {
     e.printStackTrace();
   }
   DocumentType doctype = xmlDoc.getDoctype();       
   if(doctype == null) {                             
     System.out.println("DOCTYPE is null");
   } else {                                          
     System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
   }
   System.out.println("\nDocument body contents are:");
   listNodes(xmlDoc.getDocumentElement(),"");         // Root element & children
 }
 static void listNodes(Node node, String indent) {
   String nodeName = node.getNodeName();
   System.out.println(indent+" Node: " + nodeName);
   short type = node.getNodeType();
   System.out.println(indent+" Node Type: " + nodeType(type));
   if(type == TEXT_NODE){
     System.out.println(indent+" Content is: "+((Text)node).getWholeText());
   }
   
   NodeList list = node.getChildNodes();       
   if(list.getLength() > 0) {                  
     System.out.println(indent+" Child Nodes of "+nodeName+" are:");
     for(int i = 0 ; i<list.getLength() ; i++) {
       listNodes(list.item(i),indent+"  ");     
     }
   }         
 }
 static String nodeType(short type) {
   switch(type) {
     case ELEMENT_NODE:                return "Element";
     case DOCUMENT_TYPE_NODE:          return "Document type";
     case ENTITY_NODE:                 return "Entity";
     case ENTITY_REFERENCE_NODE:       return "Entity reference";
     case NOTATION_NODE:               return "Notation";
     case TEXT_NODE:                   return "Text";
     case COMMENT_NODE:                return "Comment";
     case CDATA_SECTION_NODE:          return "CDATA Section";
     case ATTRIBUTE_NODE:              return "Attribute";
     case PROCESSING_INSTRUCTION_NODE: return "Attribute";
   }
   return "Unidentified";
 }
 static String xmlString ="<?xml version=\"1.0\"?>" +
     "  <!DOCTYPE address" +
     "  [" +
     "     <!ELEMENT address (buildingnumber, street, city, state, zip)>" +
     "     <!ATTLIST address xmlns CDATA #IMPLIED>" +
     "     <!ELEMENT buildingnumber (#PCDATA)>" +
     "     <!ELEMENT street (#PCDATA)>" +
     "     <!ELEMENT city (#PCDATA)>" +
     "     <!ELEMENT state (#PCDATA)>" +
     "     <!ELEMENT zip (#PCDATA)>" +
     "  ]>" +
     "" +
     "  <address>" +
     "    <buildingnumber> 29 </buildingnumber>" +
     "    <street> South Street</street>" +
     "    <city>Vancouver</city>" +
     "" +
     "    <state>BC</state>" +
     "    <zip>V6V 4U7</zip>" +
     "  </address>";

}


 </source>
   
  
 
  



Document: getDocumentElement()

   <source lang="java">

/* DOCTYPE node: <!ELEMENT address (buildingnumber,street,city,state,zip)> <!ATTLIST address xmlns CDATA #IMPLIED> <!ELEMENT buildingnumber (#PCDATA)> <!ELEMENT street (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT state (#PCDATA)> <!ELEMENT zip (#PCDATA)>

Document body contents are:

Node: address
Node Type: Element
Child Nodes of address are:
  Node: buildingnumber
  Node Type: Element
  Child Nodes of buildingnumber are:
    Node: #text
    Node Type: Text
    Content is:  29 
  Node: street
  Node Type: Element
  Child Nodes of street are:
    Node: #text
    Node Type: Text
    Content is:  South Street
  Node: city
  Node Type: Element
  Child Nodes of city are:
    Node: #text
    Node Type: Text
    Content is: Vancouver
  Node: state
  Node Type: Element
  Child Nodes of state are:
    Node: #text
    Node Type: Text
    Content is: BC
  Node: zip
  Node Type: Element
  Child Nodes of zip are:
    Node: #text
    Node Type: Text
    Content is: V6V 4U7
*/

import static org.w3c.dom.Node.ATTRIBUTE_NODE; import static org.w3c.dom.Node.CDATA_SECTION_NODE; import static org.w3c.dom.Node.ruMENT_NODE; import static org.w3c.dom.Node.DOCUMENT_TYPE_NODE; import static org.w3c.dom.Node.ELEMENT_NODE; import static org.w3c.dom.Node.ENTITY_NODE; import static org.w3c.dom.Node.ENTITY_REFERENCE_NODE; import static org.w3c.dom.Node.NOTATION_NODE; import static org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE; import static org.w3c.dom.Node.TEXT_NODE; import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.DocumentType; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class MainClass {

 public static void main(String args[]) {
   DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
   builderFactory.setNamespaceAware(true);       // Set namespace aware
   builderFactory.setValidating(true);           // and validating parser feaures
   builderFactory.setIgnoringElementContentWhitespace(true); 
   
   DocumentBuilder builder = null;
   try {
     builder = builderFactory.newDocumentBuilder();  // Create the parser
   } catch(ParserConfigurationException e) {
     e.printStackTrace();
   }
   Document xmlDoc = null;
   try {
     xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));
   } catch(SAXException e) {
     e.printStackTrace();
   } catch(IOException e) {
     e.printStackTrace();
   }
   DocumentType doctype = xmlDoc.getDoctype();       
   if(doctype == null) {                             
     System.out.println("DOCTYPE is null");
   } else {                                          
     System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
   }
   System.out.println("\nDocument body contents are:");
   listNodes(xmlDoc.getDocumentElement(),"");         // Root element & children
 }
 static void listNodes(Node node, String indent) {
   String nodeName = node.getNodeName();
   System.out.println(indent+" Node: " + nodeName);
   short type = node.getNodeType();
   System.out.println(indent+" Node Type: " + nodeType(type));
   if(type == TEXT_NODE){
     System.out.println(indent+" Content is: "+((Text)node).getWholeText());
   }
   
   NodeList list = node.getChildNodes();       
   if(list.getLength() > 0) {                  
     System.out.println(indent+" Child Nodes of "+nodeName+" are:");
     for(int i = 0 ; i<list.getLength() ; i++) {
       listNodes(list.item(i),indent+"  ");     
     }
   }         
 }
 static String nodeType(short type) {
   switch(type) {
     case ELEMENT_NODE:                return "Element";
     case DOCUMENT_TYPE_NODE:          return "Document type";
     case ENTITY_NODE:                 return "Entity";
     case ENTITY_REFERENCE_NODE:       return "Entity reference";
     case NOTATION_NODE:               return "Notation";
     case TEXT_NODE:                   return "Text";
     case COMMENT_NODE:                return "Comment";
     case CDATA_SECTION_NODE:          return "CDATA Section";
     case ATTRIBUTE_NODE:              return "Attribute";
     case PROCESSING_INSTRUCTION_NODE: return "Attribute";
   }
   return "Unidentified";
 }
 static String xmlString ="<?xml version=\"1.0\"?>" +
     "  <!DOCTYPE address" +
     "  [" +
     "     <!ELEMENT address (buildingnumber, street, city, state, zip)>" +
     "     <!ATTLIST address xmlns CDATA #IMPLIED>" +
     "     <!ELEMENT buildingnumber (#PCDATA)>" +
     "     <!ELEMENT street (#PCDATA)>" +
     "     <!ELEMENT city (#PCDATA)>" +
     "     <!ELEMENT state (#PCDATA)>" +
     "     <!ELEMENT zip (#PCDATA)>" +
     "  ]>" +
     "" +
     "  <address>" +
     "    <buildingnumber> 29 </buildingnumber>" +
     "    <street> South Street</street>" +
     "    <city>Vancouver</city>" +
     "" +
     "    <state>BC</state>" +
     "    <zip>V6V 4U7</zip>" +
     "  </address>";

}


 </source>
   
  
 
  



Document: getElementsByTagName(String tagname)

   <source lang="java">

import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class MainClass {

 public static void main(String[] args) throws IOException, ParserConfigurationException,
     org.xml.sax.SAXException {
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   factory.setIgnoringComments(true);
   factory.setCoalescing(true); // Convert CDATA to Text nodes
   factory.setNamespaceAware(false); // No namespaces: this is default
   factory.setValidating(false); // Don"t validate DTD: also default
   DocumentBuilder parser = factory.newDocumentBuilder();
   Document document = parser.parse(new File(args[0]));
   NodeList sections = document.getElementsByTagName("sect1");
   int numSections = sections.getLength();
   for (int i = 0; i < numSections; i++) {
     Element section = (Element) sections.item(i); // A <sect1>
     Node title = section.getFirstChild();
     while (title != null && title.getNodeType() != Node.ELEMENT_NODE)
       title = title.getNextSibling();
     if (title != null)
       System.out.println(title.getFirstChild().getNodeValue());
   }
 }

}


 </source>