Java Code Examples for org.w3c.dom.Node.hasChildNodes()

The following are Jave code examples for showing how to use hasChildNodes() of the org.w3c.dom.Node class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: ChronoBike   File: XmlHelper.java   View Source Code Vote up 6 votes
static public String GetNodeText(Node parent) {
	String s;
	StringBuffer value;
	NodeList nodes;
	Node node;
	int n;

	value=new StringBuffer(""); 
	if (parent.hasChildNodes()) {
		nodes=parent.getChildNodes();
		for(n=0;n<nodes.getLength();n++) { 
			node=nodes.item(n);
			if (node.getNodeType()==Node.TEXT_NODE)
				value.append(node.getNodeValue());
			if (node.hasChildNodes())
				value.append(GetNodeText(node));
		}
	} else {
		s=parent.getNodeValue();
		if (s!=null) return s; else return "";
	}
	return value.toString();
}
 
Example 2
Project: vexillo   File: FlagParser.java   View Source Code Vote up 6 votes
private static Flag parseDocument(Node node) throws IOException {
	String type = node.getNodeName();
	if (type.equalsIgnoreCase("#document")) {
		for (Node child : getChildren(node)) {
			String ctype = child.getNodeName();
			if (ctype.equalsIgnoreCase("flag")) {
				if (child.hasAttributes() || child.hasChildNodes()) {
					return parseFlag(child);
				}
			} else {
				throw new IOException("Unknown element: " + ctype);
			}
		}
		throw new IOException("Empty document.");
	} else {
		throw new IOException("Unknown element: " + type);
	}
}
 
Example 3
Project: EspressoViews   File: QueryDefinitionParser.java   View Source Code Vote up 6 votes
public static  LinkedList<Map<String,String>> loadIniSettings(String filePath){ 
	LinkedList<Map<String,String>> xmlExtrakt = new LinkedList<Map<String,String>>();
	Document doc= getXmlDocument(filePath);	
	Map<String,String> tagsAndContents = new HashMap<String,String>();

	Node lastDir = getElementByTagName(doc,"LASTDIR" );
	if (lastDir!=null &&lastDir.hasChildNodes()){
		String lastDirLocator = lastDir.getFirstChild().getNodeValue();
		if (lastDirLocator!=null && lastDirLocator.trim()!=null){
		tagsAndContents.put("LASTDIR",lastDirLocator);
		xmlExtrakt.add(tagsAndContents);
		}
	}
	
	Node warnLvlNode = getElementByTagName(doc,"WARNINGLEVEL" );
	if (warnLvlNode!=null && warnLvlNode.hasChildNodes()){
		String warnLvl = warnLvlNode.getFirstChild().getNodeValue();
		if (warnLvl!=null && warnLvl.trim()!=null){
		tagsAndContents.put("WARNINGLEVEL",warnLvl);
		xmlExtrakt.add(tagsAndContents);
		}
	}
	
	//System.out.println(xmlExtrakt);
    return xmlExtrakt;
}
 
Example 4
Project: EspressoViews   File: QueryDefinitionParser.java   View Source Code Vote up 6 votes
public static void storeIniSettings( String iniFile, String iniSettingName,String iniSettingValue){
	Document doc= getXmlDocument((new File (iniFile)).getAbsolutePath() );	
	Node lastDir = getElementByTagName(doc,iniSettingName );
	if (lastDir==null ){//neues ini setting tag kreieren
		Element neuesElement = doc.createElement(iniSettingName);
		neuesElement.appendChild(doc.createTextNode(iniSettingValue));
		getElementByTagName(doc,"ROOT" ).appendChild(neuesElement);
	}
	else 
		if (lastDir.hasChildNodes()){//altes ini setting ´┐Żberschreiben
			lastDir.getFirstChild().setTextContent(iniSettingValue); 
		}
	if (lastDir!= null && !lastDir.hasChildNodes()){//ini setting im leeren tag schreiben 
		lastDir.appendChild(doc.createTextNode(iniSettingValue)); 
	}
	storeXmlDocToFile(iniFile, doc);	
}
 
