How do you debug an xml object that causes a transform error when writing to string? How do you debug an xml object that causes a transform error when writing to string? xml xml

How do you debug an xml object that causes a transform error when writing to string?


I am guessing that the error is with one of your objects, either because that object is not valid XML or because the XML has an empty (null) text node rather than an empty string value.

see java.lang.NullPointerException

at com.sun.org.apache.xml.internal.serializer. ToUnknownStream.characters(ToUnknownStream.java:338)

Based on the link in @Sajan Chandran's comment:

http://dotcommers.wordpress.com/2008/10/22/javaxxmltransformtransformerexception-javalangnullpointerexception-how-to-solve/

If you take a look at that you will see the code.

public void characters(String chars) throws SAXException{  final int length = chars.length();

The issue being char.length() is zero because of a null text node.

As it states, to fix this simply make sure all XML nodes have a string value rather than 'null' value.

If this does not work can you post an example of the XML object that causes the exception to be thrown. Also, can you check that the object is valid XML and that character entities, etc, are properly encoded.


Basically agreeing with the other answers, except:

The problem would be that you have a text node with null (not empty) character content, in your doc or e data structure.

While Kiran's post (linked by Sajan) is helpful, I'm pretty sure he's mistaken about "the chars.length() was zero because there was a null text node insertion being tried." The chars.length() was not zero; the call to chars.length() threw an NPE because chars was null.

To find the problem, add a method call here:

    if (e != null) {        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");        source = new DOMSource(e);        checkForNullTextNodes(e); // ADDED    } else {        source = new DOMSource(doc);        checkForNullTextNodes(doc);  // ADDED    }    transformer.transform(source, result); // <-- Error occurs here

Then define checkForNullTextNodes(node) to: (pseudocode)

  • walk through the tree with n for each node
  • check each text node for null character data

E.g. (still semi-pseudocode)

 if (n.getType() == org.w3c.dom.Node.TEXT_NODE && n.getNodeValue() == null) {     throw new Exception("Text node with null content: " +         path to this node); }

That should help you find out where any text nodes with null character content are, which should help you figure out where they are coming from. Where are these doc or e structures being created, anyway?


I agree with the other answer, here is the code I used to get rid of the null nodes

public void deleteNullNode(Node racine)    {        NodeList nl = racine.getChildNodes();        for (int i = 0; i < nl.getLength(); i++)        {            if (nl.item(i).getNodeType() == Node.TEXT_NODE && nl.item(i).getNodeValue() == null)            {                nl.item(i).getParentNode().removeChild(nl.item(i));            }            else            {                deleteNullNode(nl.item(i));            }        }    }