List of usage examples for org.w3c.dom Document createComment
public Comment createComment(String data);
Comment
node given the specified string. From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*from w w w . ja v a2 s . c o m*/ factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); // Find all elements with the name "entry" and append a comment NodeList list = doc.getElementsByTagName("entry"); for (int i = 0; i < list.getLength(); i++) { Element element = (Element) list.item(i); Comment comment = doc.createComment("index=" + i); // Add the comment after this element element.getParentNode().insertBefore(comment, element.getNextSibling()); } }
From source file:com.cladonia.security.signature.SignatureGenerator.java
public static void main(String args[]) throws Exception { // use this if you want to configure logging, normally would put this in a static block, // but this is just for testing (see jre\lib\logging.properties) org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory .getLog(SignatureGenerator.class.getName()); //System.out.println("Using the logger: "+log.getClass().getName()); //log.debug("Debug is on"); //log.warn("Warning is on"); //log.error("Error is on"); log.info("**** Testing Signature Generator *****"); //All the parameters for the keystore String keystoreType = "JKS"; String keystoreFile = "data/keystore.jks"; String keystorePass = "xmlexchanger"; String privateKeyAlias = "exchanger"; String privateKeyPass = "xmlexchanger"; String certificateAlias = "exchanger"; // set the keystore and private key properties KeyBuilder.setParams(keystoreType, keystoreFile, keystorePass, privateKeyAlias, privateKeyPass, certificateAlias);/*from ww w. ja v a 2 s. co m*/ // get the private key for signing. PrivateKey privateKey = KeyBuilder.getPrivateKey(); // get the cert X509Certificate cert = KeyBuilder.getCertificate(); // ************* create a sample to be signed ****************** javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); //XML Signature needs to be namespace aware dbf.setNamespaceAware(true); javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder(); org.w3c.dom.Document document = db.newDocument(); //Build a sample document. It will look something like: //<!-- Comment before --> //<cladonia:Exchanger xmlns:cladonia="http://www.exchangerxml.com"> //</cladonia:Exchanger> document.appendChild(document.createComment(" Comment before ")); Element root = document.createElementNS("http://www.exchangerxml.com", "cladonia:Exchanger"); root.setAttributeNS(null, "attr1", "test1"); root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo", "http://www.exchangerxml.com/#foo"); root.setAttributeNS("http://example.org/#foo", "foo:attr1", "foo's test"); root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:cladonia", "http://www.exchangerxml.com"); document.appendChild(root); Element firstchild = document.createElementNS("http://www.exchangerxml.com", "cladonia:Editor"); firstchild.appendChild(document.createTextNode("simple text\n")); firstchild.setAttributeNS(null, "Id", "CladoniaId"); root.appendChild(firstchild); //******************** End of sample to be signed************************* // *************** Signature 1 // create SignatureGenerator using private key, cert and the dom (i.e an enveloped signature) SignatureGenerator gen = new SignatureGenerator(privateKey, cert, document); // set the c14n algorithm (Exclusive) gen.setC14nAlgorithm(SignatureGenerator.TRANSFORM_C14N_EXCL_WITH_COMMENTS); // set the xpath transform gen.setXpath("//cladonia:Editor"); // set the id gen.setId("CladoniaId"); // sign the document document = gen.sign(null); // output the enveloped signature FileOutputStream fos = new FileOutputStream("c:\\temp\\sigout.xml"); XMLUtils.outputDOMc14nWithComments(document, fos); fos.close(); System.out.println("Created Signature 1 - an enveloped signature"); // ************** Signature 2 // now sign the previous output as an example of a detached signature SignatureGenerator gen2 = new SignatureGenerator(privateKey, cert, "file:///c:/temp/sigout.xml"); // set the c14n algorithm gen2.setC14nAlgorithm(SignatureGenerator.TRANSFORM_C14N_WITH_COMMENTS); // sign the document Document document2 = gen2.sign(null); // output the detached signature FileOutputStream fos2 = new FileOutputStream("c:\\temp\\sigout2.xml"); XMLUtils.outputDOMc14nWithComments(document2, fos2); fos2.close(); System.out.println("Created Signature 2 - a detached signature"); System.out.println(""); }
From source file:Main.java
public static void createComment(Document doc, String comment) { Comment c = doc.createComment(comment); doc.getDocumentElement().appendChild(c); }
From source file:Main.java
/** * Convenience function for creating comments. * //from w w w . j a v a2 s .co m * @param document * the DOM document we're creating the comment in * @param comment * the comment text * @return a comment node */ public static Comment createComment(Document doc, String comment) { return doc.createComment(comment); }
From source file:Main.java
/** * Creates an XML comment with the given text * //from ww w .j a va 2 s .c o m * @param d * the "mother" document of the created comment * @param text * the comment text * @return a comment containing the given text */ public static Comment createComment(Document d, String text) { return d.createComment(text); }
From source file:Main.java
public static void createComment(Document doc, Element elem, String comment) { Comment c = doc.createComment(comment); elem.appendChild(c);/* w ww.j av a2s. com*/ }
From source file:Main.java
/** * add a comment below element el/*from ww w . ja v a 2 s . com*/ */ public static void addComment(Document outDoc, Element el, String cText) { Comment com = outDoc.createComment(cText); el.appendChild(com); }
From source file:Main.java
public static void createComment(Document paramDocument, String paramString) { Comment localComment = paramDocument.createComment(paramString); paramDocument.getDocumentElement().appendChild(localComment); }
From source file:Main.java
/** * Creates a DOM comment/* ww w . j a va 2 s . co m*/ * @param parent parent DOM element * @param str comment text * @return DOM comment */ public static Comment createComment(Element parent, String str) { Document doc = parent.getOwnerDocument(); Comment c = doc.createComment(str); parent.appendChild(c); return c; }
From source file:Main.java
public static void removeNode(Document doc, Node parent, Node node, String nodeName) { Node comment = doc.createComment(" '" + nodeName + "' was removed by FM GUI @ " + (new Date()) + " "); parent.insertBefore(comment, node);/* w w w . j a va 2 s.c o m*/ parent.removeChild(node); }