org.w3c.dom.Document#createProcessingInstruction ( )源码实例Demo

下面列出了org.w3c.dom.Document#createProcessingInstruction ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: caja   文件: NodesTest.java
public final void testProcessingInstructionInHtml() {
  Document doc = DomParser.makeDocument(null, null);
  ProcessingInstruction pi = doc.createProcessingInstruction(
      "foo", "<script>alert(1)</script>");
  Element el = doc.createElementNS(Namespaces.HTML_NAMESPACE_URI, "div");
  el.appendChild(pi);
  assertEquals(
      "<div><?foo <script>alert(1)</script>?></div>",
      Nodes.render(el, MarkupRenderMode.XML));
  try {
    Nodes.render(el, MarkupRenderMode.HTML);
  } catch (UncheckedUnrenderableException ex) {
    // OK
    return;
  }
  fail("Rendered in html");
}
 
protected Document initRepository ( final String processingType, final String type )
{
    final Document doc = this.xml.create ();

    {
        final ProcessingInstruction pi = doc.createProcessingInstruction ( processingType, "version=\"1.1.0\"" );
        doc.appendChild ( pi );
    }

    final Element root = doc.createElement ( "repository" );
    doc.appendChild ( root );
    root.setAttribute ( "name", this.title );
    root.setAttribute ( "type", type );
    root.setAttribute ( "version", "1" );

    return doc;
}
 
源代码3 项目: neoscada   文件: CompositeBuilder.java
private Document createContent ( final File targetDir, final String type ) throws Exception
{
    final Document doc = createDocument ();

    final ProcessingInstruction pi = doc.createProcessingInstruction ( type, "" );
    pi.setData ( "version=\"1.0.0\"" );
    doc.appendChild ( pi );

    final Element rep = doc.createElement ( "repository" );
    doc.appendChild ( rep );

    final String typeUpper = Character.toUpperCase ( type.charAt ( 0 ) ) + type.substring ( 1 );

    rep.setAttribute ( "name", this.repositoryName );
    rep.setAttribute ( "type", "org.eclipse.equinox.internal.p2.metadata.repository." + typeUpper );
    rep.setAttribute ( "version", "1.0.0" );

    final Element props = doc.createElement ( "properties" );
    rep.appendChild ( props );
    props.setAttribute ( "size", "" + this.properties.size () );

    for ( final Map.Entry<String, String> entry : this.properties.entrySet () )
    {
        addProperty ( props, entry.getKey (), entry.getValue () );
    }

    final Element children = doc.createElement ( "children" );
    rep.appendChild ( children );
    children.setAttribute ( "size", "" + this.dirs.size () );

    for ( final File file : this.dirs )
    {
        final String ref = targetDir.toPath ().relativize ( file.toPath () ).toString ();
        final Element c = doc.createElement ( "child" );
        children.appendChild ( c );
        c.setAttribute ( "location", ref );
    }

    return doc;
}
 
源代码4 项目: openjdk-jdk9   文件: Bug6354955.java
@Test
public void testPINode() {
    try {
        Document xmlDocument = createNewDocument();
        ProcessingInstruction piNode = xmlDocument.createProcessingInstruction("execute", "test");
        String outerXML = getOuterXML(piNode);
        System.out.println("OuterXML of Comment Node is:" + outerXML);

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Exception occured: " + e.getMessage());
    }
}
 
源代码5 项目: xmlunit   文件: test_XpathNodeTracker.java
public void testNodes() throws Exception {
    Document doc = XMLUnit.newControlParser().newDocument();
    Element element = doc.createElementNS("http://example.com/xmlunit", "eg:root");
    xpathNodeTracker.visited(element);
    assertEquals("root element", "/root[1]", xpathNodeTracker.toXpathString());
            
    Attr attr = doc.createAttributeNS("http://example.com/xmlunit", "eg:type");
    attr.setValue("qwerty");
    element.setAttributeNodeNS(attr);
    xpathNodeTracker.visited(attr);
    assertEquals("root element attribute", "/root[1]/@type", xpathNodeTracker.toXpathString());             
            
    xpathNodeTracker.indent();
            
    Comment comment = doc.createComment("testing a comment");
    xpathNodeTracker.visited(comment);
    assertEquals("comment", "/root[1]/comment()[1]", xpathNodeTracker.toXpathString());

    ProcessingInstruction pi = doc.createProcessingInstruction("target","data");
    xpathNodeTracker.visited(pi);
    assertEquals("p-i", "/root[1]/processing-instruction()[1]", xpathNodeTracker.toXpathString());

    Text text = doc.createTextNode("some text");
    xpathNodeTracker.visited(text);
    assertEquals("text", "/root[1]/text()[1]", xpathNodeTracker.toXpathString());

    CDATASection cdata = doc.createCDATASection("some characters");
    xpathNodeTracker.visited(cdata);
    assertEquals("cdata", "/root[1]/text()[2]", xpathNodeTracker.toXpathString());
}
 
