List of usage examples for org.w3c.dom Element getTagName
public String getTagName();
From source file:XMLUtils.java
/** * Get the first child element to match a given name. * Look for a child element in the same namespace as the parent. * /*from ww w . j a v a2s.c o m*/ * @param parent * @param name * @return */ public static Element getChild(Element parent, String name) { Element child = getFirstChild(parent); while (child != null) { String tagName = child.getTagName(); if (tagName != null && tagName.equals(name)) { return child; } if (child.getPrefix() != null && child.getPrefix().equals(parent.getPrefix()) && child.getLocalName() != null && child.getLocalName().equals(name)) { return child; } child = getNext(child); } return child; }
From source file:com.impetus.kundera.loader.PersistenceXMLLoader.java
/** * Parses the persistence unit./* www . j av a2s. c om*/ * * @param top * the top * @return the persistence metadata * @throws Exception * the exception */ private static PersistenceUnitMetadata parsePersistenceUnit(final URL url, final String[] persistenceUnits, Element top, final String versionName) { PersistenceUnitMetadata metadata = new PersistenceUnitMetadata(versionName, getPersistenceRootUrl(url), url); String puName = top.getAttribute("name"); if (!Arrays.asList(persistenceUnits).contains(puName)) { // Returning null because this persistence unit is not intended for // creating entity manager factory. return null; } if (!isEmpty(puName)) { log.trace("Persistent Unit name from persistence.xml: " + puName); metadata.setPersistenceUnitName(puName); String transactionType = top.getAttribute("transaction-type"); if (StringUtils.isEmpty(transactionType) || PersistenceUnitTransactionType.RESOURCE_LOCAL.name().equals(transactionType)) { metadata.setTransactionType(PersistenceUnitTransactionType.RESOURCE_LOCAL); } else if (PersistenceUnitTransactionType.JTA.name().equals(transactionType)) { metadata.setTransactionType(PersistenceUnitTransactionType.JTA); } } NodeList children = top.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { if (children.item(i).getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) children.item(i); String tag = element.getTagName(); if (tag.equals("provider")) { metadata.setProvider(getElementContent(element)); } else if (tag.equals("properties")) { NodeList props = element.getChildNodes(); for (int j = 0; j < props.getLength(); j++) { if (props.item(j).getNodeType() == Node.ELEMENT_NODE) { Element propElement = (Element) props.item(j); // if element is not "property" then skip if (!"property".equals(propElement.getTagName())) { continue; } String propName = propElement.getAttribute("name").trim(); String propValue = propElement.getAttribute("value").trim(); if (isEmpty(propValue)) { propValue = getElementContent(propElement, ""); } metadata.getProperties().put(propName, propValue); } } } else if (tag.equals("class")) { metadata.getClasses().add(getElementContent(element)); } else if (tag.equals("jar-file")) { metadata.addJarFile(getElementContent(element)); } else if (tag.equals("exclude-unlisted-classes")) { String excludeUnlisted = getElementContent(element); metadata.setExcludeUnlistedClasses(Boolean.parseBoolean(excludeUnlisted)); } } } PersistenceUnitTransactionType transactionType = getTransactionType(top.getAttribute("transaction-type")); if (transactionType != null) { metadata.setTransactionType(transactionType); } return metadata; }
From source file:XMLUtils.java
/** * Get the previous sibling element of a given element. * @param el/* w ww. ja v a2s .c o m*/ * @return */ public static Element getPreviousSibling(Element el) { Node n = el.getPreviousSibling(); while (n != null && (!(n instanceof Element) || !el.getTagName().equals(((Element) n).getTagName()))) { // get the next one n = n.getPreviousSibling(); } if (n instanceof Element) { return (Element) n; } else { // else, nothing to return return null; } }
From source file:com.vinexs.tool.XML.java
/** * Parse standard XML to JSONObject.//w ww.j a v a 2 s . c o m * * @param inputStream InputStream point to a XML document. * @return JSONObject */ public static JSONObject toJSONObject(InputStream inputStream) { JSONObject json = new JSONObject(); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(inputStream); org.w3c.dom.Element tag = document.getDocumentElement(); json.put(tag.getTagName(), getChildJSONObject(tag)); } catch (Exception e) { e.printStackTrace(); } return json; }
From source file:eu.esdihumboldt.hale.common.core.io.HaleIO.java
/** * Get the value of a complex property represented as a DOM element. * /*www .ja va 2 s . co m*/ * @param element the DOM element * @param context the context object, may be <code>null</code> * @return the complex value converted through the * {@link ComplexValueExtension}, or the original element */ public static Object getComplexValue(Element element, Object context) { QName name; if (element.getNamespaceURI() != null && !element.getNamespaceURI().isEmpty()) { name = new QName(element.getNamespaceURI(), element.getLocalName()); } else { name = new QName(element.getTagName()); // .getLocalName()); } ComplexValueDefinition cvt = ComplexValueExtension.getInstance().getDefinition(name); if (cvt != null) { // create and return the complex parameter value return cvt.fromDOM(element, context); } // the element itself is the complex value return element; }
From source file:com.vinexs.tool.XML.java
private static Object getChildJSONObject(org.w3c.dom.Element tag) { int i, k;// w ww . ja v a2 s.co m //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:mondrian.test.DiffRepository.java
private static void writeNode(Node node, XMLOutput out) { final NodeList childNodes; switch (node.getNodeType()) { case Node.DOCUMENT_NODE: out.print("<?xml version=\"1.0\" ?>" + Util.nl); childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); writeNode(child, out);//w ww. java 2 s. com } // writeNode(((Document) node).getDocumentElement(), out); break; case Node.ELEMENT_NODE: Element element = (Element) node; final String tagName = element.getTagName(); out.beginBeginTag(tagName); // Attributes. final NamedNodeMap attributeMap = element.getAttributes(); for (int i = 0; i < attributeMap.getLength(); i++) { final Node att = attributeMap.item(i); out.attribute(att.getNodeName(), att.getNodeValue()); } out.endBeginTag(tagName); // Write child nodes, ignoring attributes but including text. childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); if (child.getNodeType() == Node.ATTRIBUTE_NODE) { continue; } writeNode(child, out); } out.endTag(tagName); break; case Node.ATTRIBUTE_NODE: out.attribute(node.getNodeName(), node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: CDATASection cdata = (CDATASection) node; out.cdata(cdata.getNodeValue(), true); break; case Node.TEXT_NODE: Text text = (Text) node; final String wholeText = text.getNodeValue(); if (!isWhitespace(wholeText)) { out.cdata(wholeText, false); } break; case Node.COMMENT_NODE: Comment comment = (Comment) node; out.print("<!--" + comment.getNodeValue() + "-->" + Util.nl); break; default: throw new RuntimeException("unexpected node type: " + node.getNodeType() + " (" + node + ")"); } }
From source file:com.meltmedia.cadmium.core.util.WarUtils.java
/** * <p>Adds a child xml element to a parent element relative to other elements with a tagname.</p> * <p>If first is true then the child is added before any elements with a tagname, the opposite happens when first is false.</p> * @param parent The parent xml element. * @param child The child xml element to add. * @param tagname The tagname to be relative to. * @param first A flag that describes the relative relationship between the child element and its siblings named by tagname. *//*from www .j av a 2 s . co m*/ public static void addRelativeTo(Element parent, Element child, String tagname, boolean first) { NodeList nodes = parent.getChildNodes(); if (nodes.getLength() > 0) { Node relativeEl = null; boolean found = false; for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element el = (Element) node; if (el.getTagName().equals(tagname)) { if (relativeEl == null || !first) { relativeEl = el; } else if (first && !found) { relativeEl = el; } found = true; } } } if (relativeEl != null && !first) { relativeEl = relativeEl.getNextSibling(); } if (relativeEl == null && first) { relativeEl = nodes.item(0); } else { parent.appendChild(child); } if (relativeEl != null) { parent.insertBefore(child, relativeEl); } } else { //There are no elements in the parent node so lets just append child. parent.appendChild(child); } }
From source file:eu.esdihumboldt.hale.common.core.io.HaleIO.java
/** * Get the value of a complex property represented as a DOM element. * //from w w w . jav a2s. c o m * @param element the DOM element * @param expectedType the expected parameter type, this must be either * {@link String}, DOM {@link Element} or a complex value type * defined in the {@link ComplexValueExtension} * @param context the context object, may be <code>null</code> * @return the complex value or <code>null</code> if it could not be created * from the element */ @SuppressWarnings("unchecked") public static <T> T getComplexValue(Element element, Class<T> expectedType, Object context) { if (element == null) { return null; } QName name; if (element.getNamespaceURI() != null && !element.getNamespaceURI().isEmpty()) { name = new QName(element.getNamespaceURI(), element.getLocalName()); } else { String ln = element.getTagName(); // .getLocalName(); name = new QName(ln); } ComplexValueDefinition cvt = ComplexValueExtension.getInstance().getDefinition(name); Object value = null; if (cvt != null) { try { value = cvt.fromDOM(element, context); } catch (Exception e) { throw new IllegalStateException("Failed to load complex value from DOM", e); } } if (value != null && expectedType.isAssignableFrom(value.getClass())) { return (T) value; } // maybe the element itself is OK if (expectedType.isAssignableFrom(element.getClass())) { return (T) element; } if (expectedType.isAssignableFrom(String.class)) { // FIXME use legacy complex value if possible } return null; }
From source file:org.escidoc.browser.elabsmodul.service.ELabsService.java
private static InvestigationBean resolveInvestigation(final ContainerProxy containerProxy) { final String URI_DC = "http://purl.org/dc/elements/1.1/"; final String URI_EL = "http://escidoc.org/ontologies/bw-elabs/re#"; final String URI_RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; if (containerProxy == null) { throw new NullPointerException("Container Proxy is null."); }//from www .ja v a2s . c om final InvestigationBean investigationBean = new InvestigationBean(); final Element e = containerProxy.getMetadataRecords().get("escidoc").getContent(); investigationBean.setObjid(containerProxy.getId()); if (!(("Investigation".equals(e.getLocalName()) && URI_EL.equals(e.getNamespaceURI())) || "el:Investigation".equals(e.getTagName()))) { LOG.error("Container is not an eLabs Investigation"); return investigationBean; } final NodeList nodeList = e.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { final Node node = nodeList.item(i); final String nodeName = node.getLocalName(); final String nsUri = node.getNamespaceURI(); if (nodeName == null || nsUri == null) { continue; } if ("title".equals(nodeName) && URI_DC.equals(nsUri)) { investigationBean.setName(node.getTextContent()); } else if ("description".equals(nodeName) && URI_DC.equals(nsUri)) { investigationBean.setDescription(node.getTextContent()); } else if ("max-runtime".equals(nodeName) && URI_EL.equals(nsUri)) { investigationBean.setMaxRuntime("<<not used>>"); try { investigationBean.setMaxRuntimeInMin(Integer.valueOf(node.getTextContent())); } catch (final NumberFormatException nfe) { LOG.error(nfe.getMessage()); investigationBean.setMaxRuntimeInMin(0); } } else if ("deposit-endpoint".equals(nodeName) && URI_EL.equals(nsUri)) { investigationBean.setDepositEndpoint(node.getTextContent()); } else if ("investigator".equals(nodeName) && URI_EL.equals(nsUri)) { final String investigatorId = node.getAttributes().getNamedItemNS(URI_RDF, "resource") .getTextContent(); investigationBean.setInvestigator(investigatorId); } else if ("rig".equals(nodeName) && URI_EL.equals(nsUri)) { final String rigId = node.getAttributes().getNamedItemNS(URI_RDF, "resource").getTextContent(); if (StringUtils.notEmpty(rigId)) { final RigBean rigBean = new RigBean(); rigBean.setObjectId(rigId); investigationBean.setRigBean(rigBean); } } else if ("instrument".equals(nodeName) && URI_EL.equals(nsUri)) { final String instrument = node.getAttributes().getNamedItemNS(URI_RDF, "resource").getTextContent(); final String folder = node.getTextContent().trim(); investigationBean.getInstrumentFolder().put(instrument, folder); } } return investigationBean; }