Java by API/javax.xml.parsers/DocumentBuilder — различия между версиями

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

Версия 17:43, 31 мая 2010

DocumentBuilder: newDocument()

/*
 
 <?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);
  }
}





DocumentBuilder: parse(File f)

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());
    }
  }
}