Example 5
Project: openjdk-jdk10   File: SOAPDocumentImpl.java   View Source Code Vote up 6 votes
@Override
public org.w3c.dom.Node importNode(Node importedNode, boolean deep)
    throws DOMException {
    Node domNode = getDomNode(importedNode);
    final Node newNode = document.importNode(domNode, deep);

    if (importedNode instanceof javax.xml.soap.Node) {
        Node newSoapNode = createSoapNode(importedNode.getClass(), newNode);
        newNode.setUserData(SAAJ_NODE, newSoapNode, null);
        if (deep && importedNode.hasChildNodes()) {
            NodeList childNodes = importedNode.getChildNodes();
            for (int i = 0; i < childNodes.getLength(); i++) {
                registerChildNodes(childNodes.item(i), deep);
            }
        }
        return newSoapNode;
    }

    registerChildNodes(newNode, deep);
    return findIfPresent(newNode);
}
 
Example 6
Project: convertigo-engine   File: XMLUtils.java   View Source Code Vote up 6 votes
public static String getNormalizedText(Node node) {
	String res = "";
	if (node.hasChildNodes()) {
		NodeList nl = node.getChildNodes();
		for (int i = 0; i < nl.getLength(); i++) {
			if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
				res += nl.item(i).getNodeValue();
			} else {
				// ignore <SCRIPT> nodes ...
				if (!nl.item(i).getNodeName().equalsIgnoreCase("script"))
					res += getNormalizedText(nl.item(i));
			}
		}
	}
	return res;
}
 
Example 7
Project: FXMLHelper   File: ControllerGeneratorAction.java   View Source Code Vote up 5 votes
private List<Node> findChildrenNodes(Node node) {
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        Node n = node.getChildNodes().item(i);
        nodes.add(n);
        if (n.hasChildNodes()) {
            nodes.addAll(findChildrenNodes(n));
        }
    }
    return nodes;
}
 
Example 8
Project: openjdk-jdk10   File: DTMNodeProxy.java   View Source Code Vote up 5 votes
/**
 *
 * @param listVector
 * @param tempNode
 * @param tagname
 * @param isTagNameWildCard
 *
 *
 * Private method to be used for recursive iterations to obtain elements by tag name.
 */
private final void traverseChildren
(
  Vector listVector,
  Node tempNode,
  String tagname,
  boolean isTagNameWildCard) {
  if (tempNode == null)
  {
    return;
  }
  else
  {
    if (tempNode.getNodeType() == DTM.ELEMENT_NODE
          && (isTagNameWildCard || tempNode.getNodeName().equals(tagname)))
    {
      listVector.add(tempNode);
    }
    if(tempNode.hasChildNodes())
    {
      NodeList nodeList = tempNode.getChildNodes();
      for (int i = 0; i < nodeList.getLength(); i++)
      {
        traverseChildren(listVector, nodeList.item(i), tagname,
                         isTagNameWildCard);
      }
    }
  }
}
 
Example 9
Project: EspressoViews   File: QueryDefinitionParser.java   View Source Code Vote up 5 votes
public static void showMeTheDoc(Node node, int level ){
	//if (node.getNodeType()==1)
	if (node.hasAttributes()) System.out.println(node.getAttributes());
    System.out.println("Level "+level+DisplayData.multiplyChars(' ',(level-1)*10)+" Der Knoten "+node.getNodeName()
    		//+" mit dem Inhalt "+" Node Text Content: "+node.getTextContent()
    		+" und mit dem Node Type "+node.getNodeType()+" und node value: "+node.getNodeValue()
    		+" hat "+node.getChildNodes().getLength()+" Kinder "
    		);
    	if (node.hasChildNodes()){
			 NodeList children=  node.getChildNodes();
		 for (int t = 0;t<children.getLength();t++){
			showMeTheDoc(children.item(t), level+1);
		 }
	   }
}
 
Example 10
Project: openjdk-jdk10   File: TIFFImageMetadata.java   View Source Code Vote up 5 votes
private Node getChildNode(Node node, String childName) {
    Node childNode = null;
    if(node.hasChildNodes()) {
        NodeList childNodes = node.getChildNodes();
        int length = childNodes.getLength();
        for(int i = 0; i < length; i++) {
            Node item = childNodes.item(i);
            if(item.getNodeName().equals(childName)) {
                childNode = item;
                break;
            }
        }
    }
    return childNode;
}
 
