Java tutorial
//package com.java2s; /* ************************************************************************** * * See the NOTICE file distributed with this work for additional information * regarding copyright ownership. * * This file is licensed to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * ************************************************************************** */ import java.io.IOException; import java.util.Iterator; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static void append(Document source, String path, Document target) throws XPathExpressionException, IOException { Element e = target.getDocumentElement(); if (e == null) { e = target.createElement("result"); target.appendChild(e); } append(source.getDocumentElement(), path, e); } public static void append(Element source, String path, Element target) throws XPathExpressionException, IOException { for (Node node : eval(source, path)) { node = copyNode(target, node); } } public static Iterable<Node> eval(Document doc, String path) throws XPathExpressionException { return eval(doc.getDocumentElement(), path); } public static Iterable<Node> eval(Node element, String path) throws XPathExpressionException { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); // xpath.setNamespaceContext(new NamespaceContext() { // // /** // * @WARNING this code will work only if the namespace is present at // * each node of the xml dom and within the xpath queries. // * Otherwise you can fix like that: // * // * <pre> // * // FIXME: this is a // * hack!! if ("atom".equals(prefix)) return // * "http://www.w3.org/2005/Atom"; but it wont work with // * </pre> // * // * different namespaces // * @see // javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String) // */ // public String getNamespaceURI(String prefix) { // String namespace = DOMUtil.lookupNamespaceURI(node, prefix); // return namespace; // } // // public String getPrefix(String namespaceURI) { // String prefix = node.lookupPrefix(namespaceURI); // return prefix; // } // // public Iterator<?> getPrefixes(final String namespaceURI) { // return new Iterator<String>() { // String ns = getPrefix(namespaceURI); // // public boolean hasNext() { // return ns != null; // } // // public String next() { // String r = ns; // ns = null; // return r; // } // // public void remove() { // } // // }; // } // }); XPathExpression expr = xpath.compile(path); final NodeList result = (NodeList) expr.evaluate(element, XPathConstants.NODESET); return new Iterable<Node>() { public Iterator<Node> iterator() { return new Iterator<Node>() { int len = result.getLength(); int pos; public boolean hasNext() { return pos < len; } public Node next() { if (pos >= len) { return null; } return result.item(pos++); } public void remove() { throw new IllegalAccessError(); } }; } }; } /** * @param target * @param node * @return */ public static Node copyNode(Element target, Node node) { Node n = node.cloneNode(true); Document targetDoc = target.getOwnerDocument(); targetDoc.adoptNode(n); target.appendChild(n); return node; } }