List of usage examples for org.w3c.dom Node ATTRIBUTE_NODE
short ATTRIBUTE_NODE
To view the source code for org.w3c.dom Node ATTRIBUTE_NODE.
Click Source Link
Attr
. From source file:Main.java
public static String getTextFromNode(Node node, String defaultValue) { if (node == null) { return defaultValue; }/*from w ww . j a va2s . co m*/ if (node.getNodeType() == Node.ATTRIBUTE_NODE) { return ((Attr) node).getValue(); } else if (node.getNodeType() == Node.TEXT_NODE) { return node.getNodeValue(); } else { StringBuffer text = new StringBuffer(); getTextFromNode(node, text, true); return text.toString().trim(); } }
From source file:XmlUtil.java
/** * Returns attribute value of a node or <code>null</code> if attribute name not found. * Specified attribute is searched on every call. * Consider {@link #getAllAttributes(org.w3c.dom.Node)} for better performances. *//*from www. jav a 2s. c om*/ public static String getAttributeValue(Node node, String attrName) { NamedNodeMap nmm = node.getAttributes(); for (int j = 0; j < nmm.getLength(); j++) { Node attribute = nmm.item(j); if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) { continue; } String nodeName = attribute.getNodeName(); if (nodeName.equals(attrName)) { return attribute.getNodeValue(); } } return null; }
From source file:Main.java
public String getNodeType(Node node) { String type;/* ww w .j av a 2 s .c o m*/ switch (node.getNodeType()) { case Node.ELEMENT_NODE: { type = "Element"; break; } case Node.ATTRIBUTE_NODE: { type = "Attribute"; break; } case Node.TEXT_NODE: { type = "Text"; break; } case Node.CDATA_SECTION_NODE: { type = "CData section"; break; } case Node.ENTITY_REFERENCE_NODE: { type = "Entity reference"; break; } case Node.ENTITY_NODE: { type = "Entity"; break; } case Node.PROCESSING_INSTRUCTION_NODE: { type = "Processing instruction"; break; } case Node.COMMENT_NODE: { type = "Comment"; break; } case Node.DOCUMENT_NODE: { type = "Document"; break; } case Node.DOCUMENT_TYPE_NODE: { type = "Document type"; break; } case Node.DOCUMENT_FRAGMENT_NODE: { type = "Document fragment"; break; } case Node.NOTATION_NODE: { type = "Notation"; break; } default: { type = "???"; break; } } return type; }
From source file:Main.java
/** * Method getStrFromNode//w w w .j a va 2s.c o m * * @param xpathnode * @return the string for the node. */ public static String getStrFromNode(Node xpathnode) { if (xpathnode.getNodeType() == Node.TEXT_NODE) { // we iterate over all siblings of the context node because eventually, // the text is "polluted" with pi's or comments StringBuilder sb = new StringBuilder(); for (Node currentSibling = xpathnode.getParentNode() .getFirstChild(); currentSibling != null; currentSibling = currentSibling.getNextSibling()) { if (currentSibling.getNodeType() == Node.TEXT_NODE) { sb.append(((Text) currentSibling).getData()); } } return sb.toString(); } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) { return ((Attr) xpathnode).getNodeValue(); } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { return ((ProcessingInstruction) xpathnode).getNodeValue(); } return null; }
From source file:Main.java
private static boolean serializeXmlNode(Node node, Writer writer, boolean includeNode) throws IOException { if (node == null) { return false; }// ww w . j ava2 s . c o m short type = node.getNodeType(); boolean result = true; switch (type) { case Node.ATTRIBUTE_NODE: { String text = ((Attr) node).getValue(); writer.write(text); break; } case Node.TEXT_NODE: { String text = ((Text) node).getData(); writer.write(text); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; if (includeNode) { serializeXML(element, writer, false); } else { Node child = element.getFirstChild(); while (child != null) { serializeXmlNode(child, writer, true); child = child.getNextSibling(); } } break; } case Node.DOCUMENT_NODE: { Document doc = (Document) node; serializeXmlNode(doc.getDocumentElement(), writer, includeNode); break; } default: result = false; break; } return result; }
From source file:Main.java
/** * Returns true if the descendantOrSelf is on the descendant-or-self axis * of the context node./*from w w w .j av a2 s. co m*/ * * @param ctx * @param descendantOrSelf * @return true if the node is descendant */ static public boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) { if (ctx == descendantOrSelf) { return true; } Node parent = descendantOrSelf; while (true) { if (parent == null) { return false; } if (parent == ctx) { return true; } if (parent.getNodeType() == Node.ATTRIBUTE_NODE) { parent = ((Attr) parent).getOwnerElement(); } else { parent = parent.getParentNode(); } } }
From source file:Main.java
/** * This method returns the first non-null owner document of the Nodes in this Set. * This method is necessary because it <I>always</I> returns a * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE> * if the {@link Node} is a {@link Document}. * * @param xpathNodeSet//from w ww . j a v a 2s . c o m * @return the owner document */ public static Document getOwnerDocument(Set<Node> xpathNodeSet) { NullPointerException npe = null; for (Node node : xpathNodeSet) { int nodeType = node.getNodeType(); if (nodeType == Node.DOCUMENT_NODE) { return (Document) node; } try { if (nodeType == Node.ATTRIBUTE_NODE) { return ((Attr) node).getOwnerElement().getOwnerDocument(); } return node.getOwnerDocument(); } catch (NullPointerException e) { npe = e; } } throw new NullPointerException(npe.getMessage()); }
From source file:TreeDumper2.java
private void dumpLoop(Node node, String indent) { switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: dumpAttributeNode((Attr) node, indent); break;//w w w .j a v a 2 s.c om case Node.CDATA_SECTION_NODE: dumpCDATASectionNode((CDATASection) node, indent); break; case Node.COMMENT_NODE: dumpCommentNode((Comment) node, indent); break; case Node.DOCUMENT_NODE: dumpDocument((Document) node, indent); break; case Node.DOCUMENT_FRAGMENT_NODE: dumpDocumentFragment((DocumentFragment) node, indent); break; case Node.DOCUMENT_TYPE_NODE: dumpDocumentType((DocumentType) node, indent); break; case Node.ELEMENT_NODE: dumpElement((Element) node, indent); break; case Node.ENTITY_NODE: dumpEntityNode((Entity) node, indent); break; case Node.ENTITY_REFERENCE_NODE: dumpEntityReferenceNode((EntityReference) node, indent); break; case Node.NOTATION_NODE: dumpNotationNode((Notation) node, indent); break; case Node.PROCESSING_INSTRUCTION_NODE: dumpProcessingInstructionNode((ProcessingInstruction) node, indent); break; case Node.TEXT_NODE: dumpTextNode((Text) node, indent); break; default: System.out.println(indent + "Unknown node"); break; } NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) dumpLoop(list.item(i), indent + " "); }
From source file:Main.java
/** * * @param xmlContent/*from w w w.j a v a 2 s . c om*/ * @param charset * @param expression * @return * @throws SAXException * @throws IOException * @throws ParserConfigurationException * @throws XPathExpressionException */ public static List<String> getValues(String xmlContent, Charset charset, String expression) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException { List<String> valueList = new ArrayList<String>(); NodeList nodeList = getNodeList(xmlContent, expression); int length = nodeList.getLength(); for (int seq = 0; seq < length; seq++) { Node node = nodeList.item(seq); if (node.getNodeType() == Node.ELEMENT_NODE) { NodeList childNodeList = node.getChildNodes(); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < childNodeList.getLength(); i++) { Node childNode = childNodeList.item(i); if (childNode.getNodeType() == Node.TEXT_NODE) { sBuilder.append(((Text) childNode).getNodeValue()); } } valueList.add(sBuilder.toString()); } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) { valueList.add(node.getNodeValue()); } } return valueList; }
From source file:Main.java
/** * * @param xmlContent// w ww .ja va2 s. co m * @param charset * @param expression * @return * @throws SAXException * @throws IOException * @throws ParserConfigurationException * @throws XPathExpressionException */ public static List<String> getHexBinaryValues(String xmlContent, Charset charset, String expression) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException { List<String> valueList = new ArrayList<String>(); NodeList nodeList = getNodeList(xmlContent, expression); int length = nodeList.getLength(); for (int seq = 0; seq < length; seq++) { Node node = nodeList.item(seq); if (node.getNodeType() == Node.ELEMENT_NODE) { NodeList childNodeList = node.getChildNodes(); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < childNodeList.getLength(); i++) { Node childNode = childNodeList.item(i); if (childNode.getNodeType() == Node.TEXT_NODE) { sBuilder.append(((Text) childNode).getNodeValue()); } } valueList.add(printHexBinary(sBuilder.toString(), charset)); } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) { valueList.add(node.getNodeValue()); } } return valueList; }