The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > org.w3c.dom  [30 examples] > Getting Nodes  [5 examples]

e530. Getting the Value of an Entity Reference in a DOM Document

The value of an entity reference is kept in the child nodes of the EntityReference node. This example demonstrates an entity reference with three children.

Note: By default, a parser expands entity references while constructing the DOM tree. See e516 Preventing Expansion of Entity References While Parsing an XML File to prevent expansion.

    // Obtain a document; the following method is implemented in
    // e516 Preventing Expansion of Entity References While Parsing an XML File
    Document doc = parseXmlFileNoExpandER("infilename.xml", false);
    
    // Get the entity reference (see input file)
    Element root = doc.getDocumentElement();
    EntityReference eref = (EntityReference)root.getFirstChild();
    
    // The first child of the entity reference is comment
    Comment comment = (Comment)eref.getFirstChild();               // comment
    
    // The second child of the entity reference is an element
    Element elem = (Element)eref.getFirstChild().getNextSibling(); // <i>
    
    // The third child of the entity reference is a text
    Text text = (Text)eref.getLastChild();                         // text
This is the sample input for the example:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE root [
        <!ENTITY entity "<!--comment--><i/>text">
    ]>
    <root>&entity;</root>

 Related Examples
e1073. Getting the Root Element in a DOM Document
e527. Getting a Node Relative to Another Node in a DOM Document
e528. Getting the Notations in a DOM Document
e529. Getting the Declared Entities in a DOM Document

See also: Adding and Removing Nodes    Element Attributes    Elements    Text Nodes    XPath   


© 2002 Addison-Wesley.