Example usage for org.w3c.dom Document createProcessingInstruction

List of usage examples for org.w3c.dom Document createProcessingInstruction

Introduction

In this page you can find the example usage for org.w3c.dom Document createProcessingInstruction.

Prototype

public ProcessingInstruction createProcessingInstruction(String target, String data) throws DOMException;

Source Link

Document

Creates a ProcessingInstruction node given the specified name and data strings.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from  w  ww  .ja  va  2  s . co  m*/

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

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

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

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*w  w w.  j a va2  s.  c  om*/

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    // Add a PI at the beginning of the document
    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction("target", "instruction");
    element.getParentNode().insertBefore(pi, element);

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document xmldoc = impl.createDocument(null, "TODOs", null);

    Element root = xmldoc.getDocumentElement();
    Element e0 = xmldoc.createElement("TOPIC");
    Element e1 = xmldoc.createElement("TITLE");
    Node n1 = xmldoc.createTextNode("Java");
    e1.appendChild(n1);//from  ww  w .ja va 2s  .c  o m

    Element e2 = xmldoc.createElement("URL");
    Node n2 = xmldoc.createTextNode("http://www.server.com");
    e2.appendChild(n2);
    e0.appendChild(e1);
    e0.appendChild(e2);
    root.appendChild(e0);

    Node pi = xmldoc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"TODOs.xsl\"");
    xmldoc.insertBefore(pi, root);

    StreamResult out = new StreamResult("howto.xml");
    DOMSource domSource = new DOMSource(xmldoc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, out);
}

From source file:Main.java

public static void insertLeadingPI(Document doc, String target, String data) {
    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction(target, data);
    element.getParentNode().insertBefore(pi, element);
}

From source file:Main.java

public static void insertTrailingPI(Document doc, String target, String data) {
    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction(target, data);
    element.getParentNode().appendChild(pi);
}

From source file:Main.java

public static void addProcessingInstruction(Document doc) {
    Element root = doc.getDocumentElement();
    Element folks = (Element) root.getLastChild();
    ProcessingInstruction pi;//from  ww w .  ja va 2  s . c  o m
    pi = (ProcessingInstruction) doc.createProcessingInstruction("validate", "phone=\"lookup\"");
    root.insertBefore(pi, folks);
}

From source file:DOMEdit.java

public static void addProcessingInstruction(Document doc) {
        Element root = doc.getDocumentElement();
        Element folks = (Element) root.getLastChild();
        ProcessingInstruction pi;
        pi = (ProcessingInstruction) doc.createProcessingInstruction("validate", "phone=\"lookup\"");
        root.insertBefore(pi, folks);/*from  ww  w.  jav  a 2  s  .co  m*/
    }

From source file:Main.java

/**
 * Adds stylesheet informations to an xml document. See
 * <a href='http://stackoverflow.com/questions/2651647/add-xml-stylesheet-and-get-standalone-yes'>Stack Overflow</a>.
 *
 * @param aDocument/* w w  w.jav a  2  s  .  c om*/
 * @param aFilename
 */
public static void addStylesheet(Document aDocument, String aFilename) {

    aDocument.setXmlStandalone(true);

    Element root = aDocument.getDocumentElement();

    String data = DATA_STYLESHEET.replace(LOCATION_PLACEHOLDER, aFilename);
    ProcessingInstruction processingInstruction = aDocument.createProcessingInstruction(TARGET_STYLESHEET,
            data);

    aDocument.insertBefore(processingInstruction, root);
}

From source file:net.padaf.xmpbox.SaveMetadataHelper.java

/**
 * Prepare XMP Saving Put data necessary to make a well-formed XMP
 * /*from   ww w.j  a v a  2 s  .  c o  m*/
 * @param metadata
 *            metadata concerned by the serialization processing
 * @param intoXPacket
 *            true if Processing instruction must be embedded
 * @return The DOM Document which will represent the serialized metadata
 */
protected static Document prepareSaving(XMPMetadata metadata, boolean intoXPacket) {
    Document newdoc = (Document) metadata.getFuturOwner().cloneNode(true);
    if (intoXPacket) {
        ProcessingInstruction beginXPacket = newdoc.createProcessingInstruction("xpacket",
                "begin=\"" + metadata.getXpacketBegin() + "\" id=\"" + metadata.getXpacketId() + "\"");
        newdoc.appendChild(beginXPacket);
    }

    Element xmpMeta = newdoc.createElementNS("adobe:ns:meta/", "x:xmpmeta");
    xmpMeta.setAttributeNS(XMPSchema.NS_NAMESPACE, "xmlns:x", "adobe:ns:meta/");

    newdoc.appendChild(xmpMeta);
    Element elem = (Element) metadata.getContainerElement().cloneNode(true);
    newdoc.adoptNode(elem);
    xmpMeta.appendChild(elem);

    if (intoXPacket) {
        ProcessingInstruction endXPacket = newdoc.createProcessingInstruction("xpacket",
                metadata.getEndXPacket());
        newdoc.appendChild(endXPacket);
    }
    return newdoc;
}

From source file:com.googlecode.jgenhtml.JGenHtmlUtils.java

public static void linkToXsl(Document doc, final String xslPath) {
    ProcessingInstruction xsltLink = doc.createProcessingInstruction("xml-stylesheet",
            "type=\"text/xsl\" href=\"" + xslPath + '"');
    Element documentElement = doc.getDocumentElement();
    doc.insertBefore(xsltLink, documentElement);
}