List of usage examples for org.w3c.dom Element getTagName
public String getTagName();
From source file:Main.java
public static String nodeToString(Node node) { String strNode = ""; Node nodeIt = node;/*from w w w . j ava 2s .c om*/ if (nodeIt instanceof Element) { Element elem = (Element) nodeIt; String strElem = ""; strElem += "<" + elem.getTagName(); NamedNodeMap attribs = elem.getAttributes(); int len = attribs.getLength(); for (int i = 0; i < len; i++) { Attr attr = (Attr) attribs.item(i); strElem += " " + attr.getName() + "=\"" + attr.getValue() + "\""; } strElem += ">"; strNode = strElem + strNode; } else if (nodeIt instanceof CharacterData) { CharacterData charNode = (CharacterData) nodeIt; strNode = charNode.getData() + strNode; } else if (nodeIt instanceof Document) strNode = strNode; // no hacemos nada else strNode = nodeIt.getNodeValue() + strNode; return strNode; }
From source file:Main.java
public static org.w3c.dom.Element getSingleChildElementByTagName(org.w3c.dom.Element xmlParent, String tagName) {/*from w ww. j a v a 2s .c om*/ org.w3c.dom.NodeList nodeList = xmlParent.getChildNodes(); final int N = nodeList.getLength(); for (int i = 0; i < N; i++) { org.w3c.dom.Node node = nodeList.item(i); if (node instanceof org.w3c.dom.Element) { org.w3c.dom.Element element = (org.w3c.dom.Element) node; if (tagName.equals(element.getTagName())) { return element; } } } return null; }
From source file:Main.java
public static Hashtable<String, Element> getChildHash(Element elem, String elementName, String attrName) { if (elem == null) return null; NodeList nl = elem.getChildNodes(); if (nl == null) return null; Hashtable<String, Element> retlist = new Hashtable<String, Element>(100); for (int n = 0; n < nl.getLength(); n++) { Node child = nl.item(n);/*from w w w .j av a 2 s . c o m*/ if (child instanceof Element) { Element element = (Element) child; if (!elementName.equals(element.getTagName())) continue; String keyValue = element.getAttribute(attrName); if (keyValue == null) continue; retlist.put(keyValue, element); } } if (retlist.size() == 0) return null; return retlist; }
From source file:Main.java
public static java.util.List<org.w3c.dom.Element> getChildElementsByTagName(org.w3c.dom.Element xmlParent, String tagName) {//from w ww . j av a2 s . com java.util.List<org.w3c.dom.Element> rv = new java.util.LinkedList<org.w3c.dom.Element>(); org.w3c.dom.NodeList nodeList = xmlParent.getChildNodes(); final int N = nodeList.getLength(); for (int i = 0; i < N; i++) { org.w3c.dom.Node node = nodeList.item(i); if (node instanceof org.w3c.dom.Element) { org.w3c.dom.Element element = (org.w3c.dom.Element) node; if (tagName.equals(element.getTagName())) { rv.add(element); } } } return rv; }
From source file:Main.java
public static Element recuperarReferencia(final Element parent, final String childName) { Element tmpElement = getFirstChildElement(parent); while (tmpElement != null) { if (tmpElement.getTagName().equals(childName)) { return tmpElement; }//from ww w.j a va2 s .c om tmpElement = getNextSiblingElement(tmpElement); } return null; }
From source file:Main.java
private static String name(Element elem) { String name = elem.getAttribute("name"); if (!name.isEmpty()) { return elem.getTagName() + "[" + name + "]"; } else {/*from w ww. ja v a 2 s .c o m*/ return elem.getTagName(); } }
From source file:org.bigtester.ate.xmlschema.TestCaseBeanDefinitionParser.java
private static void parseTestStepComponents(List<Element> childElements, BeanDefinitionBuilder factory, ParserContext parserContext) {//ww w.j a v a2 s .com ManagedList<BeanDefinition> children = new ManagedList<BeanDefinition>(childElements.size()); for (Element element : childElements) { if (element.getTagName() == "ate:" //NOPMD + XsdElementConstants.ELEMENT_HOMESTEP) { HomeStepBeanDefinitionParser homeStep = new HomeStepBeanDefinitionParser(); children.add(homeStep.parse(element, parserContext)); } else if (element.getTagName() == "ate:" + XsdElementConstants.ELEMENT_ELEMENTSTEP) { ElementStepBeanDefinitionParser elementStep = new ElementStepBeanDefinitionParser(); children.add(elementStep.parse(element, parserContext)); } else if (element.getTagName() == "ate:" + XsdElementConstants.ELEMENT_REPEATSTEP) { RepeatStepBeanDefinitionParser repeatStep = new RepeatStepBeanDefinitionParser(); children.add(repeatStep.parse(element, parserContext)); } else if (element.getTagName() == "ate:" + XsdElementConstants.ELEMENT_LASTSTEP) { LastStepBeanDefinitionParser lastStep = new LastStepBeanDefinitionParser(); children.add(lastStep.parse(element, parserContext)); } else if (element.getTagName() == "ate:" + XsdElementConstants.ELEMENT_CASETYPESERVICE) { CaseTypeServiceBeanDefinitionParser caseService = new CaseTypeServiceBeanDefinitionParser(); children.add(caseService.parse(element, parserContext)); } } factory.addPropertyValue(XsdElementConstants.PROP_TESTCASE_TESTSTEPLIST, children); }
From source file:Main.java
/** * Get the first child of the element by tag name. * @param element - the element whose children to look at. * @param tag - the tag name for the child * @return the child element or null if there was no child of that tag name *///www .ja v a 2 s . c o m public static Element child(Element element, String tag) { for (Element child : childElements(element, tag)) { String childTag = child.getTagName(); if (tag.equals(childTag)) { return child; } } return null; }
From source file:Main.java
/** * Resolves the xpath of an element.//w ww. ja v a 2s.c o m * * @param elt * @return * @throws IOException */ public static final String getElementXPath(Node elt) throws IOException { String path = ""; Node currentNode = elt; while (!(currentNode instanceof Document)) { Element parent = (Element) currentNode; if (!parent.getTagName().equals("schema")) { if (!parentNodeHasMoreOfThese((Element) currentNode)) { path = '/' + parent.getTagName() + path; } else { path = '/' + parent.getTagName() + '[' + getElementIdx(parent) + ']' + path; } } else { String schema = parent.getAttribute("name"); String[] segments = path.substring(1).split(":", 2); return schema + ':' + segments[segments.length - 1]; } currentNode = currentNode.getParentNode(); } throw new IOException("Failed to parse document."); }
From source file:com.atomiton.watermanagement.ngo.util.WaterMgmtNGOUtility.java
public static String getXMLElementValue(String tagName, String xmlString) { try {//from w w w . j a v a2s.co m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xmlString))); Element rootElement = document.getDocumentElement(); String rootElementTagName = rootElement.getTagName(); if (rootElementTagName.equalsIgnoreCase(tagName)) { return rootElement.getTextContent(); } else { NodeList list = rootElement.getElementsByTagName(tagName); if (list != null && list.getLength() > 0) { NodeList subList = list.item(0).getChildNodes(); if (subList != null && subList.getLength() > 0) { return subList.item(0).getNodeValue(); } } } } catch (Exception e) { e.printStackTrace(); } return null; }