close

Вход

Забыли?

вход по аккаунту

?

More DOM

код для вставкиСкачать
More DOM
30-Sep-14
Manipulating DOM trees
пЃ®
пЃ®
DOM, unlike SAX, gives you the ability to create and modify
XML trees
There are a few roadblocks along the way
пЃ®
пЃ®
пЃ®
пЃ®
Practically everything in the JAXP implementation is an interface, with a
few abstract classes
Interfaces, such as Node, don’t have constructors; this makes it hard to get
started
Since DOM was designed to be applicable from a number of languages,
many things are not done “the Java way”
Once you get past these problems, the individual methods are
pretty straightforward
пЃ®
Even with straightforward methods, working with trees is seldom simple
Overview
пЃ®
There are three basic kinds of operations:
пЃ®
пЃ®
пЃ®
пЃ®
Creating a new DOM
Modifying the structure of a DOM
Modifying the content of a DOM
Creating a new DOM requires a few extra methods just
to get started
пЃ®
Afterwards, you can “grow” the DOM by modifying its
structure and content
Creating a DOM
import javax.xml.parsers.*;
import org.w3c.dom.Document;
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder =
factory.newDocumentBuilder();
Document doc = builder.newDocument();
}
catch (ParserConfigurationException e) { ... }
The rest of the methods
пЃ®
Most of the other methods are either instance methods
of a Document object, or are inherited from Node
пЃ®
пЃ®
пЃ®
A few are from Attr, Text, Entity, and so on
Almost all of these methods may throw a
DOMException
I’ll just go through some of the more important methods
briefly
пЃ®
The details can be looked up in the API if needed
Creating structure
пЃ®
The following are instance methods of Document:
пЃ®
пЃ®
пЃ®
пЃ®
пЃ®
пЃ®
пЃ®
public Element createElement(String tagName)
public Element createElementNS(String namespaceURI,
String qualifiedName)
public Attr createAttribute(String name)
public Attr createAttributeNS(String namespaceURI,
String qualifiedName)
public ProcessingInstruction createProcessingInstruction
(String target, String data)
public EntityReference createEntityReference(String name)
The above may all throw a DOMException
пЃ®
пЃ®
public Text createTextNode(String data)
public Comment createComment(String data)
Methods inherited from Node
пЃ®
пЃ®
пЃ®
пЃ®
пЃ®
public Node appendChild(Node newChild)
public Node insertBefore(Node newChild, Node refChild)
public Node removeChild(Node oldChild)
public Node replaceChild(Node newChild, Node oldChild)
setNodeValue(String nodeValue)
пЃ®
пЃ®
пЃ®
What this does depends on the type of node
public void setPrefix(String prefix)
public void normalize()
пЃ®
Combines adjacent TextNodes
Methods of Element
пЃ®
пЃ®
пЃ®
пЃ®
пЃ®
пЃ®
пЃ®
public void setAttribute(String name, String value)
public Attr setAttributeNode(Attr newAttr)
public void setAttributeNodeNS(String namespaceURI,
String qualifiedName,
String value)
public Attr setAttributeNodeNS(Attr newAttr)
public void removeAttribute(String name)
public void removeAttributeNS(String namespaceURI,
String localName)
public Attr removeAttributeNode(Attr oldAttr)
Method of Attr
пЃ®
пЃ®
public void setValue(String value)
This is the only method that modifies an Attr; the rest
just get information
Writing out the DOM as XML
пЃ®
пЃ®
There are no Java-supplied methods for writing out a
DOM as XML
Writing out a DOM is conceptually simple—it’s just a
tree walk
пЃ®
However, there are a lot of details—various node types and so
on—so doing a good job isn’t complicated, but it is lengthy
The End
Документ
Категория
Презентации
Просмотров
6
Размер файла
72 Кб
Теги
1/--страниц
Пожаловаться на содержимое документа