Java by API/org.w3c.dom/Element

Материал из Java эксперт
Версия от 14:15, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Element: getElementsByTagName(String name)

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class MainClass {
  public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("yourFile.xml");
    Element rootElement = doc.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    Node current = null;
    int count = children.getLength();
    for (int i = 0; i < count; i++) {
      current = children.item(i);
      if (current.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) current;
        if (element.getTagName().equalsIgnoreCase("tableOfContents")) {
          // Get the list of <tocEntry> items
          NodeList tocitems = element.getElementsByTagName("tocEntry");
          // Obtain a reference to the second one
          Node secondChild = tocitems.item(1);
          // Create a new <tocEntry> element
          Element newTOCItem = doc.createElement("tocEntry");
          // Create a new "Help" text node
          Text newText = doc.createTextNode("Help");
          // Make it a child of the new <tocEntry> element
          // <tocEntry>Help</tocEntry>
          newTOCItem.appendChild(newText);
          // Add the new <tocEntry> element to <tableOfContents>
          element.insertBefore(newTOCItem, secondChild);
        }
      }
    }
    System.out.println(doc.getDocumentElement());
  }
}





Element: NodeList getChildNodes()

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class MainClass {
  public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("yourFile.xml");
    Element rootElement = doc.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    Node current = null;
    int count = children.getLength();
    for (int i = 0; i < count; i++) {
      current = children.item(i);
      if (current.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) current;
        if (element.getTagName().equalsIgnoreCase("tableOfContents")) {
          element.setAttribute("showPageNumbers", "no");
        }
      }
    }
    System.out.println(doc.getDocumentElement());
  }
}





Element: setAttribute(String name, String value)

/*
 
 <?xml version="1.0" encoding="UTF-8"?>
 <book id="javanut4">
    <chapter>
       <title>Chapter 1</title>
       <para/>
    </chapter>
    <chapter>
       <title>Chapter 2</title>
       <para/>
    </chapter>
    <chapter>
       <title>Chapter 3</title>
       <para/>
    </chapter>
 </book>
 */
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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.Document;
import org.w3c.dom.Element;
public class MainClass {
  public static void main(String[] args) throws Exception{
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.newDocument();
    Element book = document.createElement("book"); 
    book.setAttribute("id", "javanut4");           
    document.appendChild(book);                    
    for(int i = 1; i <= 3; i++) {                  
        Element chapter = document.createElement("chapter");
        Element title = document.createElement("title");
        title.appendChild(document.createTextNode("Chapter " + i));
        chapter.appendChild(title);
        chapter.appendChild(document.createElement("para"));
        book.appendChild(chapter);
    }
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(document);          
    StreamResult result = new StreamResult(System.out);  
    transformer.transform(source, result);
  }
}





Element: String getTagName()

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class MainClass {
  public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("yourFile.xml");
    Element rootElement = doc.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    Node current = null;
    for (int i = 0; i < children.getLength(); i++) {
      current = children.item(i);
      if (current.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) current;
        if (element.getTagName().equalsIgnoreCase("tableOfContents")) {
          rootElement.removeChild(element);
        }
      }
    }
    System.out.println(doc.getDocumentElement());
  }
}