List of usage examples for org.w3c.dom NamedNodeMap item
public Node item(int index);
index
th item in the map. From source file:com.vinexs.tool.XML.java
private static Object getChildJSONObject(org.w3c.dom.Element tag) { int i, k;/*from w ww . j ava 2s . com*/ //get attributes && child nodes NamedNodeMap attributes = tag.getAttributes(); NodeList childNodes = tag.getChildNodes(); int numAttr = attributes.getLength(); int numChild = childNodes.getLength(); //get element nodes Boolean hasTagChild = false; Map<String, ArrayList<Object>> childMap = new HashMap<>(); for (i = 0; i < numChild; i++) { Node node = childNodes.item(i); //not process non-element node if (node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) { continue; } hasTagChild = true; org.w3c.dom.Element childTag = (org.w3c.dom.Element) node; String tagName = childTag.getTagName(); if (!childMap.containsKey(tagName)) { childMap.put(tagName, new ArrayList<>()); } childMap.get(tagName).add(getChildJSONObject(childTag)); } if (numAttr == 0 && !hasTagChild) { // Return String return stringToValue(tag.getTextContent()); } else { // Return JSONObject JSONObject data = new JSONObject(); if (numAttr > 0) { for (i = 0; i < numAttr; i++) { Node attr = attributes.item(i); try { data.put(attr.getNodeName(), stringToValue(attr.getNodeValue())); } catch (JSONException e) { e.printStackTrace(); } } } if (hasTagChild) { for (Map.Entry<String, ArrayList<Object>> tagMap : childMap.entrySet()) { ArrayList<Object> tagList = tagMap.getValue(); if (tagList.size() == 1) { try { data.put(tagMap.getKey(), tagList.get(0)); } catch (JSONException e) { e.printStackTrace(); } } else { JSONArray array = new JSONArray(); for (k = 0; k < tagList.size(); k++) { array.put(tagList.get(k)); } try { data.put(tagMap.getKey(), array); } catch (JSONException e) { e.printStackTrace(); } } } } else { try { data.put("content", stringToValue(tag.getTextContent())); } catch (JSONException e) { e.printStackTrace(); } } return data; } }
From source file:DOMProcessor.java
/** Renames the given element with the given new name. * @param existingElement Element to rename. * @param newName New name to give element. *//*from ww w . j a v a 2 s . c o m*/ public void renameElement(Node existingElement, String newName) { // Create an element with the new name Node newElement = dom.createElement(newName); // Copy the attributes to the new element NamedNodeMap attrs = existingElement.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr2 = (Attr) dom.importNode(attrs.item(i), true); newElement.getAttributes().setNamedItem(attr2); } // Move all the children while (existingElement.hasChildNodes()) { newElement.appendChild(existingElement.getFirstChild()); } // Replace the old node with the new node existingElement.getParentNode().replaceChild(newElement, existingElement); }
From source file:de.betterform.xml.xforms.model.Instance.java
private void listModelItems(List list, Node node, boolean deep) { ModelItem modelItem = (ModelItem) node.getUserData(""); if (modelItem == null) { modelItem = createModelItem(node); }//from w w w. j a va 2 s.com list.add(modelItem); if (deep) { NamedNodeMap attributes = node.getAttributes(); for (int index = 0; attributes != null && index < attributes.getLength(); index++) { listModelItems(list, attributes.item(index), deep); } if (node.getNodeType() != Node.ATTRIBUTE_NODE) { NodeList children = node.getChildNodes(); for (int index = 0; index < children.getLength(); index++) { listModelItems(list, children.item(index), deep); } } } }
From source file:DomPrintUtil.java
/** * Returns XML text converted from the target DOM * /*from w w w . jav a 2s . c o m*/ * @return String format XML converted from the target DOM */ public String toXMLString() { StringBuffer tmpSB = new StringBuffer(8192); TreeWalkerImpl treeWalker = new TreeWalkerImpl(document, whatToShow, nodeFilter, entityReferenceExpansion); String lt = escapeTagBracket ? ESC_LT : LT; String gt = escapeTagBracket ? ESC_GT : GT; String line_sep = indent ? LINE_SEP : EMPTY_STR; Node tmpN = treeWalker.nextNode(); boolean prevIsText = false; String indentS = EMPTY_STR; while (tmpN != null) { short type = tmpN.getNodeType(); switch (type) { case Node.ELEMENT_NODE: if (prevIsText) { tmpSB.append(line_sep); } tmpSB.append(indentS + lt + tmpN.getNodeName()); NamedNodeMap attrs = tmpN.getAttributes(); int len = attrs.getLength(); for (int i = 0; i < len; i++) { Node attr = attrs.item(i); String value = attr.getNodeValue(); if (null != value) { tmpSB.append(getAttributeString((Element) tmpN, attr)); } } if (tmpN instanceof HTMLTitleElement && !tmpN.hasChildNodes()) { tmpSB.append(gt + ((HTMLTitleElement) tmpN).getText()); prevIsText = true; } else if (checkNewLine(tmpN)) { tmpSB.append(gt + line_sep); prevIsText = false; } else { tmpSB.append(gt); prevIsText = true; } break; case Node.TEXT_NODE: if (!prevIsText) { tmpSB.append(indentS); } tmpSB.append(getXMLString(tmpN.getNodeValue())); prevIsText = true; break; case Node.COMMENT_NODE: String comment; if (escapeTagBracket) { comment = getXMLString(tmpN.getNodeValue()); } else { comment = tmpN.getNodeValue(); } tmpSB.append(line_sep + indentS + lt + "!--" + comment + "--" + gt + line_sep); prevIsText = false; break; case Node.CDATA_SECTION_NODE: tmpSB.append(line_sep + indentS + lt + "!CDATA[" + tmpN.getNodeValue() + "]]" + line_sep); break; case Node.DOCUMENT_TYPE_NODE: if (tmpN instanceof DocumentType) { DocumentType docType = (DocumentType) tmpN; String pubId = docType.getPublicId(); String sysId = docType.getSystemId(); if (null != pubId && pubId.length() > 0) { if (null != sysId && sysId.length() > 0) { tmpSB.append(lt + "!DOCTYPE " + docType.getName() + " PUBLIC \"" + pubId + " \"" + sysId + "\">" + line_sep); } else { tmpSB.append( lt + "!DOCTYPE " + docType.getName() + " PUBLIC \"" + pubId + "\">" + line_sep); } } else { tmpSB.append(lt + "!DOCTYPE " + docType.getName() + " SYSTEM \"" + docType.getSystemId() + "\">" + line_sep); } } else { System.out.println("Document Type node does not implement DocumentType: " + tmpN); } break; default: System.out.println(tmpN.getNodeType() + " : " + tmpN.getNodeName()); } Node next = treeWalker.firstChild(); if (null != next) { if (indent && type == Node.ELEMENT_NODE) { indentS = indentS + " "; } tmpN = next; continue; } if (tmpN.getNodeType() == Node.ELEMENT_NODE) { tmpSB.append(lt + "/" + tmpN.getNodeName() + gt + line_sep); prevIsText = false; } next = treeWalker.nextSibling(); if (null != next) { tmpN = next; continue; } tmpN = null; next = treeWalker.parentNode(); while (null != next) { if (next.getNodeType() == Node.ELEMENT_NODE) { if (indent) { if (indentS.length() > 0) { indentS = indentS.substring(1); } else { System.err.println("indent: " + next.getNodeName() + " " + next); } } tmpSB.append(line_sep + indentS + lt + "/" + next.getNodeName() + gt + line_sep); prevIsText = false; } next = treeWalker.nextSibling(); if (null != next) { tmpN = next; break; } next = treeWalker.parentNode(); } } return tmpSB.toString(); }
From source file:com.inbravo.scribe.rest.service.crm.ms.MSCRMMessageFormatUtils.java
/** * //from w w w .ja v a 2s .c om * @param account * @param accountId * @param cADbject * @return * @throws Exception */ public static final Task createCRMObjectTypeTask(final ScribeObject cADbject, final String crmFieldIntraSeparator) throws Exception { /* Create xml beans object */ final Task task = Task.Factory.newInstance(); /* Create new cursor */ XmlCursor cursor = null; try { cursor = task.newCursor(); cursor.toFirstContentToken(); final Node node = cursor.getDomNode(); /* Get main namespace URl */ if (node != null) { final String nsURL = node.getNamespaceURI(); /* Iterate on the Element list and create SOAP object */ for (final Element element : cADbject.getXmlContent()) { if (logger.isDebugEnabled()) { logger.debug("----Inside createCRMObject_Task: adding node : " + element.getNodeName() + " with value : " + element.getTextContent()); } /* Get crm field */ final String crmField = element.getNodeName(); String crmFieldName = null; /* If field contains the type information */ if (crmField != null && crmField.contains(crmFieldIntraSeparator)) { /* Split the field and get name */ crmFieldName = crmField.split(crmFieldIntraSeparator)[0]; } else { crmFieldName = crmField; } /* Add all new nodes */ cursor.beginElement(new QName(nsURL, crmFieldName)); /* Set node attributes */ if (element.getAttributes() != null) { /* Get attribute map */ final NamedNodeMap namedNodeMap = element.getAttributes(); /* Interate over map */ for (int i = 0; i < namedNodeMap.getLength(); i++) { /* Get respective item */ final Node namedNode = namedNodeMap.item(i); if (logger.isDebugEnabled()) { logger.debug("----Inside createCRMObject_Task: adding attribute name : " + namedNode.getNodeName() + " with value : " + namedNode.getNodeValue()); } /* Set node attribute */ cursor.insertAttributeWithValue(namedNode.getNodeName(), namedNode.getNodeValue()); } } /* Set node value */ cursor.insertChars(element.getTextContent()); /* Jump to next token */ cursor.toNextToken(); } } } finally { if (cursor != null) { cursor.dispose(); } } return task; }
From source file:com.inbravo.scribe.rest.service.crm.ms.MSCRMMessageFormatUtils.java
/** * /*from ww w . ja v a 2 s .c o m*/ * @param account * @param accountId * @param cADbject * @return * @throws Exception */ public static final Contact createCRMObjectTypeContact(final ScribeObject cADbject, final String crmFieldIntraSeparator) throws Exception { /* Create xml beans object */ final Contact contact = Contact.Factory.newInstance(); /* Create new cursor */ XmlCursor cursor = null; try { cursor = contact.newCursor(); cursor.toFirstContentToken(); final Node node = cursor.getDomNode(); /* Get main namespace URl */ if (node != null) { final String nsURL = node.getNamespaceURI(); /* Iterate on the Element list and create SOAP object */ for (final Element element : cADbject.getXmlContent()) { if (logger.isDebugEnabled()) { logger.debug("----Inside createCRMObject_Contact: adding node : " + element.getNodeName() + " with value : " + element.getTextContent()); } /* Get crm field */ final String crmField = element.getNodeName(); String crmFieldName = null; /* If field contains the type information */ if (crmField != null && crmField.contains(crmFieldIntraSeparator)) { /* Split the field and get name */ crmFieldName = crmField.split(crmFieldIntraSeparator)[0]; } else { crmFieldName = crmField; } /* Add all new nodes */ cursor.beginElement(new QName(nsURL, crmFieldName)); /* Set node attributes */ if (element.getAttributes() != null) { /* Get attribute map */ final NamedNodeMap namedNodeMap = element.getAttributes(); /* Interate over map */ for (int i = 0; i < namedNodeMap.getLength(); i++) { /* Get respective item */ final Node namedNode = namedNodeMap.item(i); if (logger.isDebugEnabled()) { logger.debug("----Inside createCRMObject_Contact: adding attribute name : " + namedNode.getNodeName() + " with value: " + namedNode.getNodeValue()); } /* Set node attribute */ cursor.insertAttributeWithValue(namedNode.getNodeName(), namedNode.getNodeValue()); } } /* Set node value */ cursor.insertChars(element.getTextContent()); /* Jump to next token */ cursor.toNextToken(); } } } finally { if (cursor != null) { cursor.dispose(); } } return contact; }
From source file:com.inbravo.scribe.rest.service.crm.ms.MSCRMMessageFormatUtils.java
/** * /*from www.j a v a 2 s.c om*/ * @param account * @param accountId * @param cADbject * @return * @throws Exception */ public static final Lead createCRMObjectTypeLead(final ScribeObject cADbject, final String crmFieldIntraSeparator) throws Exception { /* Create xml beans object */ final Lead lead = Lead.Factory.newInstance(); /* Create new cursor */ XmlCursor cursor = null; try { cursor = lead.newCursor(); cursor.toFirstContentToken(); final Node node = cursor.getDomNode(); /* Get main namespace URl */ if (node != null) { final String nsURL = node.getNamespaceURI(); /* Iterate on the Element list and create SOAP object */ for (final Element element : cADbject.getXmlContent()) { if (logger.isDebugEnabled()) { logger.debug("----Inside createCRMObject_Lead: adding node : " + element.getNodeName() + " with value : " + element.getTextContent()); } /* Get crm field */ final String crmField = element.getNodeName(); String crmFieldName = null; /* If field contains the type information */ if (crmField != null && crmField.contains(crmFieldIntraSeparator)) { /* Split the field and get name */ crmFieldName = crmField.split(crmFieldIntraSeparator)[0]; } else { crmFieldName = crmField; } /* Add all new nodes */ cursor.beginElement(new QName(nsURL, crmFieldName)); /* Set node attributes */ if (element.getAttributes() != null) { /* Get attribute map */ final NamedNodeMap namedNodeMap = element.getAttributes(); /* Interate over map */ for (int i = 0; i < namedNodeMap.getLength(); i++) { /* Get respective item */ final Node namedNode = namedNodeMap.item(i); if (logger.isDebugEnabled()) { logger.debug("----Inside createCRMObject_Lead: adding attribute name : " + namedNode.getNodeName() + " with value: " + namedNode.getNodeValue()); } /* Set node attribute */ cursor.insertAttributeWithValue(namedNode.getNodeName(), namedNode.getNodeValue()); } } /* Set node value */ cursor.insertChars(element.getTextContent()); /* Jump to next token */ cursor.toNextToken(); } } } finally { if (cursor != null) { cursor.dispose(); } } return lead; }
From source file:com.inbravo.scribe.rest.service.crm.ms.MSCRMMessageFormatUtils.java
/** * /*from www. j a v a 2 s . com*/ * @param account * @param accountId * @param cADbject * @return * @throws Exception */ public static final Account createCRMObjectTypeAccount(final ScribeObject cADbject, final String crmFieldIntraSeparator) throws Exception { /* Create xml beans object */ final Account account = Account.Factory.newInstance(); /* Create new cursor */ XmlCursor cursor = null; try { cursor = account.newCursor(); cursor.toFirstContentToken(); final Node node = cursor.getDomNode(); /* Get main namespace URl */ if (node != null) { final String nsURL = node.getNamespaceURI(); /* Iterate on the Element list and create SOAP object */ for (final Element element : cADbject.getXmlContent()) { if (logger.isDebugEnabled()) { logger.debug("----Inside createCRMObject_Account: adding node: " + element.getNodeName() + " with value : " + element.getTextContent()); } /* Get crm field */ final String crmField = element.getNodeName(); String crmFieldName = null; /* If field contains the type information */ if (crmField != null && crmField.contains(crmFieldIntraSeparator)) { /* Split the field and get name */ crmFieldName = crmField.split(crmFieldIntraSeparator)[0]; } else { crmFieldName = crmField; } /* Add all new nodes */ cursor.beginElement(new QName(nsURL, crmFieldName)); /* Set node attributes */ if (element.getAttributes() != null) { /* Get attribute map */ final NamedNodeMap namedNodeMap = element.getAttributes(); /* Interate over map */ for (int i = 0; i < namedNodeMap.getLength(); i++) { /* Get respective item */ final Node namedNode = namedNodeMap.item(i); if (logger.isDebugEnabled()) { logger.debug("----Inside createCRMObject_Account: adding attribute name : " + namedNode.getNodeName() + " with value : " + namedNode.getNodeValue()); } /* Set node attribute */ cursor.insertAttributeWithValue(namedNode.getNodeName(), namedNode.getNodeValue()); } } /* Set node value */ cursor.insertChars(element.getTextContent()); /* Jump to next token */ cursor.toNextToken(); } } } finally { if (cursor != null) { cursor.dispose(); } } return account; }
From source file:com.inbravo.scribe.rest.service.crm.ms.MSCRMMessageFormatUtils.java
/** * /*w w w. jav a 2s . c om*/ * @param account * @param accountId * @param cADbject * @return * @throws Exception */ public static final Opportunity createCRMObjectTypeOpportunity(final ScribeObject cADbject, final String crmFieldIntraSeparator) throws Exception { /* Create xml beans object */ final Opportunity opportunity = Opportunity.Factory.newInstance(); /* Create new cursor */ XmlCursor cursor = null; try { cursor = opportunity.newCursor(); cursor.toFirstContentToken(); final Node node = cursor.getDomNode(); /* Get main namespace URl */ if (node != null) { final String nsURL = node.getNamespaceURI(); /* Iterate on the Element list and create SOAP object */ for (final Element element : cADbject.getXmlContent()) { if (logger.isDebugEnabled()) { logger.debug("----Inside createCRMObject_Opportunity: adding node : " + element.getNodeName() + " with value : " + element.getTextContent()); } /* Get crm field */ final String crmField = element.getNodeName(); String crmFieldName = null; /* If field contains the type information */ if (crmField != null && crmField.contains(crmFieldIntraSeparator)) { /* Split the field and get name */ crmFieldName = crmField.split(crmFieldIntraSeparator)[0]; } else { crmFieldName = crmField; } /* Add all new nodes */ cursor.beginElement(new QName(nsURL, crmFieldName)); /* Set node attributes */ if (element.getAttributes() != null) { /* Get attribute map */ final NamedNodeMap namedNodeMap = element.getAttributes(); /* Interate over map */ for (int i = 0; i < namedNodeMap.getLength(); i++) { /* Get respective item */ final Node namedNode = namedNodeMap.item(i); if (logger.isDebugEnabled()) { logger.debug("----Inside createCRMObject_Opportunity: adding attribute name : " + namedNode.getNodeName() + " with value: " + namedNode.getNodeValue()); } /* Set node attribute */ cursor.insertAttributeWithValue(namedNode.getNodeName(), namedNode.getNodeValue()); } } /* Set node value */ cursor.insertChars(element.getTextContent()); /* Jump to next token */ cursor.toNextToken(); } } } finally { if (cursor != null) { cursor.dispose(); } } return opportunity; }
From source file:com.inbravo.scribe.rest.service.crm.ms.MSCRMMessageFormatUtils.java
/** * /*from w ww. j av a 2 s. c om*/ * @param account * @param accountId * @param cADbject * @return * @throws Exception */ public static final Incident createCRMObjectTypeIncident(final ScribeObject cADbject, final String crmFieldIntraSeparator) throws Exception { /* Create xml beans object */ /* In MS CRM 'Incident' is actually a 'Case' */ final Incident caseObject = Incident.Factory.newInstance(); /* Create new cursor */ XmlCursor cursor = null; try { cursor = caseObject.newCursor(); cursor.toFirstContentToken(); final Node node = cursor.getDomNode(); /* Get main namespace URl */ if (node != null) { final String nsURL = node.getNamespaceURI(); /* Iterate on the Element list and create SOAP object */ for (final Element element : cADbject.getXmlContent()) { if (logger.isDebugEnabled()) { logger.debug("----Inside createCRMObject_Incident: adding node : " + element.getNodeName() + " with value : " + element.getTextContent()); } /* Get crm field */ final String crmField = element.getNodeName(); String crmFieldName = null; /* If field contains the type information */ if (crmField != null && crmField.contains(crmFieldIntraSeparator)) { /* Split the field and get name */ crmFieldName = crmField.split(crmFieldIntraSeparator)[0]; } else { crmFieldName = crmField; } /* Add all new nodes */ cursor.beginElement(new QName(nsURL, crmFieldName)); /* Set node attributes */ if (element.getAttributes() != null) { /* Get attribute map */ final NamedNodeMap namedNodeMap = element.getAttributes(); /* Interate over map */ for (int i = 0; i < namedNodeMap.getLength(); i++) { /* Get respective item */ final Node namedNode = namedNodeMap.item(i); if (logger.isDebugEnabled()) { logger.debug("----Inside createCRMObject_Incident: adding attribute name : " + namedNode.getNodeName() + " with value: " + namedNode.getNodeValue()); } /* Set node attribute */ cursor.insertAttributeWithValue(namedNode.getNodeName(), namedNode.getNodeValue()); } } /* Set node value */ cursor.insertChars(element.getTextContent()); /* Jump to next token */ cursor.toNextToken(); } } } finally { if (cursor != null) { cursor.dispose(); } } return caseObject; }