0

I have an XML document which has null values in its Value tag (something similar to below)

  <ns2:Attribute Name="Store" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">
         <ns2:AttributeValue/>
  </ns2:Attribute>

Now, I have to write Java code to modify the values as below.

  <ns2:Attribute Name="Store" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">
         <ns2:AttributeValue>ABCDEF</ns2:AttributeValue>
  </ns2:Attribute>

I am using DOM parser to parse the XML. I am able to delete the null tag for " but not able to add new values. I am not even sure if we have a direct way to replace or add the values.

Below is the code I am using to remove the child("")

    Node aNode = nodeList.item(i);
    eElement = (Element) aNode;
    eElement.removeChild(eElement.getFirstChild().getNextSibling());

Thanks in advance

2
  • 1
    Have you tried setTextContent? Commented Apr 28, 2014 at 20:56
  • You may want to look at JAXB if you are doing multiple such manipulations. Commented Apr 28, 2014 at 21:06

1 Answer 1

1

Just add data through setTextContent to the element. Sample code is as below:

public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, TransformerException {
    String xml = "<ns2:Attribute Name=\"Store\" NameFormat=\"urn:oasis:names:tc:SAML:2.0:attrname-format:uri\"><ns2:AttributeValue/></ns2:Attribute>";
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(new ByteArrayInputStream(xml.getBytes()));
    doc.getDocumentElement().normalize();
    NodeList nList = doc.getElementsByTagName("ns2:AttributeValue");
    for (int i=0;i<nList.getLength();i++) {
        Element elem = (Element)nList.item(i);
        elem.setTextContent("Content"+i);
    }
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(System.out);

    transformer.transform(source, result);



System.out.println(nList.getLength());
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Hirak for above code. I am getting this error when I use elem.setTextContent("ABCD") java.lang.NoSuchMethodError: org.w3c.dom.Element.setTextContent(Ljava/lang/String;)V do you know how to clear this error?
I never faced this problem. I hope your java version is 1.5 or above? Are you using the default xerces Dom implementation that come with Java? Or any third party implementation?.... However, alternatively can you try nList.item(i).appendChild(doc.createTextNode("")); and see if this works?
I was not including proper class (import org.w3c.dom.Text) After including it I was able to get it done. Thanks a lot...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.