public String GetChildPlainContent(Node myNode) { String parsedString = ""; try { if (myNode.hasChildNodes()) { NodeList subNodes = myNode.getChildNodes(); for (int c = 0; c < subNodes.getLength(); c++) { Node innerNode = subNodes.item(c); if (innerNode.getNodeName().equalsIgnoreCase("#text")) { parsedString += innerNode.getNodeValue().toString(); } if (innerNode.hasChildNodes()) { if (innerNode.getFirstChild().getNodeName().equalsIgnoreCase("#text")) { parsedString += innerNode.getFirstChild().getNodeValue().toString(); } } } } if (myNode.getNodeName().equalsIgnoreCase("#text")) { parsedString += myNode.getNodeValue().toString(); } // parsedString = c.replaceAll(parsedString, "\r", ""); // parsedString = c.replaceAll(parsedString, "\n", ""); return parsedString; } catch (Exception e) { return parsedString; } }
public OpenHABSitemap(Node startNode) { if (startNode.hasChildNodes()) { NodeList childNodes = startNode.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode.getNodeName().equals("name")) { this.setName(childNode.getTextContent()); } else if (childNode.getNodeName().equals("label")) { this.setLabel(childNode.getTextContent()); } else if (childNode.getNodeName().equals("link")) { this.setLink(childNode.getTextContent()); } else if (childNode.getNodeName().equals("icon")) { this.setIcon(childNode.getTextContent()); } else if (childNode.getNodeName().equals("homepage")) { if (childNode.hasChildNodes()) { NodeList homepageNodes = childNode.getChildNodes(); for (int j = 0; j < homepageNodes.getLength(); j++) { Node homepageChildNode = homepageNodes.item(j); if (homepageChildNode.getNodeName().equals("link")) { this.setHomepageLink(homepageChildNode.getTextContent()); } else if (homepageChildNode.getNodeName().equals("leaf")) { if (homepageChildNode.getTextContent().equals("true")) { setLeaf(true); } else { setLeaf(false); } } } } } } } }
private @Nullable ResourceStatus toGatewayStatus(@Nullable Node node) throws CloudException, InternalException { if (node == null) { return null; } NodeList attributes = node.getChildNodes(); VPNGatewayState state = VPNGatewayState.PENDING; String gatewayId = null; for (int i = 0; i < attributes.getLength(); i++) { Node attr = attributes.item(i); String nodeName = attr.getNodeName(); if (nodeName.equalsIgnoreCase("customerGatewayId") && attr.hasChildNodes()) { gatewayId = attr.getFirstChild().getNodeValue().trim(); } else if (nodeName.equalsIgnoreCase("state") && attr.hasChildNodes()) { state = toGatewayState(attr.getFirstChild().getNodeValue().trim()); } } if (gatewayId == null) { return null; } return new ResourceStatus(gatewayId, state); }
public static void traverseAMLforObjectNames( HashMap partialMap, Node currentNode, HashMap ObjDef_LinkId, HashMap ModelId_ModelType) { if (currentNode.hasChildNodes()) { for (int i = 0; i < currentNode.getChildNodes().getLength(); i++) { Node currentChild = currentNode.getChildNodes().item(i); if (currentChild.getNodeName().equals("Group")) { traverseAMLforObjectNames(partialMap, currentChild, ObjDef_LinkId, ModelId_ModelType); } if (currentChild.getNodeName().equals("Model")) { if (currentChild.hasAttributes()) { String mid = currentChild.getAttributes().getNamedItem("Model.ID").getNodeValue(); String type = currentChild.getAttributes().getNamedItem("Model.Type").getNodeValue(); ModelId_ModelType.put(mid, type); } // traverseAMLforObjectNames(partialMap, currentChild, // ObjDef_LinkId); } if (currentChild.getNodeName().equals("ObjDef")) { String id = currentChild.getAttributes().getNamedItem("ObjDef.ID").getNodeValue(); NodeList currentChildren = currentChild.getChildNodes(); String ObjName = ""; for (int k = 0; k < currentChildren.getLength(); k++) { Node Child = currentChildren.item(k); if (!Child.getNodeName().equals("AttrDef")) { continue; } else if (!Child.getAttributes() .getNamedItem("AttrDef.Type") .getNodeValue() .equals("AT_NAME")) { continue; } else if (Child.hasChildNodes()) { for (int l = 0; l < Child.getChildNodes().getLength(); l++) { if (!(Child.getChildNodes().item(l).getNodeName().equals("AttrValue"))) { continue; } else { ObjName = getTextContent(Child.getChildNodes().item(l)); ObjName = ObjName.replaceAll("\n", "\\\\n"); break; } } } } partialMap.put(id, ObjName); for (int j = 0; j < currentChild.getAttributes().getLength(); j++) { if (currentChild.getAttributes().item(j).getNodeName().equals("LinkedModels.IdRefs")) { String links = currentChild.getAttributes().getNamedItem("LinkedModels.IdRefs").getNodeValue(); /* * if (links.indexOf(" ") > -1) { * Message.add("yes, yes, yes"); links = * links.substring(0, links.indexOf(" ")); } */ ObjDef_LinkId.put(id, links); } } } } } }
/** * This method traverses <i>office:settings</i> {@code Element}. * * @param node <i>office:settings</i> {@code Node}. * @throws IOException If any I/O error occurs. */ public void traverseSettings(Node node) throws IOException { if (node.hasChildNodes()) { NodeList nodeList = node.getChildNodes(); int len = nodeList.getLength(); for (int i = 0; i < len; i++) { Node child = nodeList.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { String nodeName = child.getNodeName(); if (nodeName.equals(TAG_CONFIG_ITEM_SET)) { traverseSettings(child); } else if (nodeName.equals(TAG_CONFIG_ITEM_MAP_INDEXED)) { traverseSettings(child); } else if (nodeName.equals(TAG_CONFIG_ITEM_MAP_ENTRY)) { BookSettings bs = new BookSettings(child); encoder.addSettings(bs); } else { Debug.log(Debug.TRACE, "<OTHERS " + XmlUtil.getNodeInfo(child) + " />"); } } } } }
/* */ Node getLastChild(Node node) /* */ { /* 447 */ if (node == null) return null; /* */ /* 449 */ if ((!this.fEntityReferenceExpansion) && (node.getNodeType() == 5)) /* */ { /* 451 */ return null; /* */ } /* 453 */ Node newNode = node.getLastChild(); /* 454 */ if (newNode == null) return null; /* */ /* 456 */ int accept = acceptNode(newNode); /* */ /* 458 */ if (accept == 1) { /* 459 */ return newNode; /* */ } /* 461 */ if ((accept == 3) && (newNode.hasChildNodes())) /* */ { /* 464 */ Node lChild = getLastChild(newNode); /* 465 */ if (lChild == null) { /* 466 */ return getPreviousSibling(newNode, node); /* */ } /* 468 */ return lChild; /* */ } /* */ /* 473 */ return getPreviousSibling(newNode, node); /* */ }
/* */ Node getFirstChild(Node node) /* */ { /* 409 */ if (node == null) return null; /* */ /* 411 */ if ((!this.fEntityReferenceExpansion) && (node.getNodeType() == 5)) /* */ { /* 413 */ return null; /* 414 */ } Node newNode = node.getFirstChild(); /* 415 */ if (newNode == null) return null; /* 416 */ int accept = acceptNode(newNode); /* */ /* 418 */ if (accept == 1) { /* 419 */ return newNode; /* */ } /* 421 */ if ((accept == 3) && (newNode.hasChildNodes())) /* */ { /* 424 */ Node fChild = getFirstChild(newNode); /* */ /* 426 */ if (fChild == null) { /* 427 */ return getNextSibling(newNode, node); /* */ } /* 429 */ return fChild; /* */ } /* */ /* 434 */ return getNextSibling(newNode, node); /* */ }
private void readNodes(NodeList list, String path) { path = path.trim(); for (int i = 0; i < list.getLength(); i++) { Node node = (Node) list.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && node.hasChildNodes()) { Element elem = (Element) node; String nextPath = new String(); if (path.length() > 0) { nextPath = path + "."; } nextPath = nextPath + elem.getTagName(); readNodes(elem.getChildNodes(), nextPath); } else { String val = node.getNodeValue().trim(); if (!val.isEmpty()) { config.put(path, val); } } } }
private boolean listItemContainsHeadings(Node node) { if (node.hasChildNodes()) { NodeList nList = node.getChildNodes(); int len = nList.getLength(); for (int i = 0; i < len; i++) { Node child = nList.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { String nodeName = child.getNodeName(); if (nodeName.equals(XMLString.TEXT_H)) { return true; } if (nodeName.equals(XMLString.TEXT_LIST)) { if (listContainsHeadings(child)) return true; } if (nodeName.equals(XMLString.TEXT_ORDERED_LIST)) { if (listContainsHeadings(child)) return true; } if (nodeName.equals(XMLString.TEXT_UNORDERED_LIST)) { if (listContainsHeadings(child)) return true; } } } } return false; }
// This method recursively traverses the XML Tree (starting from currElement) public void traverseXML(Node currNode) { // If it's an Element, spit out the Name if (currNode.getNodeType() == Node.ELEMENT_NODE) { System.out.print(currNode.getNodeName() + ": "); // If it's a "Text" node, take the value // These will come one after another and therefore appear in the right order } else if (currNode.getNodeType() == Node.TEXT_NODE) { System.out.println(currNode.getNodeValue().trim()); } // Display any attributes if (currNode.hasAttributes()) { NamedNodeMap attributes = currNode.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node attr = attributes.item(i); System.out.println(" " + attr.getNodeName() + ": " + attr.getNodeValue()); } } // Check any children if (currNode.hasChildNodes()) { // Get the list of children NodeList children = currNode.getChildNodes(); // Go through all the chilrden for (int i = 0; i < children.getLength(); i++) { // Search each Node Node n = children.item(i); traverseXML(n); } } }
/** * Parses a string containing XML and returns a DocumentFragment containing the nodes of the * parsed XML. */ public static void loadFragment(Element el, String fragment) { // Wrap the fragment in an arbitrary element fragment = "<fragment>" + fragment + "</fragment>"; try { // Create a DOM builder and parse the fragment DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document d = factory.newDocumentBuilder().parse(new InputSource(new StringReader(fragment))); Document doc = el.getOwnerDocument(); // Import the nodes of the new document into doc so that they // will be compatible with doc Node node = doc.importNode(d.getDocumentElement(), true); // Create the document fragment node to hold the new nodes DocumentFragment docfrag = doc.createDocumentFragment(); // Move the nodes into the fragment while (node.hasChildNodes()) { el.appendChild(node.removeChild(node.getFirstChild())); } } catch (Exception e) { log.error(e, e); } }
private void collectIds(String fileName, Node node) { // Kør rekursivt if (stop) { return; } // Check on noden har et id if (node.getNodeType() == Node.ELEMENT_NODE) { String id = getAttributeFromNode(node, "id"); if (id != "") { // Læg i Map if (addId2Map(id, fileName) == false) { stop = true; return; } } } if (node.hasChildNodes()) { NodeList lst = node.getChildNodes(); for (int i = 0; i < lst.getLength(); i++) { Node childNode = lst.item(i); collectIds(fileName, childNode); } } }
private String getContent(Node node) { if (node.hasChildNodes()) { return getContent(node.getChildNodes()); } return node.getNodeValue(); }
/** * 字符串格式参数转换成HashMap * * @param str String 字符串参数 * @return Map */ public static Map splitParam(String str) { Map result = new HashMap(); Document doc = XMLTools.stringToDocument(str); NodeList elements = doc.getElementsByTagName("param"); for (int i = 0, j = elements.getLength(); i < j; i++) { Node param = elements.item(i); NodeList childs = param.getChildNodes(); String paramName = null; String paramValue = null; for (int m = 0, n = childs.getLength(); m < n; m++) { Node child = childs.item(m); if (child.getNodeName().equalsIgnoreCase("name")) { paramName = child.getFirstChild().getNodeValue(); } else { if (child.hasChildNodes()) { paramValue = child.getFirstChild().getNodeValue(); } else { paramValue = ""; } } } result.put(paramName, paramValue); } return result; }
public static void removeChildren(Node node) { if (node != null) { while (node.hasChildNodes()) { node.removeChild(node.getFirstChild()); } } }
/** * Removes style attributes from this node and it's children recursively. * * @param node node being filtered. * @param attributeMappings attribute map to be used for filtering. */ private void filter(Node node, Map<String, String> attributeMappings) { if (node instanceof Element) { Element element = (Element) node; String allowedAttributes = attributeMappings.get(element.getNodeName().toLowerCase()); NamedNodeMap currentAttributes = element.getAttributes(); if (null == allowedAttributes) { // Strip off all attributes. while (currentAttributes.getLength() > 0) { currentAttributes.removeNamedItem(currentAttributes.item(0).getNodeName()); } } else { // Collect those attributes that need to be removed. List<String> attributesToBeRemoved = new ArrayList<String>(); for (int i = 0; i < currentAttributes.getLength(); i++) { String attributeName = currentAttributes.item(i).getNodeName(); String pattern = ATTRIBUTE_SEPARATOR + attributeName.toLowerCase() + ATTRIBUTE_SEPARATOR; if (allowedAttributes.indexOf(pattern) == -1) { attributesToBeRemoved.add(attributeName); } } // Remove those attributes collected above. for (String attribute : attributesToBeRemoved) { currentAttributes.removeNamedItem(attribute); } } if (node.hasChildNodes()) { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { filter(children.item(i), attributeMappings); } } } }
/** * Get the node absolute start location in its residing IStructuredModel. * * @param p * @return the location */ public static int getIndexedRegionLocation(IDOMPosition p) { if (!EditValidateUtil.validPosition(p)) { return -1; } Node parent = p.getContainerNode(); if (p.isText()) { return ((IndexedRegion) parent).getStartOffset() + p.getOffset(); } int index = p.getOffset(); if (!parent.hasChildNodes()) { // Element: if (!isDocument(parent)) { IStructuredDocumentRegion region = ((IDOMNode) parent).getStartStructuredDocumentRegion(); return region.getEnd(); } // Document node: int offset = ((IndexedRegion) parent).getStartOffset(); return offset; } NodeList children = parent.getChildNodes(); // After rightmost child if (children.getLength() == index) { if (!isDocument(parent)) { int pos = getNodeEndNameStartIndex(parent); return pos; } int offset = ((IndexedRegion) parent).getEndOffset(); return offset; } Node node = children.item(index); return ((IndexedRegion) node).getStartOffset(); }
public Node getFirstRealChild(Node node) { if (node.hasChildNodes()) { NodeList childs = node.getChildNodes(); Node child = getFirstRealNode(childs); return child; } return null; }
public static String text(Node node) { if (node.getNodeType() == Node.TEXT_NODE) { return node.getNodeValue(); } if (node.hasChildNodes()) { return text(node.getChildNodes()); } return ""; }
public static Node getChild(Node node, String name) { if (!node.hasChildNodes()) return null; NodeList lst = node.getChildNodes(); if (lst == null) return null; for (int i = 0; i < lst.getLength(); i++) { Node child = lst.item(i); if (name.equals(child.getNodeName())) return child; } return null; }
/** * Parse multi-level object data model hierarchy definition node. * * @param node */ public void parse(Node node) { if (node.hasChildNodes() && tagHierarchy.equals(node.getNodeName())) { if (node.getFirstChild().getNodeType() == Node.ELEMENT_NODE) { if (HierarchyNodeDef.tagNode.equals(node.getFirstChild().getNodeName())) { this.node = new HierarchyNodeDef(); this.node.parse(node.getFirstChild()); } } } }
/** * This method traverses the <i>table:table</i> element {@code Node}. * * @param node A <i>table:table</i> {@code Node}. * @throws IOException If any I/O error occurs. */ protected void traverseTable(Node node) throws IOException { Debug.log(Debug.TRACE, "<TABLE>"); ColumnRowList = new ArrayList<ColumnRowInfo>(); // Get table attributes // ToDo - extract style from attribute NamedNodeMap att = node.getAttributes(); String tableName = att.getNamedItem(ATTRIBUTE_TABLE_NAME).getNodeValue(); rowID = 1; encoder.createWorksheet(tableName); if (node.hasChildNodes()) { NodeList nodeList = node.getChildNodes(); int len = nodeList.getLength(); for (int i = 0; i < len; i++) { Node child = nodeList.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { String nodeName = child.getNodeName(); if (nodeName.equals(TAG_TABLE_ROW)) { // TODO - handle all the possible rows // spelled out in the entities traverseTableRow(child); } else if (nodeName.equals(TAG_TABLE_COLUMN)) { traverseTableColumn(child); } else if (nodeName.equals(TAG_TABLE_SCENARIO)) { // TODO } else { Debug.log(Debug.TRACE, "<OTHERS " + XmlUtil.getNodeInfo(child) + " />"); } } } } // Add column width info to the current sheet encoder.setColumnRows(ColumnRowList); Debug.log(Debug.TRACE, "</TABLE>"); }
public @Nonnull int getOperationStatus(String requestID) throws CloudException, InternalException { ProviderContext ctx = provider.getContext(); Document doc = getAsXML(ctx.getAccountNumber(), "/operations/" + requestID); if (doc == null) { return -2; } NodeList entries = doc.getElementsByTagName("Operation"); Node entry = entries.item(0); NodeList s = entry.getChildNodes(); String status = ""; String httpCode = ""; for (int i = 0; i < s.getLength(); i++) { Node attribute = s.item(i); if (attribute.getNodeType() == Node.TEXT_NODE) { continue; } if (attribute.getNodeName().equalsIgnoreCase("status") && attribute.hasChildNodes()) { status = attribute.getFirstChild().getNodeValue().trim(); continue; } if (status.length() > 0 && !status.equalsIgnoreCase("inProgress")) { if (attribute.getNodeName().equalsIgnoreCase("httpstatuscode") && attribute.hasChildNodes()) { httpCode = attribute.getFirstChild().getNodeValue().trim(); } } } if (status.equalsIgnoreCase("succeeded")) { return HttpServletResponse.SC_OK; } else if (status.equalsIgnoreCase("failed")) { String errMsg = checkError(s, httpCode); throw new CloudException(errMsg); } return -1; }
/** * generateChild method * * @return java.lang.String * @param org.w3c.dom.Node */ public String generateChild(Node parentNode) { if (parentNode == null) return null; if (!parentNode.hasChildNodes()) return null; StringBuffer buffer = new StringBuffer(); for (Node child = parentNode.getFirstChild(); child != null; child = child.getNextSibling()) { String childSource = generateSource(child); if (childSource != null) buffer.append(childSource); } return buffer.toString(); }
private void parse(Document doc) { NodeList nodeList = doc.getChildNodes(); for (int count = 0; count < nodeList.getLength(); count++) { Node node = nodeList.item(count); if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.hasChildNodes()) { parseAttrList(node.getChildNodes()); } } } }
private void readPositionalNode(Node node, PositionalRecordDescriptor recordDescriptorIn) throws MetadataReaderException { if (node.hasChildNodes()) { NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode.getNodeType() == Node.ELEMENT_NODE) { readPositionalFieldNode(childNode, recordDescriptorIn); } } } }
private static void trimWhitespaceTextNodes(final org.w3c.dom.Node node) { if (node != null && node.hasChildNodes()) { for (int i = 0; i < node.getChildNodes().getLength(); i++) { final org.w3c.dom.Node child = node.getChildNodes().item(i); if (child.getNodeType() == org.w3c.dom.Node.TEXT_NODE && child.getNodeValue().trim().length() == 0) { node.removeChild(child); } trimWhitespaceTextNodes(node.getChildNodes().item(i)); } } }
// ��ȡnode�ӽ����tag��ǩ�������б� public List<String> getChildTags(Node node, String tag) { List<String> list = new LinkedList<String>(); if (node.hasChildNodes()) { NodeList childs = node.getChildNodes(); Node child = getFirstTagNode(childs, tag); while (child != null) { list.add(child.getTextContent()); child = getNextTagNode(child); } } return list; }
/** * @author Michael Y W Yeung [18 July 13] * @description EULA enhancement Parse the <version></version> item for EULA */ protected Version getVersion(final Node eulaItemChild) throws EntityListParserException { Version version = new Version(); String versionCode = eulaItemChild.getAttributes().getNamedItem(EULA_VER_CODE).getNodeValue(); version.setVersionCode(versionCode); // === parse version segment ===// NodeList versionNodeList = eulaItemChild.getChildNodes(); for (int l = 0; l < versionNodeList.getLength(); l++) { Node versionItemChildNode = versionNodeList.item(l); if (versionItemChildNode.getNodeType() == Node.ELEMENT_NODE) { if (versionItemChildNode.getNodeName().equalsIgnoreCase(EULA_NOTICEPERIOD)) { String noticeperiod = versionItemChildNode.getTextContent(); version.setNoticeperiod(noticeperiod); } if (versionItemChildNode.getNodeName().equalsIgnoreCase(EULA_DESC)) { String desc = versionItemChildNode.getTextContent(); version.setDesc(desc); } if (versionItemChildNode.getNodeName().equalsIgnoreCase(EULA_PHONE)) { if (versionItemChildNode.hasChildNodes()) { Phone phone = getPhone(versionItemChildNode); version.setPhone(phone); } } if (versionItemChildNode.getNodeName().equalsIgnoreCase(EULA_TABLET)) { if (versionItemChildNode.hasChildNodes()) { Tablet tablet = getTablet(versionItemChildNode); version.setTablet(tablet); } } } } // === parse version segment ===// return version; }
/** * Getting node value * * @param elem element */ public final String getElementValue(Node elem) { Node child; if (elem != null) { if (elem.hasChildNodes()) { for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.TEXT_NODE) { return child.getNodeValue(); } } } } return ""; }