Adding a Processing Instruction to a DOM Document - Java XML

Java examples for XML:DOM

Description

Adding a Processing Instruction to a DOM Document

Demo Code


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;

public class Main {

  public void main(String[] argv) {
    Document doc = null;//www  . j  a v  a2s . c  o  m

    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction("target","instruction");
    element.getParentNode().insertBefore(pi, element);

    NodeList list = doc.getElementsByTagName("entry");
    for (int i = 0; i < list.getLength(); i++) {
      element = (Element) list.item(i);
      pi = doc.createProcessingInstruction("target", "instruction=" + i);

      // Add the comment to this element
      element.appendChild(pi);
    }
  }

}

Related Tutorials