Create New Container : DOM Edit « XML « Java Tutorial






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.NodeList;

/*******************************************************************************
 * Copyright (C) 2007 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/

/**
 * Various XML utilities.
 * 
 * @author simonjsmith, ksim
 * @version 1.1 - ksim - March 6th, 2007 - Added functions regarding streaming
 * @version 1.2 - ksim - March 10th, 2007 - Added functions regarding DOM
 *          manipulation
 */
public class Utils {
  public static Element createNewContainer(Document document, Element parent,
      String childElement) {
    Element child = (Element) document.createElement(childElement);
    parent.appendChild(child);
    return child;
  }
}








33.3.DOM Edit
33.3.1.Creating an XML Document from a DOM Tree
33.3.2.Create Document with root QName
33.3.3.Create New Container
33.3.4.Document To String
33.3.5.Start a new XML Document
33.3.6.Adding a New Entry to the End of a List
33.3.7.A Method for Inserting a New Entry in a List
33.3.8.Locating a Node and Modifying Text
33.3.9.Locating a Node by Using Siblings
33.3.10.Deleting a Tree Node
33.3.11.Replacing a Tree Node: Replacing an Existing Node with a New One
33.3.12.Locating Elements by Tag Name
33.3.13.Duplicating a Portion of the Tree
33.3.14.Setting an Attribute
33.3.15.Deleting an Attribute
33.3.16.Moving and Copying Attributes
33.3.17.Locating a Node by an ID
33.3.18.Editing the Text of a Node
33.3.19.Modifying Text by Cutting and Pasting
33.3.20.Edit Text by Insertion and Replacement
33.3.21.Inserting a CDATASection Node
33.3.22.Normalize All of the Text in a Document
33.3.23.Moving Nodes between Documents: Copy a Node from One Parse Tree into Another
33.3.24.Editing by Using a Document Fragment: Creating a DocumentFragment Subtree and Appending to the Document
33.3.25.Inserting a Processing Instruction and Comment
33.3.26.Methods to Insert a Processing Instruction and a Comment
33.3.27.Changing the Name of a DOM Element