DOM 解析是把文件一次性加载到内存里,生成一个树状结构,在内存中对树状节点进行修改或添加节点。所以,这种方法的缺点是消耗的内存太大。
2. 常用的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
DocumentBuilder抽象类 /** * Parse the content of the given <code>InputStream</code> as an XML * document and return a new DOM {@link Document} object. * An <code>IllegalArgumentException</code> is thrown if the * <code>InputStream</code> is null. * * @param is InputStream containing the content to be parsed. * @return <code>Document</code> result of parsing the * <code>InputStream</code> * @exception IOException If any IO errors occur. * @exception SAXException If any parse errors occur. * @see org.xml.sax.DocumentHandler */ public Document parse(InputStream is)
1 2 3 4 5 6 7 8 9 10 11 12 13
Document接口 /** * Returns a <code>NodeList</code> of all the <code>Elements</code> in * document order with a given tag name and are contained in the * document. * @param tagname The name of the tag to match on. The special value "*" * matches all tags. For XML, the <code>tagname</code> parameter is * case-sensitive, otherwise it depends on the case-sensitivity of the * markup language in use. * @return A new <code>NodeList</code> object containing all the matched * <code>Elements</code>. */ public NodeList getElementsByTagName(String tagname);
1 2 3 4 5 6 7 8 9 10 11
NodeList接口 /** * Returns the <code>index</code>th item in the collection. If * <code>index</code> is greater than or equal to the number of nodes in * the list, this returns <code>null</code>. * @param index Index into the collection. * @return The node at the <code>index</code>th position in the * <code>NodeList</code>, or <code>null</code> if that is not a valid * index. */ public Node item(int index);
1 2 3 4 5 6
Node接口 /** * A <code>NamedNodeMap</code> containing the attributes of this node (if * it is an <code>Element</code>) or <code>null</code> otherwise. */ public NamedNodeMap getAttributes();
1 2 3 4 5 6
Node接口 /** * The first child of this node. If there is no such node, this returns * <code>null</code>. */ public Node getFirstChild();
1 2 3 4 5 6 7 8 9 10 11
Node接口 /** * The value of this node, depending on its type; see the table above. * When it is defined to be <code>null</code>, setting it has no effect, * including if the node is read-only. * @exception DOMException * DOMSTRING_SIZE_ERR: Raised when it would return more characters than * fit in a <code>DOMString</code> variable on the implementation * platform. */ public String getNodeValue()throws DOMException;