Example usage for org.w3c.dom Document getFirstChild

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

Introduction

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

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:Main.java

public static void main(final String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from  w  w  w  .  j av  a2s .c  o  m*/
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse(new ByteArrayInputStream(( //
    "<?xml version=\"1.0\"?>" + //
            "<people>" + //
            "<person><name>First Person Name</name></person>" + //
            "<person><name>Second Person Name</name></person>" + //
            "</people>" //
    ).getBytes()));

    String fragment = "<name>Changed Name</name>";
    Document fragmentDoc = builder.parse(new ByteArrayInputStream(fragment.getBytes()));

    Node injectedNode = doc.adoptNode(fragmentDoc.getFirstChild());

    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile("//people/person[2]/name");
    Element nodeFound = (Element) expr.evaluate(doc, XPathConstants.NODE);

    Node parentNode = nodeFound.getParentNode();
    parentNode.removeChild(nodeFound);
    parentNode.appendChild(injectedNode);

    DOMSource domSource = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StreamResult result = new StreamResult(new StringWriter());
    transformer.transform(domSource, result);
    System.out.println(result.getWriter().toString());
}

From source file:Main.java

static public void main(String[] arg) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("foo.xml");

    TransformerFactory tranFactory = TransformerFactory.newInstance();
    Transformer aTransformer = tranFactory.newTransformer();

    NodeList list = doc.getFirstChild().getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node element = list.item(i).cloneNode(true);

        if (element.hasChildNodes()) {
            Source src = new DOMSource(element);
            FileOutputStream fs = new FileOutputStream("k" + i + ".xml");
            Result dest = new StreamResult(fs);
            aTransformer.transform(src, dest);
            fs.close();/*w ww  .j ava 2s .c o m*/
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new File("Sample.xml"));
    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xPath = xFactory.newXPath();
    XPathExpression exp = xPath.compile(
            "/article/body/section/region[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'perfect')]");
    NodeList nl = (NodeList) exp.evaluate(doc.getFirstChild(), XPathConstants.NODESET);
    for (int index = 0; index < nl.getLength(); index++) {
        Node node = nl.item(index);
        System.out.println(node.getTextContent());
    }/*from   w ww.j  ava  2s  .c  o m*/
}

From source file:Main.java

public static Element getRootElement(Document doc) {
    Node node = doc.getFirstChild();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
        node = node.getNextSibling();//from   w  w  w.  j a v  a2s.  com
    }
    return null;
}

From source file:Main.java

public static Node parseXMLSnippet(String s) throws ParserConfigurationException, SAXException, IOException {
    Document doc = parseXML(s);
    return doc.getFirstChild();
}

From source file:Main.java

public static int countChildNodes(String fileName) {
    Document doc = parseFile(fileName);
    return doc.getFirstChild().getChildNodes().getLength();
}

From source file:Main.java

public static void appendChildToRoot(Document doc, String childName, byte[] childContents) {
    appendChild(doc, doc.getFirstChild(), childName, new String(Base64.encodeBase64(childContents)));
}

From source file:Main.java

public static void appendChildToRoot(Document doc, String childName, String childContents) {
    appendChild(doc, doc.getFirstChild(), childName, childContents);
}

From source file:Main.java

/**
 * Creates (only if necessary) and returns the element which is at the end of the specified
 * path.//from   w  w w.  java 2 s.  com
 * 
 * @param doc
 *            the target document where the specified path should be created
 * @param path
 *            a dot separated string indicating the path to be created
 * @return the component at the end of the newly created path.
 */
public static Element createLastPathComponent(Document doc, String[] path) {
    Element parent = (Element) doc.getFirstChild();
    if (path == null || parent == null || doc == null) {
        throw new IllegalArgumentException("Document parent and path must not be null");
    }

    Element e = parent;
    for (int i = 0; i < path.length; i++) {
        Element newEl = getChildElementByTagName(e, path[i]);
        if (newEl == null) {
            newEl = doc.createElement(path[i]);
            e.appendChild(newEl);
        }
        e = newEl;
    }
    return e;
}

From source file:Main.java

public static Node asDOMNode(String xml) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
    return doc.getFirstChild();
}