List of usage examples for org.dom4j Element attributeCount
int attributeCount();
From source file:architecture.common.xml.XmlWriter.java
License:Apache License
/** * Writes the attributes of the given element * *///from ww w . j a v a2 s. co m protected void writeAttributes(Element element) throws IOException { // I do not yet handle the case where the same prefix maps to // two different URIs. For attributes on the same element // this is illegal; but as yet we don't throw an exception // if someone tries to do this for (int i = 0, size = element.attributeCount(); i < size; i++) { Attribute attribute = element.attribute(i); Namespace ns = attribute.getNamespace(); if (ns != null && ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) { String prefix = ns.getPrefix(); String uri = namespaceStack.getURI(prefix); if (!ns.getURI().equals(uri)) { // output a new namespace // declaration writeNamespace(ns); namespaceStack.push(ns); } } // If the attribute is a namespace declaration, check if we have // already // written that declaration elsewhere (if that's the case, it must // be // in the namespace stack String attName = attribute.getName(); if (attName.startsWith("xmlns:")) { String prefix = attName.substring(6); if (namespaceStack.getNamespaceForPrefix(prefix) == null) { String uri = attribute.getValue(); namespaceStack.push(prefix, uri); writeNamespace(prefix, uri); } } else if (attName.equals("xmlns")) { if (namespaceStack.getDefaultNamespace() == null) { String uri = attribute.getValue(); namespaceStack.push(null, uri); writeNamespace(null, uri); } } else { char quote = format.getAttributeQuoteCharacter(); writer.write(" "); writer.write(attribute.getQualifiedName()); writer.write("="); writer.write(quote); writeEscapeAttributeEntities(attribute.getValue()); writer.write(quote); } } }
From source file:architecture.ee.util.xml.XmlWriter.java
License:Apache License
/** Writes the attributes of the given element *//from ww w .ja va 2 s .c om */ protected void writeAttributes(Element element) throws IOException { // I do not yet handle the case where the same prefix maps to // two different URIs. For attributes on the same element // this is illegal; but as yet we don't throw an exception // if someone tries to do this for (int i = 0, size = element.attributeCount(); i < size; i++) { Attribute attribute = element.attribute(i); Namespace ns = attribute.getNamespace(); if (ns != null && ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) { String prefix = ns.getPrefix(); String uri = namespaceStack.getURI(prefix); if (!ns.getURI().equals(uri)) { // output a new namespace declaration writeNamespace(ns); namespaceStack.push(ns); } } // If the attribute is a namespace declaration, check if we have already // written that declaration elsewhere (if that's the case, it must be // in the namespace stack String attName = attribute.getName(); if (attName.startsWith("xmlns:")) { String prefix = attName.substring(6); if (namespaceStack.getNamespaceForPrefix(prefix) == null) { String uri = attribute.getValue(); namespaceStack.push(prefix, uri); writeNamespace(prefix, uri); } } else if (attName.equals("xmlns")) { if (namespaceStack.getDefaultNamespace() == null) { String uri = attribute.getValue(); namespaceStack.push(null, uri); writeNamespace(null, uri); } } else { char quote = format.getAttributeQuoteCharacter(); writer.write(" "); writer.write(attribute.getQualifiedName()); writer.write("="); writer.write(quote); writeEscapeAttributeEntities(attribute.getValue()); writer.write(quote); } } }
From source file:ch.javasoft.xml.config.ConstConfigParser.java
License:BSD License
private static <T> T parseConstant(Element constElement, Class<T> expectedType, boolean allowNull) throws XmlConfigException { final String sValue; if (constElement.attributeCount() == 1) { sValue = XmlUtil.getRequiredAttributeValue(constElement, new Attribute(constElement.attribute(0))); } else {/*from w ww . j ava2 s . c om*/ sValue = XmlUtil.getRequiredAttributeValue(constElement, XmlAttribute.value); } if (allowNull && sValue.equals("null")) { return null; } try { return expectedType.getConstructor(METHOD_SIGNATURE).newInstance(new Object[] { sValue }); } catch (Exception ex) { throw new XmlConfigException( "cannot create " + expectedType.getSimpleName() + " const value '" + sValue + "', e=" + ex, constElement, ex); } }
From source file:com.cladonia.xml.ExchangerXMLWriter.java
License:Open Source License
protected void writeAttributes(Element element) throws IOException { // I do not yet handle the case where the same prefix maps to // two different URIs. For attributes on the same element // this is illegal; but as yet we don't throw an exception // if someone tries to do this for (int i = 0, size = element.attributeCount(); i < size; i++) { Attribute attribute = element.attribute(i); Namespace ns = attribute.getNamespace(); if (ns != null && ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) { String prefix = ns.getPrefix(); String uri = namespaceStack.getURI(prefix); if (!ns.getURI().equals(uri)) { // output a new namespace declaration writeNamespace(ns);//from w ww .ja va 2 s . com namespaceStack.push(ns); } } writeAttribute(attribute); } }
From source file:com.github.megatronking.svg.applet.graphics.VdTree.java
License:Apache License
private void parseRootNode(Element rootNode) { if (rootNode.attributeCount() != 0) { parseSize(rootNode.attributes()); } }
From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java
License:Apache License
/** * ???//from ww w . j a v a 2 s . c o m * * @param element * @return */ public int getAttributeCount(Element element) { int attributeCount = element.attributeCount(); return attributeCount; }
From source file:Data.Storage.java
License:Apache License
/** * helper for adding xml/*from w ww. jav a 2 s .c om*/ * @param path where the xml will be added * @param xmlData the data as an xml Element */ private void addXmlWithPath(ArrayList<String> path, Element xmlData) { boolean isXml = xmlData.elements().size() > 0; //set the key String[] thePath = new String[path.size()]; path.toArray(thePath); //put either the value or an imbeded object if (isXml && !has(thePath)) { put(thePath, new Storage()); } else { put(thePath, xmlData.getStringValue()); } for (int i = 0; i < xmlData.attributeCount(); ++i) { //get attribute data Attribute attr = xmlData.attribute(i); //save attribute putAttribute(thePath, attr.getName(), attr.getValue()); } //if it is xml need to see all children if (isXml) { recurseOverElements(path, xmlData); } }
From source file:de.mwolff.command.chainbuilder.XMLChainBuilder.java
License:Open Source License
private static Map<String, String> getAttributeOfElement(final Element element) { final Map<String, String> attributeMap = new HashMap<>(); for (int i = 0; i < element.attributeCount(); i++) { attributeMap.put(element.attribute(i).getName(), element.attribute(i).getValue()); }//from w w w. j ava 2 s . c om return attributeMap; }
From source file:fr.gouv.culture.vitam.utils.XmlDom.java
License:Open Source License
public final static void removeEmptyElement(Element root) { // look first at attribute if (root.attributeCount() > 0) { @SuppressWarnings("unchecked") Iterator<Attribute> attribs = root.attributeIterator(); while (attribs.hasNext()) { Attribute attribute = (Attribute) attribs.next(); removeEmptyAttribute(attribute); }/*from ww w . j ava2 s . c o m*/ } @SuppressWarnings("unchecked") Iterator<Element> elements = root.elementIterator(); while (elements.hasNext()) { Element elt = (Element) elements.next(); // look at its descendant removeEmptyElement(elt); if (elt.attributeCount() > 0) { continue; } if (elt.hasContent()) { continue; } elt.detach(); } }
From source file:fr.gouv.vitam.xml.XmlDom4jTools.java
License:Open Source License
public final static void removeEmptyElement(Element root) { // look first at attribute if (root.attributeCount() > 0) { @SuppressWarnings("unchecked") Iterator<Attribute> attribs = root.attributeIterator(); List<Attribute> toremove = new ArrayList<>(); while (attribs.hasNext()) { Attribute attribute = (Attribute) attribs.next(); if (attribute.getValue().length() == 0) { toremove.add(attribute); }/* www .j a va 2s .c o m*/ //removeEmptyAttribute(attribute); } for (Attribute attribute : toremove) { root.remove(attribute); } toremove.clear(); } @SuppressWarnings("unchecked") Iterator<Element> elements = root.elementIterator(); List<Element> toremove = new ArrayList<>(); while (elements.hasNext()) { Element elt = (Element) elements.next(); // look at its descendant removeEmptyElement(elt); if (elt.attributeCount() > 0) { continue; } if (elt.hasContent()) { continue; } toremove.add(elt); //elt.detach(); } for (Element element : toremove) { root.remove(element); } toremove.clear(); }