Declared Entities

In this chapter you will learn:

  1. How to get and display Declared Entities using DOM parser

Display Declared Entities

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
/* ja v  a  2 s  . c  o  m*/
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Entity;
import org.w3c.dom.EntityReference;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Main {
  public static void main(String[] argv) throws Exception {
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));
    
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    
    Document doc = db.parse(is);
    Map<String, Node> entityValues = new HashMap<String, Node>();
    getEntityValues(doc, entityValues);

    NamedNodeMap entities = doc.getDoctype().getEntities();
    for (int i = 0; i < entities.getLength(); i++) {
      Entity entity = (Entity) entities.item(i);
      System.out.println(entity);
      String entityName = entity.getNodeName();
      System.out.println(entityName);
      String entityPublicId = entity.getPublicId();
      System.out.println(entityPublicId);
      String entitySystemId = entity.getSystemId();
      System.out.println(entitySystemId);
      Node entityValue = (Node) entityValues.get(entityName);
      System.out.println(entityValue);
    }
  }

  public static void getEntityValues(Node node, Map<String, Node> map) {
    if (node instanceof EntityReference) {
      map.put(node.getNodeName(), node);
    }
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
      getEntityValues(list.item(i), map);
    }

  }
  static String xmlRecords = 
      "<!DOCTYPE sgml ["+
      "<!ENTITY publisher 'java2s.com'>"+
      "]>"+
      "<data>" +
      "  <employee>" +
      "    <name>Tom</name>"+ 
      "    <title>Manager</title>" +
      "  </employee>" +
      "  <employee>" +
      "    <name>&publisher;</name>"+ 
      "    <title>Programmer</title>" +
      "  </employee>" +
      "</data>";
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Intercepting All Accesses to External Entities During XML SAX Parsing
Home » Java Tutorial » XML

DOM Parser

    DOM
    Parse XML string
    Parse XML file with DOM Parser
    DOM Parser Error
    Output DOM Document to console
    Save DOM Document to a file
    Format a DOM Document
    Output Document to String
    Visit all elements
    Element retrieve by tag name
    Element character data retrieve
    DOM node visiting
    Node type
    DOM node dump
    Children nodes
    Root Element
    Doc Type
    Declared Entities
    External Entities resolve
    DOMImplementation

DOM Edit

    Element text child data
    Element remove
    Add text node to element
    Element text data update
    Add attribute to element
    Attribute remove
    Create new empty element
    Element clone
    CDATASection
    Comments element
    Processing Instruction
    New XML Document

SAX Parser

    SAX Parser action
    Parse an XML file with SAX parser
    SAX Parser based XML Document dump
    SAX Parser Error Handler
    SAX Parser content handler
    SAX Locator

Utility

    Encode String for XML attribute
    Convert Source to InputSource