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:com.marklogic.mapreduce.MarkLogicNode.java
public void readFields(DataInput in) throws IOException { int type = in.readInt(); DocumentBuilder docBuilder = builderLocal.get(); String val = Text.readString(in); try {/*w ww.j a v a 2 s .c o m*/ if (type == Node.ATTRIBUTE_NODE) { AttributeImpl attr = new AttributeImpl(Text.readString(in), Text.readString(in)); node = attr.asW3cNode(docBuilder); } else { node = DomUtil.readXml(IOHelper.newStream(val)); } } catch (SAXException e) { LOG.error("error parsing input", e); throw new IOException(e); } catch (ParserConfigurationException e) { LOG.error("error parsing input", e); throw new IOException(e); } }
From source file:com.adaptris.util.text.xml.XPath.java
/** * returns an array of string values taken from a list of elements returned by * an xpath//from w w w. j av a 2 s. c o m * * @param context the node to apply the XPath to * @param xpath the xpath to apply * @return the strings extracted * @throws XPathExpressionException on error */ public String[] selectMultipleTextItems(Node context, String xpath) throws XPathExpressionException { NodeList list = selectNodeList(context, xpath); String[] retArray = new String[list.getLength()]; for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node != null) { if (node.getNodeType() == Node.ATTRIBUTE_NODE) { retArray[i] = node.getNodeValue(); } else if (node.getNodeType() == Node.TEXT_NODE) { retArray[i] = node.getNodeValue(); } else { node.normalize(); Node text = node.getFirstChild(); if (text != null) { retArray[i] = text.getNodeValue(); } } } } return retArray; }
From source file:DOM2SAX.java
/** * Writes a node using the given writer. * @param node node to serialize//from ww w . j av a 2 s.c o m * @throws SAXException In case of a problem while writing XML */ private void writeNode(Node node) throws SAXException { if (node == null) { return; } switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: // handled by ELEMENT_NODE case Node.DOCUMENT_FRAGMENT_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.ENTITY_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.NOTATION_NODE: // These node types are ignored!!! break; case Node.CDATA_SECTION_NODE: final String cdata = node.getNodeValue(); if (lexicalHandler != null) { lexicalHandler.startCDATA(); contentHandler.characters(cdata.toCharArray(), 0, cdata.length()); lexicalHandler.endCDATA(); } else { // in the case where there is no lex handler, we still // want the text of the cdate to make its way through. contentHandler.characters(cdata.toCharArray(), 0, cdata.length()); } break; case Node.COMMENT_NODE: // should be handled!!! if (lexicalHandler != null) { final String value = node.getNodeValue(); lexicalHandler.comment(value.toCharArray(), 0, value.length()); } break; case Node.DOCUMENT_NODE: contentHandler.startDocument(); Node next = node.getFirstChild(); while (next != null) { writeNode(next); next = next.getNextSibling(); } contentHandler.endDocument(); break; case Node.ELEMENT_NODE: String prefix; List pushedPrefixes = new java.util.ArrayList(); final AttributesImpl attrs = new AttributesImpl(); final NamedNodeMap map = node.getAttributes(); final int length = map.getLength(); // Process all namespace declarations for (int i = 0; i < length; i++) { final Node attr = map.item(i); final String qnameAttr = attr.getNodeName(); // Ignore everything but NS declarations here if (qnameAttr.startsWith(XMLNS_PREFIX)) { final String uriAttr = attr.getNodeValue(); final int colon = qnameAttr.lastIndexOf(':'); prefix = (colon > 0) ? qnameAttr.substring(colon + 1) : EMPTYSTRING; if (startPrefixMapping(prefix, uriAttr)) { pushedPrefixes.add(prefix); } } } // Process all other attributes for (int i = 0; i < length; i++) { final Node attr = map.item(i); final String qnameAttr = attr.getNodeName(); // Ignore NS declarations here if (!qnameAttr.startsWith(XMLNS_PREFIX)) { final String uriAttr = attr.getNamespaceURI(); // Uri may be implicitly declared if (uriAttr != null) { final int colon = qnameAttr.lastIndexOf(':'); prefix = (colon > 0) ? qnameAttr.substring(0, colon) : EMPTYSTRING; if (startPrefixMapping(prefix, uriAttr)) { pushedPrefixes.add(prefix); } } // Add attribute to list attrs.addAttribute(attr.getNamespaceURI(), getLocalName(attr), qnameAttr, "CDATA", attr.getNodeValue()); } } // Now process the element itself final String qname = node.getNodeName(); final String uri = node.getNamespaceURI(); final String localName = getLocalName(node); // Uri may be implicitly declared if (uri != null) { final int colon = qname.lastIndexOf(':'); prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING; if (startPrefixMapping(prefix, uri)) { pushedPrefixes.add(prefix); } } // Generate SAX event to start element contentHandler.startElement(uri, localName, qname, attrs); // Traverse all child nodes of the element (if any) next = node.getFirstChild(); while (next != null) { writeNode(next); next = next.getNextSibling(); } // Generate SAX event to close element contentHandler.endElement(uri, localName, qname); // Generate endPrefixMapping() for all pushed prefixes final int nPushedPrefixes = pushedPrefixes.size(); for (int i = 0; i < nPushedPrefixes; i++) { endPrefixMapping((String) pushedPrefixes.get(i)); } break; case Node.PROCESSING_INSTRUCTION_NODE: contentHandler.processingInstruction(node.getNodeName(), node.getNodeValue()); break; case Node.TEXT_NODE: final String data = node.getNodeValue(); contentHandler.characters(data.toCharArray(), 0, data.length()); break; default: //nop } }
From source file:br.com.elotech.sits.service.AbstractService.java
public Node getXSDNotaFiscal(File file) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setNamespaceAware(true);// ww w . jav a2 s. co m DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(file); Node root = (Node) doc.getDocumentElement(); NamedNodeMap attributes = root.getAttributes(); if (attributes != null) { for (int i = 0; i < attributes.getLength(); i++) { Node node = attributes.item(i); if (node.getNodeType() == Node.ATTRIBUTE_NODE) { if (ATRIBUTO_PRINCIPAL_NOTA_FISCAL.equals(node.getNodeName())) { return node; } } } } return null; }
From source file:com.git.original.common.config.XMLFileConfigDocument.java
/** * XML??//from w w w.ja v a2 s . c o m * * @param elem * XML * @return ? */ static ConfigNode convertElement(Element elem) { if (elem == null) { return null; } ConfigNode cn = new ConfigNode(elem.getTagName(), null); NamedNodeMap attrNodeMap = elem.getAttributes(); if (attrNodeMap != null) { for (int i = 0; i < attrNodeMap.getLength(); i++) { Node node = attrNodeMap.item(i); cn.addAttribute(node.getNodeName(), node.getNodeValue()); } } NodeList nodeList = elem.getChildNodes(); if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: cn.addAttribute(node.getNodeName(), node.getNodeValue()); break; case Node.ELEMENT_NODE: ConfigNode child = convertElement((Element) node); cn.addChild(child); break; case Node.TEXT_NODE: cn.value = node.getNodeValue(); break; default: continue; } } } return cn; }
From source file:com.marklogic.mapreduce.MarkLogicNode.java
public void write(DataOutput out) throws IOException { if (node != null) { int type = node.getNodeType(); out.writeInt(type);/* w w w.j av a2 s .co m*/ if (type == Node.ATTRIBUTE_NODE) { Text.writeString(out, node.getNodeName()); Text.writeString(out, node.getNodeValue()); } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { DomUtil.writeXml(node, baos); Text.writeString(out, baos.toString()); } catch (TransformerException e) { LOG.error("error transforming node", e); throw new IOException(e); } } } else { LOG.error("Node to write is null."); } }
From source file:com.mtgi.analytics.XmlBehaviorEventPersisterTest.java
@Test public void testNestedEvents() throws InterruptedException, IOException, XMLStreamException, SAXException { //we reuse the test event creation code from jdbc persister test to get ourselves an interesting dataset. final ArrayList<BehaviorEvent> events = new ArrayList<BehaviorEvent>(); int[] counter = { 0 }; for (int i = 0; i < 3; ++i) JdbcBehaviorEventPersisterTest.createEvent(null, 1, 3, 3, counter, events); LinkedList<BehaviorEvent> queue = new LinkedList<BehaviorEvent>(events); persister.persist(queue);/*from ww w . j av a2 s .c o m*/ assertEquals("queue unmodified by persistence operation", 39, queue.size()); assertEquals("persister reports correct file size", file.length(), persister.getFileSize()); //now perform verification of log data against the expected results. String fileName = persister.rotateLog(); Document actualXML = buildTestDocument(FileUtils.readFileToString(new File(fileName))); //read up the expected results for comparison. InputStream expectedData = XmlBehaviorEventPersisterTest.class .getResourceAsStream("XmlBehaviorEventPersisterTest.testNestedEvents-result.xml"); Document controlXML = buildTestDocument(new InputSource(expectedData)); expectedData.close(); XMLUnit.setIgnoreWhitespace(true); //compare the logged data to our expectations, ignoring time-sensitive values. Diff diff = new Diff(controlXML, actualXML); diff.overrideDifferenceListener(new DifferenceListener() { //filter out artificial differences in id, start time, duration public int differenceFound(Difference diff) { Node cn = diff.getControlNodeDetail().getNode(); Node tn = diff.getTestNodeDetail().getNode(); if (cn != null && tn != null) { short ctype = cn.getNodeType(); short ttype = tn.getNodeType(); if (ctype == ttype) { if (ctype == Node.ATTRIBUTE_NODE) { if (cn.getNodeName().equals("id") || cn.getNodeName().equals("parent-id")) { //we can at least verify that the logged ID matches the ID assigned to our data model. int index = -1; for (Node n = ((Attr) cn).getOwnerElement(); n != null; n = n.getPreviousSibling()) if (n.getNodeType() == Node.ELEMENT_NODE) ++index; BehaviorEvent event = events.get(index); if (cn.getNodeName().equals("id")) { assertEquals("logged event has same id as data model", event.getId().toString(), tn.getNodeValue()); } else { BehaviorEvent parent = event.getParent(); assertNotNull("node " + event.getId() + " has parent", parent); assertEquals("logged event has same parent-id as data model", parent.getId().toString(), tn.getNodeValue()); } return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR; } } else if (ctype == Node.TEXT_NODE) { String cname = cn.getParentNode().getNodeName(); String tname = tn.getParentNode().getNodeName(); if (cname.equals(tname)) { //TODO: sanity check values. if ("duration-ns".equals(cname) || "start".equals(cname)) return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR; } } } } return RETURN_ACCEPT_DIFFERENCE; } public void skippedComparison(Node n0, Node n1) { } }); if (!diff.similar()) fail(diff.appendMessage(new StringBuffer()).toString()); }
From source file:com.crawljax.plugins.adi.Report.java
/** * Taken from ErrorReport.//from ww w . j a v a2 s. c o m */ private Document addMarker(String id, Document doc, String xpath) { try { String prefixMarker = "###BEGINMARKER" + id + "###"; String suffixMarker = "###ENDMARKER###"; NodeList nodeList = XPathHelper.evaluateXpathExpression(doc, xpath); if (nodeList.getLength() == 0 || nodeList.item(0) == null) { return doc; } Node element = nodeList.item(0); if (element.getNodeType() == Node.ELEMENT_NODE) { Node beginNode = doc.createTextNode(prefixMarker); Node endNode = doc.createTextNode(suffixMarker); element.getParentNode().insertBefore(beginNode, element); if (element.getNextSibling() == null) { element.getParentNode().appendChild(endNode); } else { element.getParentNode().insertBefore(endNode, element.getNextSibling()); } } else if (element.getNodeType() == Node.TEXT_NODE && element.getTextContent() != null) { element.setTextContent(prefixMarker + element.getTextContent() + suffixMarker); } else if (element.getNodeType() == Node.ATTRIBUTE_NODE) { element.setNodeValue(prefixMarker + element.getTextContent() + suffixMarker); } return doc; } catch (Exception e) { return doc; } }
From source file:com.marklogic.mapreduce.MarkLogicNode.java
@Override public String toString() { if (node != null) { try {// w ww . j av a 2 s . c om StringBuilder buf = new StringBuilder(); if (node.getNodeType() == Node.ATTRIBUTE_NODE) { buf.append("attribute name: "); buf.append(((Attr) node).getName()); buf.append(", value: "); buf.append(((Attr) node).getValue()); } else { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DomUtil.writeXml(node, bos); buf.append(bos.toString()); } return buf.toString(); } catch (TransformerException e) { LOG.error(e); } } return null; }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * //from w w w . j a v a2 s. c om * @param out The <CODE>PrintWriter</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(PrintWriter, Node) * @since TFP 1.0 */ private static void doDump(PrintWriter out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }