You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Tom Schindl <to...@bestsolution.at> on 2002/11/28 20:02:53 UTC

output of entities

hello,

I have the following problem:

reading in a xml-document containing

<hello>World &amp; Universe</hello>

and writing it out using

----------------------8<--------------------------------

DOMParser parser = new DOMParser();
parser.parse("/some/file.xml");
doc = parser.getDocument();
printDOMTree(doc);

----------------------8<--------------------------------

----------------------8<--------------------------------

    public void printDOMTree(Node node)
    {
        int type = node.getNodeType();
        switch (type)
        {
            case Node.DOCUMENT_NODE:
            {
                System.out.println("<?xml version=\"1.0\" ?>");
                printDOMTree(((Document)node).getDocumentElement());
                break;
            }
            case Node.ELEMENT_NODE:
            {
                System.out.print("<");
                System.out.print(node.getNodeName());
                NamedNodeMap attrs = node.getAttributes();
                for (int i = 0; i < attrs.getLength(); i++)
                {
                    Node attr = attrs.item(i);
                    System.out.print(" " + attr.getNodeName() +
                    "=\"" + attr.getNodeValue() +
                    "\"");
                }
                System.out.println(">");
                
                NodeList children = node.getChildNodes();
                if (children != null)
                {
                    int len = children.getLength();
                    for (int i = 0; i < len; i++)
                        printDOMTree(children.item(i));
                }
                
                break;
            }
            case Node.ENTITY_REFERENCE_NODE:
            {
                System.out.print("&");
                System.out.print(node.getNodeName());
                System.out.print(";");
                break;
            }
            case Node.TEXT_NODE:
            {
                System.out.print(node.getNodeValue());
                break;
            }
        }
    }
----------------------8<--------------------------------
prints

<hello>World & Universe</hello>

instead of

<hello>World &amp; Universe</hello>

How could I turn off resolving of entities??

Thanks,
Tom

p.s. already tried some features but with no success.

-- 
Tom Schindl <to...@bestsolution.at>

Re: output of entities

Posted by Andy Clark <an...@apache.org>.
Tom Schindl wrote:
> reading in a xml-document containing
> 
> <hello>World &amp; Universe</hello>
> [...] 
>             case Node.TEXT_NODE:
>             {
>                 System.out.print(node.getNodeValue());
>                 break;
>             }
> [...] 
> <hello>World & Universe</hello>
> [...]
> How could I turn off resolving of entities??

You are taking the wrong approach. Your code that
outputs the text value must check each character and
escape ampersands (&) and less-than (<) characters.

-- 
Andy Clark * andyc@apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org