List of usage examples for org.w3c.dom Attr getName
public String getName();
From source file:Main.java
/** * Builds an XPointer that refers to the given node. If a shorthand pointer * (using a schema-determined identifier) cannot be constructed, then a * scheme-based pointer is derived that indicates the absolute location path * of a node in a DOM document. The location is specified as a scheme-based * XPointer having two parts://from w w w. ja va 2 s. com * <ul> * <li>an xmlns() part that declares a namespace binding context;</li> * <li>an xpointer() part that includes an XPath expression using the * abbreviated '//' syntax for selecting a descendant node.</li> * </ul> * * @param node * A node in a DOM document. * @return A String containing either a shorthand or a scheme-based pointer * that refers to the node. * * @see <a href="http://www.w3.org/TR/xptr-framework/"target="_blank"> * XPointer Framework</a> * @see <a href="http://www.w3.org/TR/xptr-xmlns/" target="_blank">XPointer * xmlns() Scheme</a> * @see <a href="http://www.w3.org/TR/xptr-xpointer/" * target="_blank">XPointer xpointer() Scheme</a> */ public static String buildXPointer(Node node) { if (null == node) { return ""; } StringBuilder xpointer = new StringBuilder(); if (null != node.getAttributes() && null != node.getAttributes().getNamedItem("id")) { String id = node.getAttributes().getNamedItem("id").getNodeValue(); xpointer.append(node.getLocalName()).append("[@id='").append(id).append("']"); return xpointer.toString(); } String nsURI = node.getNamespaceURI(); String nsPrefix = node.getPrefix(); if (null == nsPrefix) nsPrefix = "tns"; // WARNING: Escaping rules are currently ignored. xpointer.append("xmlns(").append(nsPrefix).append("=").append(nsURI).append(")"); xpointer.append("xpointer(("); switch (node.getNodeType()) { case Node.ELEMENT_NODE: // Find the element in the list of all similarly named descendants // of the document root. NodeList elementsByName = node.getOwnerDocument().getElementsByTagNameNS(nsURI, node.getLocalName()); for (int i = 0; i < elementsByName.getLength(); i++) { if (elementsByName.item(i).isSameNode(node)) { xpointer.append("//"); xpointer.append(nsPrefix).append(':').append(node.getLocalName()).append(")[").append(i + 1) .append("])"); break; } } break; case Node.DOCUMENT_NODE: xpointer.append("/"); break; case Node.ATTRIBUTE_NODE: Attr attrNode = (Attr) node; xpointer = new StringBuilder(buildXPointer(attrNode.getOwnerElement())); xpointer.insert(xpointer.lastIndexOf(")"), "/@" + attrNode.getName()); break; default: xpointer.setLength(0); break; } return xpointer.toString(); }
From source file:DOMUtils.java
public static void compareNodes(Node expected, Node actual) throws Exception { if (expected.getNodeType() != actual.getNodeType()) { throw new Exception("Different types of nodes: " + expected + " " + actual); }//from ww w . java 2s . c o m if (expected instanceof Document) { Document expectedDoc = (Document) expected; Document actualDoc = (Document) actual; compareNodes(expectedDoc.getDocumentElement(), actualDoc.getDocumentElement()); } else if (expected instanceof Element) { Element expectedElement = (Element) expected; Element actualElement = (Element) actual; // compare element names if (!expectedElement.getLocalName().equals(actualElement.getLocalName())) { throw new Exception("Element names do not match: " + expectedElement.getLocalName() + " " + actualElement.getLocalName()); } // compare element ns String expectedNS = expectedElement.getNamespaceURI(); String actualNS = actualElement.getNamespaceURI(); if ((expectedNS == null && actualNS != null) || (expectedNS != null && !expectedNS.equals(actualNS))) { throw new Exception("Element namespaces names do not match: " + expectedNS + " " + actualNS); } String elementName = "{" + expectedElement.getNamespaceURI() + "}" + actualElement.getLocalName(); // compare attributes NamedNodeMap expectedAttrs = expectedElement.getAttributes(); NamedNodeMap actualAttrs = actualElement.getAttributes(); if (countNonNamespaceAttribures(expectedAttrs) != countNonNamespaceAttribures(actualAttrs)) { throw new Exception(elementName + ": Number of attributes do not match up: " + countNonNamespaceAttribures(expectedAttrs) + " " + countNonNamespaceAttribures(actualAttrs)); } for (int i = 0; i < expectedAttrs.getLength(); i++) { Attr expectedAttr = (Attr) expectedAttrs.item(i); if (expectedAttr.getName().startsWith("xmlns")) { continue; } Attr actualAttr = null; if (expectedAttr.getNamespaceURI() == null) { actualAttr = (Attr) actualAttrs.getNamedItem(expectedAttr.getName()); } else { actualAttr = (Attr) actualAttrs.getNamedItemNS(expectedAttr.getNamespaceURI(), expectedAttr.getLocalName()); } if (actualAttr == null) { throw new Exception(elementName + ": No attribute found:" + expectedAttr); } if (!expectedAttr.getValue().equals(actualAttr.getValue())) { throw new Exception(elementName + ": Attribute values do not match: " + expectedAttr.getValue() + " " + actualAttr.getValue()); } } // compare children NodeList expectedChildren = expectedElement.getChildNodes(); NodeList actualChildren = actualElement.getChildNodes(); if (expectedChildren.getLength() != actualChildren.getLength()) { throw new Exception(elementName + ": Number of children do not match up: " + expectedChildren.getLength() + " " + actualChildren.getLength()); } for (int i = 0; i < expectedChildren.getLength(); i++) { Node expectedChild = expectedChildren.item(i); Node actualChild = actualChildren.item(i); compareNodes(expectedChild, actualChild); } } else if (expected instanceof Text) { String expectedData = ((Text) expected).getData().trim(); String actualData = ((Text) actual).getData().trim(); if (!expectedData.equals(actualData)) { throw new Exception("Text does not match: " + expectedData + " " + actualData); } } }
From source file:Main.java
public static String nodeToString(Object node) { String result = ""; if (node instanceof String) { String str = ((String) node).replaceAll("\\s+", " ").trim(); result += str;/* ww w.j a v a2 s .c o m*/ } else if (node instanceof Text) { String str = ((Text) node).getTextContent().replaceAll("\\s+", " ").trim(); result += str; } else if (node instanceof Element) { Element en = (Element) node; result += "<" + en.getTagName(); for (int j = 0; j < en.getAttributes().getLength(); j++) { Attr attr = (Attr) en.getAttributes().item(j); //if (!(attr.getNamespaceURI() != null && attr.getNamespaceURI().equals("http://www.w3.org/2000/xmlns/") && (attr.getLocalName().equals("xmlns") || attr.getLocalName().equals("xsi")))) { result += " " + attr.getName() + "=\"" + attr.getValue() + "\""; //} } if (en.getChildNodes().getLength() == 0) { result += "/>"; } else { result += ">"; ArrayList<Object> children = new ArrayList<Object>(); for (int i = 0; i < en.getChildNodes().getLength(); i++) { children.add(en.getChildNodes().item(i)); } result += nodesToString(children); result += "</" + en.getTagName() + ">"; } } return result; }
From source file:Main.java
static boolean canBeMerged(Node node1, Node node2, String requiredTagName) { if (node1.getNodeType() != Node.ELEMENT_NODE || node2.getNodeType() != Node.ELEMENT_NODE) return false; Element element1 = (Element) node1; Element element2 = (Element) node2; if (!equals(requiredTagName, element1.getTagName()) || !equals(requiredTagName, element2.getTagName())) return false; NamedNodeMap attributes1 = element1.getAttributes(); NamedNodeMap attributes2 = element2.getAttributes(); if (attributes1.getLength() != attributes2.getLength()) return false; for (int i = 0; i < attributes1.getLength(); i++) { final Attr attr1 = (Attr) attributes1.item(i); final Attr attr2; if (isNotEmpty(attr1.getNamespaceURI())) attr2 = (Attr) attributes2.getNamedItemNS(attr1.getNamespaceURI(), attr1.getLocalName()); else// w w w .ja v a2 s .co m attr2 = (Attr) attributes2.getNamedItem(attr1.getName()); if (attr2 == null || !equals(attr1.getTextContent(), attr2.getTextContent())) return false; } return true; }
From source file:Main.java
private static Element copyNode(Document destDocument, Element dest, Element src) { NamedNodeMap namedNodeMap = src.getAttributes(); for (int i = 0; i < namedNodeMap.getLength(); i++) { Attr attr = (Attr) namedNodeMap.item(i); dest.setAttribute(attr.getName(), attr.getValue()); }/*from w ww .j a v a 2 s. c o m*/ NodeList childNodeList = src.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node child = childNodeList.item(i); if (child.getNodeType() == Node.TEXT_NODE) { Text text = destDocument.createTextNode(child.getTextContent()); dest.appendChild(text); } else if (child.getNodeType() == Node.ELEMENT_NODE) { Element element = destDocument.createElement(((Element) child).getTagName()); element = copyNode(destDocument, element, (Element) child); dest.appendChild(element); } } return dest; }
From source file:Main.java
private static Node copyNodeExceptNamespace(Document document, Node srcNode, Set<String> namespace, List<String> exceptNamespaces) { if (srcNode.getNodeType() == Node.ELEMENT_NODE) { String nodeName = srcNode.getNodeName(); nodeName = nodeName.substring(nodeName.indexOf(":") + 1); Element element = document.createElement(nodeName); for (int i = 0; i < srcNode.getAttributes().getLength(); i++) { Attr attr = (Attr) srcNode.getAttributes().item(i); String name = attr.getName(); if (name.startsWith("xmlns:")) { String suffix = name.substring(6); if (!exceptNamespaces.contains(suffix)) { namespace.add(suffix); }/*from w ww .j a v a2 s .c o m*/ continue; } } for (int i = 0; i < srcNode.getAttributes().getLength(); i++) { Attr attr = (Attr) srcNode.getAttributes().item(i); String name = attr.getName(); if (name.startsWith("xmlns:")) { continue; } int semi = name.indexOf(":"); if (semi > 0) { if (namespace.contains(name.substring(0, semi))) { name = name.substring(semi + 1); } } element.setAttribute(name, attr.getValue()); } NodeList nodeList = srcNode.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); if (childNode.getNodeType() == Node.TEXT_NODE) { element.appendChild(document.createTextNode(childNode.getTextContent())); } else if (childNode.getNodeType() == Node.ELEMENT_NODE) { Node node = copyNodeExceptNamespace(document, childNode, namespace, exceptNamespaces); element.appendChild(node); } } return element; } if (srcNode.getNodeType() == Node.TEXT_NODE) { Text text = document.createTextNode(srcNode.getTextContent()); return text; } return null; }
From source file:Main.java
/** * Copies all attributes from one element to another in the official way. */// w ww. j a v a 2 s. c o m public static void copyAttributes(Element elementFrom, Element elementTo) { NamedNodeMap nodeList = elementFrom.getAttributes(); if (nodeList == null) { // No attributes to copy: just return return; } Attr attrFrom = null; Attr attrTo = null; // Needed as factory to create attrs Document documentTo = elementTo.getOwnerDocument(); int len = nodeList.getLength(); // Copy each attr by making/setting a new one and // adding to the target element. for (int i = 0; i < len; i++) { attrFrom = (Attr) nodeList.item(i); // Create an set value attrTo = documentTo.createAttribute(attrFrom.getName()); attrTo.setValue(attrFrom.getValue()); // Set in target element elementTo.setAttributeNode(attrTo); } }
From source file:jp.go.nict.langrid.bpel.ProcessAnalyzer.java
private static void addNamespaceMappings(NamespaceContextImpl nsc, NamedNodeMap attrs) { for (int i = 0; i < attrs.getLength(); i++) { Attr attr = (Attr) attrs.item(i); String[] names = attr.getName().split(":", 2); if (names.length == 2) { if (names[0].equals("xmlns")) { nsc.addMapping(names[1], attr.getValue()); }//from www . j ava2s . c o m } } }
From source file:com.portfolio.data.utils.DomUtils.java
public static String getNodeAttributesString(Node node) { String ret = ""; NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr attribute = (Attr) attributes.item(i); ret += attribute.getName().trim() + "=\"" + StringEscapeUtils.escapeXml11(attribute.getValue().trim()) + "\" "; }/*ww w . j a v a2 s . c o m*/ return ret; }
From source file:com.twinsoft.convertigo.engine.localbuild.BuildLocally.java
private static Element cloneNode(Node node, String newNodeName) { Element newElement = node.getOwnerDocument().createElement(newNodeName); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr = (Attr) attrs.item(i); newElement.setAttribute(attr.getName(), attr.getValue()); }/* ww w .j a v a 2s . c om*/ return newElement; }