Reputation: 77
I am using DocumentBuilder Factory in JAVA for creating an XML file and in my createTextNode() method "null" value is not acceptable.
Element card_number = doc.createElement("number");
card_number.appendChild(doc.createTextNode(MYVALUE));
card.appendChild(card_number);
In this piece of code if MYVALUE equals to NULL, it gives an
javax.xml.transform.TransformerException: java.lang.NullPointerException
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:736)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:340)
I don't want to make an if check because I have lots of Element attribute. I can't make an if check for every single Element attribute. So, does DocumentBuilder have a setting for this issue for null variables it just writes a blank field??
Upvotes: 0
Views: 132
Reputation: 12817
You can create a utility routine to do that: I'll give you the signature:
void appendTextNodeIfNotNull(Element parent, String text)
Upvotes: 1