List of usage examples for org.w3c.dom Element getFirstChild
public Node getFirstChild();
From source file:DOMImport.java
public void importName(Document doc1, Document doc2) { Element root1 = doc1.getDocumentElement(); Element personInDoc1 = (Element) root1.getFirstChild(); Node importedPerson = doc2.importNode(personInDoc1, true); Element root2 = doc2.getDocumentElement(); root2.appendChild(importedPerson);// www . j a va 2 s . co m }
From source file:com.rapidminer.gui.OperatorDocLoader.java
/** * //from w w w .j av a 2 s . c o m * @param operatorWikiName * @param opDesc * @return The parsed <tt>Document</tt> (not finally parsed) of the selected operator. * @throws MalformedURLException * @throws ParserConfigurationException */ private static Document parseDocumentForOperator(String operatorWikiName, OperatorDescription opDesc) throws MalformedURLException, ParserConfigurationException { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setIgnoringComments(true); builderFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); documentBuilder.setEntityResolver(new XHTMLEntityResolver()); Document document = null; URL url = new URL(WIKI_PREFIX_FOR_OPERATORS + operatorWikiName); if (url != null) { try { document = documentBuilder.parse(WebServiceTools.openStreamFromURL(url)); } catch (IOException e) { logger.warning("Could not open " + url.toExternalForm() + ": " + e.getMessage()); } catch (SAXException e) { logger.warning("Could not parse operator documentation: " + e.getMessage()); } int i = 0; if (document != null) { Element contentElement = document.getElementById("content"); // removing content element from document if (contentElement != null) { contentElement.getParentNode().removeChild(contentElement); } // removing everything from body NodeList bodies = document.getElementsByTagName("body"); for (int k = 0; k < bodies.getLength(); k++) { Node body = bodies.item(k); while (body.hasChildNodes()) { body.removeChild(body.getFirstChild()); } // read content element to body if (contentElement != null && k == 0) { body.appendChild(contentElement); } } // removing everything from head NodeList heads = document.getElementsByTagName("head"); for (int k = 0; k < heads.getLength(); k++) { Node head = heads.item(k); while (head.hasChildNodes()) { head.removeChild(head.getFirstChild()); } } // removing...<head/> from document if (heads != null) { while (i < heads.getLength()) { Node head = heads.item(i); head.getParentNode().removeChild(head); } } // removing jump-to-nav element from document Element jumpToNavElement = document.getElementById("jump-to-nav"); if (jumpToNavElement != null) { jumpToNavElement.getParentNode().removeChild(jumpToNavElement); } // removing mw-normal-catlinks element from document Element mwNormalCatlinksElement = document.getElementById("mw-normal-catlinks"); if (mwNormalCatlinksElement != null) { mwNormalCatlinksElement.getParentNode().removeChild(mwNormalCatlinksElement); } // removing complete link navigation Element tocElement = document.getElementById("toc"); if (tocElement != null) { tocElement.getParentNode().removeChild(tocElement); } // removing everything from class printfooter NodeList nodeListDiv = document.getElementsByTagName("div"); for (int k = 0; k < nodeListDiv.getLength(); k++) { Element div = (Element) nodeListDiv.item(k); if (div.getAttribute("class").equals("printfooter")) { div.getParentNode().removeChild(div); } } // removing everything from class editsection NodeList spanList = document.getElementsByTagName("span"); for (int k = 0; k < spanList.getLength(); k++) { Element span = (Element) spanList.item(k); if (span.getAttribute("class").equals("editsection")) { span.getParentNode().removeChild(span); } } // Synopsis Header boolean doIt = true; NodeList pList = document.getElementsByTagName("p"); for (int k = 0; k < pList.getLength(); k++) { if (doIt) { Node p = pList.item(k); NodeList pChildList = p.getChildNodes(); for (int j = 0; j < pChildList.getLength(); j++) { Node pChild = pChildList.item(j); if (pChild.getNodeType() == Node.TEXT_NODE && pChild.getNodeValue() != null && StringUtils.isNotBlank(pChild.getNodeValue()) && StringUtils.isNotEmpty(pChild.getNodeValue())) { String pChildString = pChild.getNodeValue(); Element newPWithoutSpaces = document.createElement("p"); newPWithoutSpaces.setTextContent(pChildString); Node synopsis = document.createTextNode("Synopsis"); Element span = document.createElement("span"); span.setAttribute("class", "mw-headline"); span.setAttribute("id", "Synopsis"); span.appendChild(synopsis); Element h2 = document.createElement("h2"); h2.appendChild(span); Element div = document.createElement("div"); div.setAttribute("id", "synopsis"); div.appendChild(h2); div.appendChild(newPWithoutSpaces); Node pChildParentParent = pChild.getParentNode().getParentNode(); Node pChildParent = pChild.getParentNode(); pChildParentParent.replaceChild(div, pChildParent); doIt = false; break; } } } else { break; } } // removing all <br...>-Tags NodeList brList = document.getElementsByTagName("br"); while (i < brList.getLength()) { Node br = brList.item(i); Node parentBrNode = br.getParentNode(); parentBrNode.removeChild(br); } // removing everything from script NodeList scriptList = document.getElementsByTagName("script"); while (i < scriptList.getLength()) { Node scriptNode = scriptList.item(i); Node parentNode = scriptNode.getParentNode(); parentNode.removeChild(scriptNode); } // removing all empty <p...>-Tags NodeList pList2 = document.getElementsByTagName("p"); int ccc = 0; while (ccc < pList2.getLength()) { Node p = pList2.item(ccc); NodeList pChilds = p.getChildNodes(); int kk = 0; while (kk < pChilds.getLength()) { Node pChild = pChilds.item(kk); if (pChild.getNodeType() == Node.TEXT_NODE) { String pNodeValue = pChild.getNodeValue(); if (pNodeValue == null || StringUtils.isBlank(pNodeValue) || StringUtils.isEmpty(pNodeValue)) { kk++; } else { ccc++; break; } } else { ccc++; break; } if (kk == pChilds.getLength()) { Node parentBrNode = p.getParentNode(); parentBrNode.removeChild(p); } } } // removing firstHeading element from document Element firstHeadingElement = document.getElementById("firstHeading"); if (firstHeadingElement != null) { CURRENT_OPERATOR_NAME_READ_FROM_RAPIDWIKI = firstHeadingElement.getFirstChild().getNodeValue() .replaceFirst(".*:", ""); firstHeadingElement.getParentNode().removeChild(firstHeadingElement); } // setting operator plugin name if (opDesc != null && opDesc.getProvider() != null) { CURRENT_OPERATOR_PLUGIN_NAME = opDesc.getProvider().getName(); } // removing sitesub element from document Element siteSubElement = document.getElementById("siteSub"); if (siteSubElement != null) { siteSubElement.getParentNode().removeChild(siteSubElement); } // removing contentSub element from document Element contentSubElement = document.getElementById("contentSub"); if (contentSubElement != null) { contentSubElement.getParentNode().removeChild(contentSubElement); } // removing catlinks element from document Element catlinksElement = document.getElementById("catlinks"); if (catlinksElement != null) { catlinksElement.getParentNode().removeChild(catlinksElement); } // removing <a...> element from document, if they are empty NodeList aList = document.getElementsByTagName("a"); if (aList != null) { int k = 0; while (k < aList.getLength()) { Node a = aList.item(k); Element aElement = (Element) a; if (aElement.getAttribute("class").equals("internal")) { a.getParentNode().removeChild(a); } else { Node aChild = a.getFirstChild(); if (aChild != null && (aChild.getNodeValue() != null && aChild.getNodeType() == Node.TEXT_NODE && StringUtils.isNotBlank(aChild.getNodeValue()) && StringUtils.isNotEmpty(aChild.getNodeValue()) || aChild.getNodeName() != null)) { Element aChildElement = null; if (aChild.getNodeName().startsWith("img")) { aChildElement = (Element) aChild; Element imgElement = document.createElement("img"); imgElement.setAttribute("alt", aChildElement.getAttribute("alt")); imgElement.setAttribute("class", aChildElement.getAttribute("class")); imgElement.setAttribute("height", aChildElement.getAttribute("height")); imgElement.setAttribute("src", WIKI_PREFIX_FOR_IMAGES + aChildElement.getAttribute("src")); imgElement.setAttribute("width", aChildElement.getAttribute("width")); imgElement.setAttribute("border", "1"); Node aParent = a.getParentNode(); aParent.replaceChild(imgElement, a); } else { k++; } } else { a.getParentNode().removeChild(a); } } } } } } return document; }
From source file:Main.java
public static void stripDuplicateAttributes(Node node, Node parent) { // The output depends on the type of the node switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document doc = (Document) node; Node child = doc.getFirstChild(); while (child != null) { stripDuplicateAttributes(child, node); child = child.getNextSibling(); }/*from w w w . j a v a 2 s. c om*/ break; } case Node.ELEMENT_NODE: { Element elt = (Element) node; NamedNodeMap attrs = elt.getAttributes(); ArrayList nodesToRemove = new ArrayList(); int nodesToRemoveNum = 0; for (int i = 0; i < attrs.getLength(); i++) { final Node a = attrs.item(i); for (int j = 0; j < attrs.getLength(); j++) { final Node b = attrs.item(j); //if there are two attributes with same name if (i != j && (a.getNodeName().equals(b.getNodeName()))) { nodesToRemove.add(b); nodesToRemoveNum++; } } } for (int i = 0; i < nodesToRemoveNum; i++) { Attr nodeToDelete = (Attr) nodesToRemove.get(i); Element nodeToDeleteParent = (Element) node; // nodeToDelete.getParentNode(); nodeToDeleteParent.removeAttributeNode(nodeToDelete); } nodesToRemove.clear(); Node child = elt.getFirstChild(); while (child != null) { stripDuplicateAttributes(child, node); child = child.getNextSibling(); } break; } default: //do nothing break; } }
From source file:com.viadee.acceptancetests.roo.addon.PluginDependency.java
private void removeAllChildNodes(Element element) { while (element.hasChildNodes()) { element.removeChild(element.getFirstChild()); }//from w w w . j a v a 2s .com }
From source file:org.carewebframework.shell.BaseXmlParser.java
/** * Find the child element whose tag matches the specified tag name. * //from w w w . j a va 2 s . co m * @param tagName Tag name to locate. * @param element Parent element whose children are to be searched. * @return The matching node (first occurrence only) or null if not found. */ protected Element findTag(String tagName, Element element) { Node result = element.getFirstChild(); while (result != null) { if (result instanceof Element && (tagName.equals(((Element) result).getNodeName()) || tagName.equals(((Element) result).getLocalName()))) { break; } result = result.getNextSibling(); } return (Element) result; }
From source file:net.frontlinesms.plugins.forms.FormsPluginController.java
/** * Fabaris_a.zanchi Static utility method to extract tag contents from a xml * * @param xmlContent The xml conent// w w w . jav a2 s.c o m * @param tag the specified xml tag we want to fetch contents from * @return a List of Strings with the tags' content */ public static List<String> getTagContentFromXml(String xmlContent, String tag) throws Exception { List<String> ret = new ArrayList<String>(); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); InputSource inStream = new InputSource(); inStream.setCharacterStream(new StringReader(xmlContent)); org.w3c.dom.Document xmlDoc = docBuilder.parse(inStream); xmlDoc.getDocumentElement().normalize(); if (!tag.equals("client_version")) { NodeList nodeList = xmlDoc.getElementsByTagName(tag); for (int i = 0; i < nodeList.getLength(); i++) { Node dataNode = nodeList.item(i); Element elem = (Element) dataNode; ret.add(elem.getFirstChild().getNodeValue()); } } else { // Search for client version not knowing its index NodeList dataNode = xmlDoc.getElementsByTagName("data"); Element dataElem = (Element) dataNode.item(0); NodeList allNodes = dataElem.getChildNodes(); for (int i = 0; i < allNodes.getLength(); i++) { if (allNodes.item(i).getNodeName().contains("client_version")) { ret.add(allNodes.item(i).getTextContent()); } } } return ret; }
From source file:org.wikipedia.vlsergey.secretary.jwpf.actions.Edit.java
@Override protected void parseAPI(Element root) throws ProcessException, ParseException { String result = ((Element) root.getFirstChild()).getAttribute("result"); if (!result.equals("Success")) throw new ProcessException(result); }
From source file:de.betterform.xml.dom.DOMUtil.java
/** * Returns the node value of the element's first child if there is any, * otherwise <code>null</code>. * * @param element the element.//from w w w . j a v a2s . c o m * @return the element's value. */ public static String getElementValue(Element element) { Node child = element.getFirstChild(); if (child != null) { return child.getNodeValue(); } return null; }
From source file:net.frontlinesms.plugins.forms.FormsPluginController.java
/** * Fabaris_a.zanchi This method extracts the Form Id from the "id" attribute * of <data> tag// w ww .ja v a 2 s. c om * * @param xmlContent String who contains the xml * @return a String representing the form ID * @throws Exception */ public static String getFormIdFromXml(String xmlContent) throws Exception { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); InputSource inStream = new InputSource(); inStream.setCharacterStream(new StringReader(xmlContent)); org.w3c.dom.Document xmlDoc = docBuilder.parse(inStream); xmlDoc.getDocumentElement().normalize(); NodeList nodeList = xmlDoc.getElementsByTagName("id"); Node dataNode = nodeList.item(0); Element elem = (Element) dataNode; // String id = elem.getAttribute("id"); String id = elem.getFirstChild().getNodeValue(); return id; }
From source file:com.sap.research.connectivity.gw.parsers.MetadataXMLParser.java
private String getTextValue(Element ele, String tagName) { String textVal = null;//from w w w. ja va 2 s. co m NodeList nl = ele.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { Element el = (Element) nl.item(0); textVal = el.getFirstChild().getNodeValue(); } return textVal; }