Example 11
Project: Cognizant-Intelligent-Test-Scripter   File: XMLOperation.java   View Source Code Vote up 5 votes
public static void removeAll(Node node) {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        if (n.hasChildNodes()) {
            removeAll(n);
            node.removeChild(n);
        } else {
            node.removeChild(n);
        }
    }
}
 
Example 12
Project: Artificial-Intelligent-chat-bot-   File: AIMLProcessor.java   View Source Code Vote up 5 votes
/**
 * evaluate tag contents and swap 1st and 2nd person pronouns
 * implements {@code <person>} tag
 *
 * @param node     current XML parse node
 * @param ps       AIML parse state
 * @return         sentence with pronouns swapped
 */
private static String person(Node node, ParseState ps) {
    String result;
    if (node.hasChildNodes())
      result = evalTagContent(node, ps, null);
    else result = ps.starBindings.inputStars.star(0);   // for <person/>
    result = " "+result+" ";
    result = ps.chatSession.bot.preProcessor.person(result);
    return result.trim();
}
 
Example 13
Project: convertigo-engine   File: Sequence.java   View Source Code Vote up 5 votes
private static Node cloneNodeWithUserData(Node node, boolean recurse) {
	if (node != null) {
		Object node_output = node.getUserData(Step.NODE_USERDATA_OUTPUT);

		Node clonedNode = node.cloneNode(false);
		clonedNode.setUserData(Step.NODE_USERDATA_OUTPUT, node_output, null);
		
		if (node.getNodeType() == Node.ELEMENT_NODE) {
			// attributes
			NamedNodeMap attributeMap = clonedNode.getAttributes();
			for (int i=0; i< attributeMap.getLength(); i++) {
				Node clonedAttribute = attributeMap.item(i);
				String attr_name = clonedAttribute.getNodeName();
				Object attr_output = ((Element)node).getAttributeNode(attr_name).getUserData(Step.NODE_USERDATA_OUTPUT);
				clonedAttribute.setUserData(Step.NODE_USERDATA_OUTPUT, attr_output, null);
			}
			
			// recurse on element child nodes only
			if (recurse && node.hasChildNodes()) {
				NodeList list = node.getChildNodes();
				for (int i=0; i<list.getLength(); i++) {
					Node clonedChild = cloneNodeWithUserData(list.item(i), recurse);
					if (clonedChild != null) {
						clonedNode.appendChild(clonedChild);
					}
				}
			}
		}
		
		return clonedNode;
	}
	return null;
}
 
Example 14
Project: OpenJSharp   File: DTMNodeProxy.java   View Source Code Vote up 5 votes
/**
 *
 * @param listVector
 * @param tempNode
 * @param namespaceURI
 * @param localname
 * @param isNamespaceURIWildCard
 * @param isLocalNameWildCard
 *
 * Private method to be used for recursive iterations to obtain elements by tag name
 * and namespaceURI.
 */
private final void traverseChildren
(
 Vector listVector,
 Node tempNode,
 String namespaceURI,
 String localname,
 boolean isNamespaceURIWildCard,
 boolean isLocalNameWildCard)
 {
  if (tempNode == null)
  {
    return;
  }
  else
  {
    if (tempNode.getNodeType() == DTM.ELEMENT_NODE
            && (isLocalNameWildCard
                    || tempNode.getLocalName().equals(localname)))
    {
      String nsURI = tempNode.getNamespaceURI();
      if ((namespaceURI == null && nsURI == null)
             || isNamespaceURIWildCard
             || (namespaceURI != null && namespaceURI.equals(nsURI)))
      {
        listVector.add(tempNode);
      }
    }
    if(tempNode.hasChildNodes())
    {
      NodeList nl = tempNode.getChildNodes();
      for(int i = 0; i < nl.getLength(); i++)
      {
        traverseChildren(listVector, nl.item(i), namespaceURI, localname,
                         isNamespaceURIWildCard, isLocalNameWildCard);
      }
    }
  }
}
 
