Java by API/javax.xml.parsers/DocumentBuilderFactory

Материал из Java эксперт
Версия от 14:22, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

DocumentBuilderFactory: newDocumentBuilder()

 
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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
  static public void main(String[] arg) {
    boolean validate = false;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);
    Document doc = null;
    try {
      DocumentBuilder builder = dbf.newDocumentBuilder();
      doc = builder.parse(new InputSource(new StringReader(xmlString)));
    } catch (SAXException e) {
      System.exit(1);
    } catch (ParserConfigurationException e) {
      System.err.println(e);
      System.exit(1);
    } catch (IOException e) {
      System.err.println(e);
      System.exit(1);
    }
    TreeDumper td = new TreeDumper();
    td.dump(doc);
  }
  static String xmlString = "<PHONEBOOK>" +
  "  <PERSON>" +
  "   <NAME >Joe Wang</NAME>" +
  "   <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
  "   <TELEPHONE>202-999-9999</TELEPHONE>" +
  "   <WEB>www.jexp.ru</WEB>" +
  "  </PERSON>" +
  "  <PERSON>   " +
  "<NAME>Karol</NAME>" +    
  "   <EMAIL>karol@yourserver.ru</EMAIL>" +
  "   <TELEPHONE>306-999-9999</TELEPHONE>" +
  "   <WEB>www.jexp.ru</WEB>" +
  "  </PERSON>" +
  "  <PERSON>" +
  "   <NAME>Green</NAME>" +
  "   <EMAIL>green@yourserver.ru</EMAIL>" +
  "   <TELEPHONE>202-414-9999</TELEPHONE>" +
  "   <WEB>www.jexp.ru</WEB>" +
  "  </PERSON>" +
  "  </PHONEBOOK>";
}
class TreeDumper {
    public void dump(Document doc) {
        dumpLoop((Node)doc,"");
    }
    private void dumpLoop(Node node,String indent) {
        switch(node.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
            System.out.println(indent + "CDATA_SECTION_NODE");
            break;
        case Node.ruMENT_NODE:
            System.out.println(indent + "COMMENT_NODE");
            break;
        case Node.DOCUMENT_FRAGMENT_NODE:
            System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
            break;
        case Node.DOCUMENT_NODE:
            System.out.println(indent + "DOCUMENT_NODE");
            break;
        case Node.DOCUMENT_TYPE_NODE:
            System.out.println(indent + "DOCUMENT_TYPE_NODE");
            break;
        case Node.ELEMENT_NODE:
            System.out.println(indent + "ELEMENT_NODE");
            break;
        case Node.ENTITY_NODE:
            System.out.println(indent + "ENTITY_NODE");
            break;
        case Node.ENTITY_REFERENCE_NODE:
            System.out.println(indent + "ENTITY_REFERENCE_NODE");
            break;
        case Node.NOTATION_NODE:
            System.out.println(indent + "NOTATION_NODE");
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
            break;
        case Node.TEXT_NODE:
            System.out.println(indent + "TEXT_NODE");
            break;
        default:
            System.out.println(indent + "Unknown node");
            break;
        }
        NodeList list = node.getChildNodes();
        for(int i=0; i<list.getLength(); i++)
            dumpLoop(list.item(i),indent + "   ");
    }
}





DocumentBuilderFactory: newInstance()

 
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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
  static public void main(String[] arg) {
    boolean validate = false;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);
    Document doc = null;
    try {
      DocumentBuilder builder = dbf.newDocumentBuilder();
      doc = builder.parse(new InputSource(new StringReader(xmlString)));
    } catch (SAXException e) {
      System.exit(1);
    } catch (ParserConfigurationException e) {
      System.err.println(e);
      System.exit(1);
    } catch (IOException e) {
      System.err.println(e);
      System.exit(1);
    }
    TreeDumper td = new TreeDumper();
    td.dump(doc);
  }
  static String xmlString = "<PHONEBOOK>" +
  "  <PERSON>" +
  "   <NAME >Joe Wang</NAME>" +
  "   <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
  "   <TELEPHONE>202-999-9999</TELEPHONE>" +
  "   <WEB>www.jexp.ru</WEB>" +
  "  </PERSON>" +
  "  <PERSON>   " +
  "<NAME>Karol</NAME>" +    
  "   <EMAIL>karol@yourserver.ru</EMAIL>" +
  "   <TELEPHONE>306-999-9999</TELEPHONE>" +
  "   <WEB>www.jexp.ru</WEB>" +
  "  </PERSON>" +
  "  <PERSON>" +
  "   <NAME>Green</NAME>" +
  "   <EMAIL>green@yourserver.ru</EMAIL>" +
  "   <TELEPHONE>202-414-9999</TELEPHONE>" +
  "   <WEB>www.jexp.ru</WEB>" +
  "  </PERSON>" +
  "  </PHONEBOOK>";
}
class TreeDumper {
    public void dump(Document doc) {
        dumpLoop((Node)doc,"");
    }
    private void dumpLoop(Node node,String indent) {
        switch(node.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
            System.out.println(indent + "CDATA_SECTION_NODE");
            break;
        case Node.ruMENT_NODE:
            System.out.println(indent + "COMMENT_NODE");
            break;
        case Node.DOCUMENT_FRAGMENT_NODE:
            System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
            break;
        case Node.DOCUMENT_NODE:
            System.out.println(indent + "DOCUMENT_NODE");
            break;
        case Node.DOCUMENT_TYPE_NODE:
            System.out.println(indent + "DOCUMENT_TYPE_NODE");
            break;
        case Node.ELEMENT_NODE:
            System.out.println(indent + "ELEMENT_NODE");
            break;
        case Node.ENTITY_NODE:
            System.out.println(indent + "ENTITY_NODE");
            break;
        case Node.ENTITY_REFERENCE_NODE:
            System.out.println(indent + "ENTITY_REFERENCE_NODE");
            break;
        case Node.NOTATION_NODE:
            System.out.println(indent + "NOTATION_NODE");
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
            break;
        case Node.TEXT_NODE:
            System.out.println(indent + "TEXT_NODE");
            break;
        default:
            System.out.println(indent + "Unknown node");
            break;
        }
        NodeList list = node.getChildNodes();
        for(int i=0; i<list.getLength(); i++)
            dumpLoop(list.item(i),indent + "   ");
    }
}





DocumentBuilderFactory: setCoalescing(boolean coalescing)

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





DocumentBuilderFactory: setExpandEntityReferences(boolean expandEntityRef)

  

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"));
    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction("target", "instruction");
    NodeList list = doc.getElementsByTagName("entry");
    for (int i = 0; i < list.getLength(); i++) {
      element = (Element) list.item(i);
      pi = doc.createProcessingInstruction("target", "instruction=" + i);
      element.appendChild(pi);
    }
  }
}





DocumentBuilderFactory: setIgnoringComments(boolean ignoreComments)

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