DOM Utilities : DOM Action « XML « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » XML » DOM Action 




DOM Utilities
 


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 TestDOM {
  public static void main(String[] argsthrows Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = factory.newDocumentBuilder();
    Document document = parser.parse("zooinventory.xml");
    Element inventory = document.getDocumentElement();
    NodeList animals = inventory.getElementsByTagName("Animal");

    System.out.println("Animals = ");
    for (int i = 0; i < animals.getLength(); i++) {
      String name = DOMUtil.getSimpleElementText((Elementanimals.item(i)"Name");
      String species = DOMUtil.getSimpleElementText((Elementanimals.item(i)"Species");
      System.out.println("  " + name + " (" + species + ")");
    }

    Element foodRecipe = DOMUtil.getFirstElement((Elementanimals.item(1)"FoodRecipe");
    String name = DOMUtil.getSimpleElementText(foodRecipe, "Name");
    System.out.println("Recipe = " + name);
    NodeList ingredients = foodRecipe.getElementsByTagName("Ingredient");
    for (int i = 0; i < ingredients.getLength(); i++)
      System.out.println("  " + DOMUtil.getSimpleElementText((Elementingredients.item(i)));
  }
}

class DOMUtil {
  public static Element getFirstElement(Element element, String name) {
    NodeList nl = element.getElementsByTagName(name);
    if (nl.getLength() 1)
      throw new RuntimeException("Element: " + element + " does not contain: " + name);
    return (Elementnl.item(0);
  }

  public static String getSimpleElementText(Element node, String name) {
    Element namedElement = getFirstElement(node, name);
    return getSimpleElementText(namedElement);
  }

  public static String getSimpleElementText(Element node) {
    StringBuffer sb = new StringBuffer();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
      Node child = children.item(i);
      if (child instanceof Text)
        sb.append(child.getNodeValue());
    }
    return sb.toString();
  }
}

//File: zooinventory.xml
/*<?xml version="1.0" encoding="UTF-8"?>

<Element2>
  <Element3 animalClass="mammal">
    <Name>A</Name>
    <Species>B</Species>
    <Habitat>C</Habitat>
    <Food>D</Food>
    <Temperament>E</Temperament>
    <Weight>1</Weight>
  </Element3>
  <Element3 animalClass="mammal">
    <Name>F</Name>
    <Species>G</Species>
    <Habitat>H</Habitat>
    <ElementA>
      <Name>I</Name>
      <Ingredient>I1</Ingredient>
      <Ingredient>I2</Ingredient>
      <Ingredient>I2</Ingredient>
    </ElementA>
    <Temperament>J</Temperament>
    <Weight>4</Weight>
  </Element3>
</Element2>
*/

   
  














Related examples in the same category
1.DOM parser with all its actions displayed
2.Access Document type
3.Custom complex filters for selecting nodes
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.