@Test public void testGetElement() { System.out.println("getElement"); String xml = "<?xml version='1.0' encoding='UTF-8' ?><rootElement><sample>sometest</sample></rootElement>"; InputStream in = new ByteArrayInputStream(xml.getBytes()); SAXBuilder saxb = new SAXBuilder(); Document doc = null; try { doc = saxb.build(in); } catch (JDOMException jDOMException) { System.out.println("JDOMException: " + jDOMException.getMessage()); } catch (IOException iOException) { System.out.println("IOExceptionL " + iOException.getMessage()); } String tagName = "sample"; XMLUtil instance = new XMLUtil(); String expResult = "sometest"; Element result = instance.getElement(doc, tagName); assertEquals(expResult, result.getText()); }
public Element getElement(Document doc, String tagName) { Element result = null; if (tagName != null) { Element root = doc.getRootElement(); if (tagName.equals(root.getName())) { result = root; } else { List children = root.getChildren(); for(Object o : children) { Element e = (Element) o; if (e.getName() != null && e.getName().equals(tagName)) { result = e; break; } } } } return result; }
XMLUtil { public Element getElement(Document doc, String tagName) { Element result = null; if (tagName != null) { Element root = doc.getRootElement(); if (tagName.equals(root.getName())) { result = root; } else { List children = root.getChildren(); for(Object o : children) { Element e = (Element) o; if (e.getName() != null && e.getName().equals(tagName)) { result = e; break; } } } } return result; } }
XMLUtil { public Element getElement(Document doc, String tagName) { Element result = null; if (tagName != null) { Element root = doc.getRootElement(); if (tagName.equals(root.getName())) { result = root; } else { List children = root.getChildren(); for(Object o : children) { Element e = (Element) o; if (e.getName() != null && e.getName().equals(tagName)) { result = e; break; } } } } return result; }  }
XMLUtil { public Element getElement(Document doc, String tagName) { Element result = null; if (tagName != null) { Element root = doc.getRootElement(); if (tagName.equals(root.getName())) { result = root; } else { List children = root.getChildren(); for(Object o : children) { Element e = (Element) o; if (e.getName() != null && e.getName().equals(tagName)) { result = e; break; } } } } return result; }  Element getElement(Document doc, String tagName); }
XMLUtil { public Element getElement(Document doc, String tagName) { Element result = null; if (tagName != null) { Element root = doc.getRootElement(); if (tagName.equals(root.getName())) { result = root; } else { List children = root.getChildren(); for(Object o : children) { Element e = (Element) o; if (e.getName() != null && e.getName().equals(tagName)) { result = e; break; } } } } return result; }  Element getElement(Document doc, String tagName);  }