List of usage examples for org.w3c.dom Element hasChildNodes
public boolean hasChildNodes();
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);// ww w . ja v a 2 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 removeChilds(Element e) { while (e.hasChildNodes()) e.removeChild(e.getFirstChild()); }
From source file:Main.java
public static String getTextContents(Element e) { if (!e.hasChildNodes()) return null; StringBuffer buf = new StringBuffer(); NodeList list = e.getChildNodes(); int len = list.getLength(); String text;//from w w w .j a v a 2 s. c o m for (int i = 0; i < len; i++) { text = ((Node) list.item(i)).getNodeValue(); if (text != null) buf.append(text); } return buf.toString(); }
From source file:Main.java
public static void insertBeforeFirstChild(Element object, Element attrId) { if (object.hasChildNodes()) { object.insertBefore(attrId, object.getFirstChild()); } else {//from w w w . j av a2s.c o m object.appendChild(attrId); } }
From source file:Main.java
public static void addAsFirstChild(Element element, Element newChild) { if (element.hasChildNodes()) { element.insertBefore(newChild, element.getChildNodes().item(0)); } else {/*w ww . j a v a2 s .co m*/ element.appendChild(newChild); } }
From source file:Main.java
/** * @param elem//from w w w . ja va 2 s. co m * @param childTag * @return */ public static Element getFirstChild(Element elem, String childTag) { if (elem.hasChildNodes()) { NodeList list = elem.getElementsByTagName(childTag); int count = list.getLength(); for (int i = 0; i < count; i++) { Node node = list.item(i); if (node.getParentNode() != elem) continue; if (node.getNodeType() == Node.ELEMENT_NODE) { return (Element) node; } } } return null; }
From source file:Main.java
/** * Returns true if element node has children elements * @param actElement//from ww w .j a v a 2 s . c om * @return */ protected static boolean hasChildElementNodes(Element actElement) { try { if (actElement.hasChildNodes()) { NodeList childNodes = actElement.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node element = childNodes.item(i); if (element.getNodeType() == Node.ELEMENT_NODE) { return true; } } } return false; } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:Main.java
/** Helper method - Determines if a <I>Node</I> has any non-<I>CharacterData</I> nodes. This call is stricter than the <CODE>Node.hasChildNodes</CODE> call. *//* w ww . ja v a2 s.c om*/ public static boolean hasNonCharacterChildren(Element pElement) { // Do the quick test. if (!pElement.hasChildNodes()) return false; NodeList pNodeList = pElement.getChildNodes(); int nLength = pNodeList.getLength(); // Find a non-character data node. for (int i = 0; i < nLength; i++) if (!(pNodeList.item(i) instanceof CharacterData)) return true; return false; }
From source file:Main.java
public static Element GetCustomer(JFrame mainFrame, long lookupValue, Document CustomerDoc) { Element Customer = null;/*from w w w .ja v a 2s .com*/ Element Root = CustomerDoc.getDocumentElement(); if (Root.hasChildNodes()) { NodeList Customers = Root.getChildNodes(); for (int i = 0; i < Customers.getLength(); i++) { if (Customers.item(i).getNodeName() == "#text") continue; Element Current = (Element) Customers.item(i); if (lookupValue == Integer.parseInt(Current.getAttribute("Id"))) { Customer = Current; } } } return Customer; }
From source file:Main.java
public static List<Node> contents(Element element) { if (element == null || !element.hasChildNodes()) { return Collections.emptyList(); }//from w ww. jav a 2s . c o m List<Node> contents = new ArrayList<Node>(); for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { contents.add(child); } return contents; }