List of usage examples for org.w3c.dom Element getAttributes
public NamedNodeMap getAttributes();
NamedNodeMap
containing the attributes of this node (if it is an Element
) or null
otherwise. From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from w ww.j a va2s . c o m factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); NamedNodeMap attrs = element.getAttributes(); while (attrs.getLength() > 0) { attrs.removeNamedItem(attrs.item(0).getNodeName()); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from ww w . j a va 2 s.c o m factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); NamedNodeMap attrs = element.getAttributes(); int numAttrs = attrs.getLength(); for (int i = 0; i < numAttrs; i++) { Attr attr = (Attr) attrs.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);// www . j av a2 s .co m factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); NamedNodeMap attrs = element.getAttributes(); String[] names = new String[attrs.getLength()]; for (int i = 0; i < names.length; i++) { names[i] = attrs.item(i).getNodeName(); } for (int i = 0; i < names.length; i++) { attrs.removeNamedItem(names[i]); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<xml xmlns:log='http://sample.com'><test log:writer='someWriter'/></xml>"; StringReader xmlReader = new StringReader(xml); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from ww w .j av a2 s .c o m DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(xmlReader)); Element currentNode = (Element) doc.getElementsByTagName("test").item(0); String attributeValue = currentNode.getAttributes().getNamedItemNS("http://sample.com", "writer") .getNodeValue(); System.out.println("Attribute value is " + attributeValue); xmlReader.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//w ww.jav a2 s . c o m factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getDocumentElement(); Element element2 = doc.createElement("newname"); NamedNodeMap attrs = element.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr2 = (Attr) doc.importNode(attrs.item(i), true); element2.getAttributes().setNamedItem(attr2); } while (element.hasChildNodes()) { element2.appendChild(element.getFirstChild()); } element.getParentNode().replaceChild(element2, element); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*w w w. j av a 2 s .c om*/ DocumentBuilder db = dbf.newDocumentBuilder(); File file1 = new File("input1.xml"); Document doc1 = db.parse(file1); Element rootElement1 = doc1.getDocumentElement(); File file2 = new File("input2.xml"); Document doc2 = db.parse(file2); Element rootElement2 = doc2.getDocumentElement(); // Copy Attributes NamedNodeMap namedNodeMap2 = rootElement2.getAttributes(); for (int x = 0; x < namedNodeMap2.getLength(); x++) { Attr importedNode = (Attr) doc1.importNode(namedNodeMap2.item(x), true); rootElement1.setAttributeNodeNS(importedNode); } // Copy Child Nodes NodeList childNodes2 = rootElement2.getChildNodes(); for (int x = 0; x < childNodes2.getLength(); x++) { Node importedNode = doc1.importNode(childNodes2.item(x), true); rootElement1.appendChild(importedNode); } // Output Document TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); DOMSource source = new DOMSource(doc1); StreamResult result = new StreamResult(System.out); t.transform(source, result); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder loader = factory.newDocumentBuilder(); Document document = loader.parse("sample.xml"); Element purchaseOrder = document.getDocumentElement(); Attr orderDate = purchaseOrder.getAttributeNode("date"); System.out.println(orderDate.getValue()); NamedNodeMap attrs = purchaseOrder.getAttributes(); int attrsCount = attrs.getLength(); for (int i = 0; i < attrsCount; i++) { Attr item = (Attr) attrs.item(i); System.out.println("'" + item.getName() + "' = '" + item.getValue() + "'"); }/*from w w w . j a va 2s. co m*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*from ww w . ja v a 2 s .co m*/ DocumentBuilder db = dbf.newDocumentBuilder(); File file1 = new File("input1.xml"); Document doc1 = db.parse(file1); Element rootElement1 = doc1.getDocumentElement(); File file2 = new File("input2.xml"); Document doc2 = db.parse(file2); Element rootElement2 = doc2.getDocumentElement(); // Copy Child Nodes NodeList childNodes2 = rootElement2.getChildNodes(); for (int x = 0; x < childNodes2.getLength(); x++) { Node importedNode = doc1.importNode(childNodes2.item(x), true); if (importedNode.getNodeType() == Node.ELEMENT_NODE) { Element importedElement = (Element) importedNode; // Copy Attributes NamedNodeMap namedNodeMap2 = rootElement2.getAttributes(); for (int y = 0; y < namedNodeMap2.getLength(); y++) { Attr importedAttr = (Attr) doc1.importNode(namedNodeMap2.item(y), true); importedElement.setAttributeNodeNS(importedAttr); } } rootElement1.appendChild(importedNode); } // Output Document TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); DOMSource source = new DOMSource(doc1); StreamResult result = new StreamResult(System.out); t.transform(source, result); }
From source file:Main.java
License:asdf
public static void dom(Element element) { NamedNodeMap attrs = element.getAttributes(); while (attrs.getLength() > 0) { attrs.removeNamedItem(attrs.item(0).getNodeName()); }/* w w w . j av a 2 s. c o m*/ }
From source file:Main.java
public static boolean hasAttribute(Element e, String s) { return e.getAttributes().getNamedItem(s) != null; }