Insert a Processing Instruction and a Comment Node
This Example shows you how to Insert a Processing Node and Comment Node in a DOM document. JAXP (Java API for XML Processing) is an interface which provides parsing of xml documents. Here the Document BuilderFactory is used to create new DOM parsers. There are some of the methods used in code given below for Inserting Nodes:-
ProcessingInstruction instruction = doc.createProcessingInstruction("open", "close"):- This method creates a ProcessingInstruction with the specified target name and data string. Here target name is "open" and data string is "close".
root.insertBefore(instruction, node):- This method inserts a new child node before an existing child node. Here new node is instruction and existing node is node.
Element root = doc.getDocumentElement():- This method creates an instance of an XMLDOMElement.
Comment c = doc.createComment("Roseindia.net is an It company"):- This method creates a Comment Node. Here "Roseindia.net is an It company" is a string that specifies the data for the node.
Xml code for the program generated is:-
<?xml version="1.0" encoding="UTF-8"?> <Company> <Location> <Companyname>Roseindia .Net</Companyname> <Employee>Girish Tewari</Employee> </Location> <Id> </Id> </Company> |
InsertProcessingInstruction.java
/* |
Output of the program:
ProcessingInstruction created is: [open: close] Commentnode inserted is: [#comment: Roseindia.net is an It company] |