javax.xml.parsers.DocumentBuilderFactory
Defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents.
产生对象树的解析器。
一般情况下:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
javax.xml.parsers.DocumentBuilder
主要用来建立Document
Method Summary
Schema getSchema()
Get a reference to the the Schema being used by the XML processor.
abstract Document newDocument()
Obtain a new instance of a DOM Document object to build a DOM tree with.
Document parse(File f)
Parse the content of the given file as an XML document and return a new DOM Document object.
abstract Document parse(InputSource is)
Parse the content of the given input source as an XML document and return a new DOM Document object.
Document parse(InputStream is)
Parse the content of the given InputStream as an XML document and return a new DOM Document object.
Document parse(InputStream is, String systemId)
Parse the content of the given InputStream as an XML document and return a new DOM Document object.
Document parse(String uri)
Parse the content of the given URI as an XML document and return a new DOM Document object.
解析并返回Document类
org.w3c.dom Interface Node
The Node interface is the primary datatype for the entire Document Object Model. It represents a single node in the document tree. While all objects implementing the Node interface expose methods for dealing with children, not all objects implementing the Node interface may have children. For example, Text nodes may not have children, and adding children to such nodes results in a DOMException being raised.
The attributes nodeName, nodeValue and attributes are included as a mechanism to get at node information without casting down to the specific derived interface. In cases where there is no obvious mapping of these attributes for a specific nodeType (e.g., nodeValue for an Element or attributes for a Comment ), this returns null. Note that the specialized interfaces may contain additional and more convenient mechanisms to get and set the relevant information.
Method Summary
Node appendChild(Node newChild)
Adds the node newChild to the end of the list of children of this node.
String getBaseURI()
The absolute base URI of this node or null if the implementation wasn’t able to obtain an absolute URI.
NodeList getChildNodes()
A NodeList that contains all children of this node.
Node removeChild(Node oldChild)
Removes the child node indicated by oldChild from the list of children, and returns it.
Node replaceChild(Node newChild, Node oldChild)
Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node.
void setNodeValue(String nodeValue)
The value of this node, depending on its type; see the table above.
void setPrefix(String prefix)
The namespace prefix of this node, or null if it is unspecified.
void setTextContent(String textContent)
This attribute returns the text content of this node and its descendants.
Object setUserData(String key, Object data, UserDataHandler handler)
Associate an object to a key on this node.
org.w3c.dom Interface Document All Superinterfaces: Node
The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document’s data.
Since elements, text nodes, comments, processing instructions, etc. cannot exist outside the context of a Document, the Document interface also contains the factory methods needed to create these objects. The Node objects created have a ownerDocument attribute which associates them with the Document within whose context they were created.
针对HTML和XML用Document对象解析
Method
Node adoptNode(Node source) 接收
Attempts to adopt a node from another document to this document.
Attr createAttribute(String name)
Creates an Attr of the given name.
Attr createAttributeNS(String namespaceURI, String qualifiedName)
Creates an attribute of the given qualified name and namespace URI.
CDATASection createCDATASection(String data)
Creates a CDATASection node whose value is the specified string.
Comment createComment(String data)
Creates a Comment node given the specified string.
DocumentFragment createDocumentFragment()
Creates an empty DocumentFragment object.
Element createElement(String tagName)
Creates an element of the type specified.
Element createElementNS(String namespaceURI, String qualifiedName)
Creates an element of the given qualified name and namespace URI.
EntityReference createEntityReference(String name)
Creates an EntityReference object.
ProcessingInstruction createProcessingInstruction(String target, String data)
Creates a ProcessingInstruction node given the specified name and data strings.
Text createTextNode(String data)
Creates a Text node given the specified string.
DocumentType getDoctype()
The Document Type Declaration (see DocumentType) associated with this document.
Element getDocumentElement()
This is a convenience attribute that allows direct access to the child node that is the document element of the document.
String getDocumentURI()
The location of the document or null if undefined or if the Document was created using DOMImplementation.createDocument.
DOMConfiguration getDomConfig()
The configuration used when Document.normalizeDocument() is invoked.
Element getElementById(String elementId)
Returns the Element that has an ID attribute with the given value.
NodeList getElementsByTagName(String tagname)
Returns a NodeList of all the Elements in document order with a given tag name and are contained in the document.
NodeList getElementsByTagNameNS(String namespaceURI, String localName)
Returns a NodeList of all the Elements with a given local name and namespace URI in document order.
DOMImplementation getImplementation()
The DOMImplementation object that handles this document.
String getInputEncoding()
An attribute specifying the encoding used for this document at the time of the parsing.
boolean getStrictErrorChecking()
An attribute specifying whether error checking is enforced or not.
String getXmlEncoding()
An attribute specifying, as part of the XML declaration, the encoding of this document.
boolean getXmlStandalone()
An attribute specifying, as part of the XML declaration, whether this document is standalone.
String getXmlVersion()
An attribute specifying, as part of the XML declaration, the version number of this document.
Node importNode(Node importedNode, boolean deep)
Imports a node from another document to this document, without altering or removing the source node from the original document; this method creates a new copy of the source node.
void normalizeDocument()
This method acts as if the document was going through a save and load cycle, putting the document in a “normal” form.
Node renameNode(Node n, String namespaceURI, String qualifiedName)
Rename an existing node of type ELEMENT_NODE or ATTRIBUTE_NODE.
void setDocumentURI(String documentURI)
The location of the document or null if undefined or if the Document was created using DOMImplementation.createDocument.
void setStrictErrorChecking(boolean strictErrorChecking)
An attribute specifying whether error checking is enforced or not.
void setXmlStandalone(boolean xmlStandalone)
An attribute specifying, as part of the XML declaration, whether this document is standalone.
void setXmlVersion(String xmlVersion)
An attribute specifying, as part of the XML declaration, the version number of this document.
e.g
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class DOMWrite {
public static void main(String[] args) throws Throwable {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.newDocument();//document包含一整个xml文档
Element root = doc.createElement("学生花名册");//元素
doc.appendChild(root);// 把这个节点追加到doc文档中
Element student = doc.createElement("学生");// 创建学生元素节点
Attr id = (Attr) doc.createAttribute("id");// 创建Id属性节点
id.setValue("1");// 给属性赋值
student.setAttributeNode(id);// 把id属性节点追加到student
root.appendChild(student);// 追加到root节点
Element name = doc.createElement("姓名");// 创建姓名元素节点
Text nameValue = doc.createTextNode("李丽");// 创建文本节点
name.appendChild(nameValue);
student.appendChild(name);
Element sex = doc.createElement("性别");// 创建姓名元素节点
Text sexValue = doc.createTextNode("女");// 创建文本节点
sex.appendChild(sexValue);
student.appendChild(sex);
FileOutputStream out = new FileOutputStream(
"C:\\Users\\Administrator\\Desktop\\out.xml");// 声明一个输出流备用
OutputStreamWriter ow = new OutputStreamWriter(out);
Transformer t = TransformerFactory.newInstance().newTransformer();
// 设置换行和缩进
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml");
t.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(
new File("C:\\Users\\Administrator\\Desktop\\out.xml"))));
}
}
DOM读
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
public class DOMDemo {
public static void main(String[] args) throws Throwable {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);
File file = new File("C:\\Users\\Administrator\\Desktop\\out.xml");
DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(file);
System.out.println(doc.getXmlVersion());
System.out.println(doc.getBaseURI());
System.out.println(doc.getInputEncoding());
Element root = (Element) doc.getDocumentElement();
NodeList nl = root.getElementsByTagName("book");
for (int i = 0; i < nl.getLength(); i++) {
Element el = (Element) nl.item(i);
System.out.print(el.getNodeName());
System.out.println(el.getAttribute("id"));
for (int j = 0; j < el.getChildNodes().getLength(); j++) {
// System.out.println(el.getAttribute("id"));
String nodename = el.getChildNodes().item(j).getNodeName();
if (!nodename.equals("#text"))
System.out.println(nodename);
System.out.println(el.getChildNodes().item(j).getTextContent()
.toString());
}
// System.out.println(el.getTextContent());
}
}
}