源代码6 项目: jdk1.8-source-analysis   文件: DOMUtil.java
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
源代码7 项目: TencentKona-8   文件: DOMUtil.java
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
源代码8 项目: jdk8u60   文件: DOMUtil.java
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
源代码9 项目: Pydev   文件: PyUnitTestRun.java
public String toXML() {
    try {
        DocumentBuilderFactory icFactory = DocumentBuilderFactory.newInstance();
        icFactory.setFeature("http://xml.org/sax/features/namespaces", false);
        icFactory.setFeature("http://xml.org/sax/features/validation", false);
        icFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        icFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

        DocumentBuilder icBuilder = icFactory.newDocumentBuilder();
        Document document = icBuilder.newDocument();
        ProcessingInstruction version = document.createProcessingInstruction("pydev-testrun", "version=\"1.0\""); //$NON-NLS-1$ //$NON-NLS-2$
        document.appendChild(version);

        PyUnitTestRun pyUnitTestRun = this;

        Element root = document.createElement("pydev-testsuite");
        document.appendChild(root);

        Element summary = document.createElement("summary");
        summary.setAttribute("name", name);
        summary.setAttribute("errors", String.valueOf(pyUnitTestRun.getNumberOfErrors()));
        summary.setAttribute("failures", String.valueOf(pyUnitTestRun.getNumberOfFailures()));
        summary.setAttribute("tests", String.valueOf(pyUnitTestRun.getTotalNumberOfRuns()));
        summary.setAttribute("finished", String.valueOf(pyUnitTestRun.getFinished()));
        summary.setAttribute("total_time", String.valueOf(pyUnitTestRun.getTotalTime()));
        root.appendChild(summary);

        for (PyUnitTestResult pyUnitTestResult : pyUnitTestRun.getResults()) {
            Element test = document.createElement("test");
            test.setAttribute("status", pyUnitTestResult.status);
            test.setAttribute("location", pyUnitTestResult.location);
            test.setAttribute("test", pyUnitTestResult.test);
            test.setAttribute("time", pyUnitTestResult.time);

            Element stdout = document.createElement("stdout");
            test.appendChild(stdout);
            stdout.appendChild(document.createCDATASection(pyUnitTestResult.capturedOutput));

            Element stderr = document.createElement("stderr");
            test.appendChild(stderr);
            stderr.appendChild(document.createCDATASection(pyUnitTestResult.errorContents));
            root.appendChild(test);
        }

        Element launchElement = document.createElement("launch");
        root.appendChild(launchElement);
        if (pyUnitLaunch != null) {
            pyUnitLaunch.fillXMLElement(document, launchElement);
        }

        ByteArrayOutputStream s = new ByteArrayOutputStream();

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$

        DOMSource source = new DOMSource(document);
        StreamResult outputTarget = new StreamResult(s);
        transformer.transform(source, outputTarget);
        return new String(s.toByteArray(), StandardCharsets.UTF_8);
    } catch (Exception e) {
        Log.log(e);
    }
    return "";

}
 
源代码10 项目: icafe   文件: XMLUtils.java
public static void insertTrailingPI(Document doc, String target, String data) {
    ProcessingInstruction pi = doc.createProcessingInstruction(target, data);
    doc.appendChild(pi);
}
 
源代码11 项目: Bytecoder   文件: DOMUtil.java
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
源代码12 项目: openjdk-jdk9   文件: DOMUtil.java
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
源代码13 项目: openjdk-jdk9   文件: DOMConfigurationTest.java
/**
 * Equivalence class partitioning with state and input values orientation
 * for public void setParameter(String name, Object value) throws
 * DOMException, <br>
 * <b>pre-conditions</b>: the doc contains two subsequent processing
 * instrictions, <br>
 * <b>name</b>: canonical-form <br>
 * <b>value</b>: true. <br>
 * <b>Expected results</b>: the subsequent processing instrictions are
 * separated with a single line break
 */
@Test
public void testCanonicalForm001() {
    DOMImplementation domImpl = null;
    try {
        domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
    } catch (ParserConfigurationException pce) {
        Assert.fail(pce.toString());
    } catch (FactoryConfigurationError fce) {
        Assert.fail(fce.toString());
    }

    Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);

    DOMConfiguration config = doc.getDomConfig();

    Element root = doc.getDocumentElement();
    ProcessingInstruction pi1 = doc.createProcessingInstruction("target1", "data1");
    ProcessingInstruction pi2 = doc.createProcessingInstruction("target2", "data2");

    root.appendChild(pi1);
    root.appendChild(pi2);

    if (!config.canSetParameter("canonical-form", Boolean.TRUE)) {
        System.out.println("OK, setting 'canonical-form' to true is not supported");
        return;
    }

    config.setParameter("canonical-form", Boolean.TRUE);
    setHandler(doc);
    doc.normalizeDocument();

    Node child1 = root.getFirstChild();
    Node child2 = child1.getNextSibling();

    if (child2.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        Assert.fail("the second child is expected to be a" + "single line break, returned: " + child2);
    }

    // return Status.passed("OK");
}
 
源代码14 项目: icafe   文件: XMLUtils.java
public static void insertLeadingPI(Document doc, String target, String data) {
	Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction(target, data);
    doc.insertBefore(pi, element);
}
 
源代码15 项目: openjdk-8-source   文件: DOMUtil.java
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
protected Node cloneNode(Document doc, boolean deep) {
    return doc.createProcessingInstruction(getTarget(), getData());
}
 
源代码17 项目: openjdk-8   文件: DOMUtil.java
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
源代码18 项目: pixymeta-android   文件: XMLUtils.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);
}
 
源代码19 项目: pixymeta-android   文件: XMLUtils.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);
}
 
源代码20 项目: caja   文件: NodesTest.java
public final void testProcessingInstructions() {
  Document doc = DomParser.makeDocument(null, null);
  ProcessingInstruction pi = doc.createProcessingInstruction("foo", "bar");
  assertEquals("<?foo bar?>", Nodes.render(pi, MarkupRenderMode.XML));
}