More DOMManipulating DOM treesOverviewCreating a DOMThe rest of the methodsCreating structureMethods inherited from NodeMethods of ElementMethod of AttrWriting out the DOM as XMLThe EndJan 14, 2019More DOMManipulating DOM treesDOM, unlike SAX, gives you the ability to create and modify XML treesThere are a few roadblocks along the wayPractically everything in the JAXP implementation is an interface, with a few abstract classesInterfaces, such as Node, don’t have constructors; this makes it hard to get startedSince 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 straightforwardEven with straightforward methods, working with trees is seldom simpleOverviewThere are three basic kinds of operations:Creating a new DOMModifying the structure of a DOMModifying the content of a DOMCreating a new DOM requires a few extra methods just to get startedAfterwards, you can “grow” the DOM by modifying its structure and contentCreating a DOMimport 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 methodsMost of the other methods are either instance methods of a Document object, or are inherited from NodeA few are from Attr, Text, Entity, and so onAlmost all of these methods may throw a DOMExceptionI’ll just go through some of the more important methods brieflyThe details can be looked up in the API if neededCreating structureThe 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 DOMExceptionpublic Text createTextNode(String data)public Comment createComment(String data)Methods inherited from Nodepublic 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 nodepublic void setPrefix(String prefix)public void normalize()Combines adjacent TextNodesMethods of Elementpublic 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 Attrpublic void setValue(String value)This is the only method that modifies an Attr; the rest just get informationWriting out the DOM as XMLThere are no Java-supplied methods for writing out a DOM as XMLWriting out a DOM is conceptually simple—it’s just a tree walkHowever, there are a lot of details—various node types and so on—so doing a good job isn’t complicated, but it is lengthyThe
View Full Document