Example 15
Project: openjdk-jdk10   File: TreeWalkerImpl.java   View Source Code Vote up 5 votes
/** Internal function.
 *  Return the last child Node, from the input node
 *  after applying filter, whatToshow.
 *  The current node is not consulted or set.
 */
Node getLastChild(Node node) {

    if (node == null) return null;

    if ( !fEntityReferenceExpansion
         && node.getNodeType() == Node.ENTITY_REFERENCE_NODE)
        return null;

    Node newNode = node.getLastChild();
    if (newNode == null)  return null;

    int accept = acceptNode(newNode);

    if (accept == NodeFilter.FILTER_ACCEPT)
        return newNode;
    else
    if (accept == NodeFilter.FILTER_SKIP
        && newNode.hasChildNodes())
    {
        Node lChild = getLastChild(newNode);
        if (lChild == null) {
            return getPreviousSibling(newNode, node);
        }
        return lChild;
    }
    else
    //if (accept == NodeFilter.REJECT_NODE)
    {
        return getPreviousSibling(newNode, node);
    }


}
 
Example 16
Project: openjdk-jdk10   File: DOM2DTM.java   View Source Code Vote up 5 votes
/** Utility function: Given a DOM Text node, determine whether it is
 * logically followed by another Text or CDATASection node. This may
 * involve traversing into Entity References.
 *
 * %REVIEW% DOM Level 3 is expected to add functionality which may
 * allow us to retire this.
 */
private Node logicalNextDOMTextNode(Node n)
{
      Node p=n.getNextSibling();
      if(p==null)
      {
              // Walk out of any EntityReferenceNodes that ended with text
              for(n=n.getParentNode();
                      n!=null && ENTITY_REFERENCE_NODE == n.getNodeType();
                      n=n.getParentNode())
              {
                      p=n.getNextSibling();
                      if(p!=null)
                              break;
              }
      }
      n=p;
      while(n!=null && ENTITY_REFERENCE_NODE == n.getNodeType())
      {
              // Walk into any EntityReferenceNodes that start with text
              if(n.hasChildNodes())
                      n=n.getFirstChild();
              else
                      n=n.getNextSibling();
      }
      if(n!=null)
      {
              // Found a logical next sibling. Is it text?
              int ntype=n.getNodeType();
              if(TEXT_NODE != ntype && CDATA_SECTION_NODE != ntype)
                      n=null;
      }
      return n;
}
 
Example 17
Project: convertigo-engine   File: CicsTransaction.java   View Source Code Vote up 5 votes
private String elementToHTMLTag(Element elt) {
	String sTags = "";
	if (elt != null) {
		if (elt.hasChildNodes()) {
			NodeList children = elt.getChildNodes();
			int len = children.getLength();
			if (len > 0) {
				for (int i=0; i<len; i++) {
					Node child = children.item(i);
					int type = child.getNodeType();
					switch (type) {
						case Node.ELEMENT_NODE:
							String sOcc = ((Element)child).getAttribute("occurence");
							sOcc = ((sOcc == null) ? "":sOcc);
							boolean bHeader = !sOcc.equalsIgnoreCase("");
							sTags += "<TR>";
							if (child.hasChildNodes()) {
								if (child.getFirstChild().getNodeType() == Node.TEXT_NODE)
									sTags += "<TD>" + addInput(((Element)child).getTagName(),((Element)child).getAttribute("bytes"),child.getFirstChild().getNodeValue()) + "</TD>";
								else {
									sTags += "<TD colspan='2'><TABLE border='1'>";
									sTags += (bHeader ? "<TR><TH colspan='2'>"+ ((Element)child).getTagName() + ": " + sOcc +"</TH></TR>":"");
									sTags += elementToHTMLTag((Element)child);
									sTags += "</TABLE></TD>";
								}
							}
							else {
								sTags += "<TD>" + addInput(((Element)child).getTagName(),((Element)child).getAttribute("bytes"),"") + "</TD>";
							}
							sTags += "</TR>";
							break;
						default:
							break;
					}
				}
			}
		}
	}
	return sTags; 
}
 
Example 18
Project: openjdk-jdk10   File: NodeIteratorImpl.java   View Source Code Vote up 5 votes
/** The method nextNode(Node, boolean) returns the next node
 *  from the actual DOM tree.
 *
 *  The boolean visitChildren determines whether to visit the children.
 *  The result is the nextNode.
 */
Node nextNode(Node node, boolean visitChildren) {

    if (node == null) return fRoot;

    Node result;
    // only check children if we visit children.
    if (visitChildren) {
        //if hasChildren, return 1st child.
        if (node.hasChildNodes()) {
            result = node.getFirstChild();
            return result;
        }
    }

    if (node == fRoot) { //if Root has no kids
        return null;
    }

    // if hasSibling, return sibling
    result = node.getNextSibling();
    if (result != null) return result;


    // return parent's 1st sibling.
    Node parent = node.getParentNode();
    while (parent != null && parent != fRoot) {
        result = parent.getNextSibling();
        if (result != null) {
            return result;
        } else {
            parent = parent.getParentNode();
        }

    } // while (parent != null && parent != fRoot) {

    // end of list, return null
    return null;
}
 
Example 19
Project: OpenJSharp   File: JFIFMarkerSegment.java   View Source Code Vote up 4 votes
/**
 * Updates the data in this object from the given DOM Node tree.
 * If fromScratch is true, this object is being constructed.
 * Otherwise an existing object is being modified.
 * Throws an IIOInvalidTreeException if the tree is invalid in
 * any way.
 */
void updateFromNativeNode(Node node, boolean fromScratch)
    throws IIOInvalidTreeException {
    // none of the attributes are required
    NamedNodeMap attrs = node.getAttributes();
    if (attrs.getLength() > 0) {
        int value = getAttributeValue(node, attrs, "majorVersion",
                                      0, 255, false);
        majorVersion = (value != -1) ? value : majorVersion;
        value = getAttributeValue(node, attrs, "minorVersion",
                                  0, 255, false);
        minorVersion = (value != -1) ? value : minorVersion;
        value = getAttributeValue(node, attrs, "resUnits", 0, 2, false);
        resUnits = (value != -1) ? value : resUnits;
        value = getAttributeValue(node, attrs, "Xdensity", 1, 65535, false);
        Xdensity = (value != -1) ? value : Xdensity;
        value = getAttributeValue(node, attrs, "Ydensity", 1, 65535, false);
        Ydensity = (value != -1) ? value : Ydensity;
        value = getAttributeValue(node, attrs, "thumbWidth", 0, 255, false);
        thumbWidth = (value != -1) ? value : thumbWidth;
        value = getAttributeValue(node, attrs, "thumbHeight", 0, 255, false);
        thumbHeight = (value != -1) ? value : thumbHeight;
    }
    if (node.hasChildNodes()) {
        NodeList children = node.getChildNodes();
        int count = children.getLength();
        if (count > 2) {
            throw new IIOInvalidTreeException
                ("app0JFIF node cannot have > 2 children", node);
        }
        for (int i = 0; i < count; i++) {
            Node child = children.item(i);
            String name = child.getNodeName();
            if (name.equals("JFXX")) {
                if ((!extSegments.isEmpty()) && fromScratch) {
                    throw new IIOInvalidTreeException
                        ("app0JFIF node cannot have > 1 JFXX node", node);
                }
                NodeList exts = child.getChildNodes();
                int extCount = exts.getLength();
                for (int j = 0; j < extCount; j++) {
                    Node ext = exts.item(j);
                    extSegments.add(new JFIFExtensionMarkerSegment(ext));
                }
            }
            if (name.equals("app2ICC")) {
                if ((iccSegment != null) && fromScratch) {
                    throw new IIOInvalidTreeException
                        ("> 1 ICC APP2 Marker Segment not supported", node);
                }
                iccSegment = new ICCMarkerSegment(child);
            }
        }
    }
}
 
Example 20
Project: OpenJSharp   File: XMLCipher.java   View Source Code Vote up 4 votes
/**
 * Removes the contents of a <code>Node</code>.
 *
 * @param node the <code>Node</code> to clear.
 */
private static void removeContent(Node node) {
    while (node.hasChildNodes()) {
        node.removeChild(node.getFirstChild());
    }
}