Example usage for org.dom4j Element attributes

List of usage examples for org.dom4j Element attributes

Introduction

In this page you can find the example usage for org.dom4j Element attributes.

Prototype

List<Attribute> attributes();

Source Link

Document

Returns the Attribute instances this element contains as a backed List so that the attributes may be modified directly using the List interface.

Usage

From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java

License:Apache License

/**
 * ??//from  www.j av  a 2s.  c om
 *
 * @param element
 *            
 * @return 
 */
public List<Object> attributeList(Element element) {
    @SuppressWarnings("unchecked")
    List<Object> list = element.attributes();
    return list;
}

From source file:com.hwsoft.util.idcard.IDCardVerifyTools.java

/**
 * ?Nciicl?/*w w w .  j  a  v  a2  s .  c o  m*/
 *
 * @param backXMl?
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static IdCardInfo parseXml(String backXMl, String userName, String idCard) {
    Document doc = null;
    String result_gmsfhm = null;
    String result_xm = null;
    String gmsfhm = null;
    String xm = null;
    String xp = null;
    String errormesage = null;
    IdCardInfo IdCardInfo = null;
    System.out.println("idcard bind Nciic  backInfo:" + backXMl);

    try {
        //XML
        doc = DocumentHelper.parseText(backXMl);

        // ?
        Element rootElt = doc.getRootElement();

        if ("RESPONSE".equalsIgnoreCase(rootElt.getName())) {
            //???
            List<DefaultAttribute> list = rootElt.attributes();

            for (DefaultAttribute e : list) {
                if ("errorcode".equals(e.getName())) {
                    if ("-70".equalsIgnoreCase(e.getValue())) {
                        throw new RuntimeException("?", null);
                    } else if ("-71".equalsIgnoreCase(e.getValue())) {
                        throw new RuntimeException("?", null);
                    } else if ("-72".equalsIgnoreCase(e.getValue())) {
                        throw new RuntimeException("IP ???", null);
                    } else if ("-80".equalsIgnoreCase(e.getValue())) {
                        throw new RuntimeException("??", null);
                    }
                }
            }
        } else if ("ROWS".equalsIgnoreCase(rootElt.getName())) {
            Iterator iterForRow = rootElt.elementIterator("ROW");

            while (iterForRow.hasNext()) {
                Element recordEleForRow = (Element) iterForRow.next();

                //?????
                Iterator itersForInput = recordEleForRow.elementIterator("INPUT");

                while (itersForInput.hasNext()) {
                    Element recordEleForInput = (Element) itersForInput.next();
                    gmsfhm = recordEleForInput.elementTextTrim("gmsfhm");

                    if (!(idCard.equalsIgnoreCase(gmsfhm))) {
                        throw new RuntimeException("?????", null);
                    }

                    xm = recordEleForInput.elementTextTrim("xm");

                    if (!(userName.equalsIgnoreCase(xm))) {
                        throw new RuntimeException("?????", null);
                    }
                }

                //??????
                Iterator itersForOutput = recordEleForRow.elementIterator("OUTPUT");

                while (itersForOutput.hasNext()) {
                    Element recordEleForSubRow = (Element) itersForOutput.next();
                    Iterator itersForItem = recordEleForSubRow.elementIterator("ITEM");

                    while (itersForItem.hasNext()) {
                        Element recordEleForItem = (Element) itersForItem.next();
                        gmsfhm = recordEleForItem.elementTextTrim("gmsfhm");
                        xm = recordEleForItem.elementTextTrim("xm");
                        xp = recordEleForItem.elementTextTrim("xp");
                        errormesage = recordEleForItem.elementTextTrim("errormesage");

                        if (gmsfhm != null) {
                            result_gmsfhm = recordEleForItem.elementTextTrim("result_gmsfhm");
                        }

                        if (xm != null) {
                            result_xm = recordEleForItem.elementTextTrim("result_xm");
                        }

                        if (xp != null) {
                            xp = recordEleForItem.elementTextTrim("xp");
                        }

                        if (errormesage != null) {
                            errormesage = recordEleForItem.elementTextTrim("errormesage");
                            break;
                        }
                    }
                }

                if (null != errormesage) {
                    throw new RuntimeException("??!", null);
                } else {
                    if (!"".equalsIgnoreCase(result_xm)) {
                        throw new RuntimeException("?????", null);
                    }

                    if (!"".equalsIgnoreCase(result_gmsfhm)) {
                        throw new RuntimeException("?????", null);
                    }

                    IdCardInfo = new IdCardInfo();
                    IdCardInfo.setIdCardNumber(idCard);
                    IdCardInfo.setRealName(userName);
                    IdCardInfo.setAvatar(xp);
                }
            }
        }
    } catch (DocumentException e) {
        System.out.println("idcard bind erroinfo:XML??");
    } catch (Exception e) {
        System.out.println("idcard bind erroinfo:XML??");
    }
    return IdCardInfo;
}

From source file:com.itextpdf.rups.view.itext.treenodes.XdpTreeNode.java

License:Open Source License

/**
 * Constructs an XdpTreeNode//from w ww .  j  av a2s . c  o m
 * @param node   the XML node
 */
@SuppressWarnings("unchecked")
public XdpTreeNode(Node node) {
    super(null, node);
    if (node instanceof Element) {
        Element element = (Element) node;
        addChildNodes(element.attributes());
    }
    if (node instanceof Branch) {
        Branch branch = (Branch) node;
        addChildNodes(branch.content());
    }
    if (node instanceof Attribute) {
        icon = IconFetcher.getIcon("attribute.png");
        return;
    }
    if (node instanceof Text) {
        icon = IconFetcher.getIcon("text.png");
        return;
    }
    if (node instanceof ProcessingInstruction) {
        icon = IconFetcher.getIcon("pi.png");
        return;
    }
    if (node instanceof Document) {
        icon = IconFetcher.getIcon("xfa.png");
        return;
    }
    icon = IconFetcher.getIcon("tag.png");
}

From source file:com.liferay.alloy.tools.builder.base.BaseBuilder.java

License:Open Source License

protected List<Component> getAllComponents() throws Exception {
    DocumentFactory factory = SAXReaderUtil.getDocumentFactory();

    Document doc = factory.createDocument();

    String taglibsXML = "<components></components>";

    Document taglibsDoc = SAXReaderUtil
            .read(new InputSource(new ByteArrayInputStream(taglibsXML.getBytes("utf-8"))));

    Element root = taglibsDoc.getRootElement();

    for (Document currentDoc : getComponentDefinitionDocs()) {
        currentDoc = _getExtendedDocument(currentDoc);

        Element currentRoot = currentDoc.getRootElement();

        String defaultPackage = currentRoot.attributeValue("short-name");
        List<Element> extComponentNodes = currentRoot.elements("component");

        for (Element extComponent : extComponentNodes) {
            String extComponentPackage = Convert.toString(extComponent.attributeValue("package"),
                    defaultPackage);/* w w w .j  a v a2s .c  o m*/

            extComponent.addAttribute("package", extComponentPackage);
        }

        Element authors = currentRoot.element("author");

        List<Element> components = currentRoot.elements("component");

        for (Element component : components) {
            Element copy = component.createCopy();
            Element componentAuthors = copy.element("authors");

            if ((authors != null) && (componentAuthors == null)) {
                copy.add(authors.createCopy());
            }

            root.add(copy);
        }

        List<org.dom4j.Attribute> attributes = currentRoot.attributes();

        for (org.dom4j.Attribute attribute : attributes) {
            root.addAttribute(attribute.getName(), attribute.getValue());
        }
    }

    doc.add(root.createCopy());

    return getComponents(doc);
}

From source file:com.liferay.util.xml.descriptor.StrictXMLDescriptor.java

License:Open Source License

private int _compareAttributes(Element el1, Element el2) {
    List el1Attrs = el1.attributes();
    List el2Attrs = el2.attributes();

    if (el1Attrs.size() < el2Attrs.size()) {
        return -1;
    } else if (el1Attrs.size() > el2Attrs.size()) {
        return 1;
    }/*from   ww w. j  av a 2 s.c o  m*/

    for (int i = 0; i < el1Attrs.size(); i++) {
        Attribute attr = (Attribute) el1Attrs.get(i);

        int value = _contains(el2Attrs, attr, new AttributeComparator());
        if (value != 0) {
            return value;
        }
    }
    return -1;
}

From source file:com.liferay.util.xml.ElementComparator.java

License:Open Source License

public int compare(Object obj1, Object obj2) {
    Element el1 = (Element) obj1;
    Element el2 = (Element) obj2;

    String el1Name = el1.getName();
    String el2Name = el2.getName();

    if (!el1Name.equals(el2Name)) {
        return el1Name.compareTo(el2Name);
    }/*from ww  w.  j a  v a2s  . co m*/

    String el1Text = el1.getTextTrim();
    String el2Text = el2.getTextTrim();

    if (!el1Text.equals(el2Text)) {
        return el1Text.compareTo(el2Text);
    }

    List el1Attrs = el1.attributes();
    List el2Attrs = el2.attributes();

    if (el1Attrs.size() < el2Attrs.size()) {
        return -1;
    } else if (el1Attrs.size() > el2Attrs.size()) {
        return 1;
    }

    for (int i = 0; i < el1Attrs.size(); i++) {
        Attribute attr = (Attribute) el1Attrs.get(i);

        int value = _compare(el2Attrs, attr, new AttributeComparator());

        if (value != 0) {
            return value;
        }
    }

    List el1Elements = el1.elements();
    List el2Elements = el2.elements();

    if (el1Elements.size() < el2Elements.size()) {
        return -1;
    } else if (el1Elements.size() > el2Elements.size()) {
        return 1;
    }

    for (int i = 0; i < el1Elements.size(); i++) {
        Element el = (Element) el1Elements.get(i);

        int value = _compare(el2Elements, el, new ElementComparator());

        if (value != 0) {
            return value;
        }
    }

    return 0;
}

From source file:com.lowagie.rups.view.itext.treenodes.XdpTreeNode.java

License:Open Source License

/**
 * Constructs an XdpTreeNode/*from   w w  w. j  av a  2s.com*/
 * @param node   the XML node
 */
public XdpTreeNode(Node node) {
    super(null, node);
    if (node instanceof Element) {
        Element element = (Element) node;
        addChildNodes(element.attributes());
    }
    if (node instanceof Branch) {
        Branch branch = (Branch) node;
        addChildNodes(branch.content());
    }
    if (node instanceof Attribute) {
        icon = IconFetcher.getIcon("attribute.png");
        return;
    }
    if (node instanceof Text) {
        icon = IconFetcher.getIcon("text.png");
        return;
    }
    if (node instanceof ProcessingInstruction) {
        icon = IconFetcher.getIcon("pi.png");
        return;
    }
    if (node instanceof Document) {
        icon = IconFetcher.getIcon("xfa.png");
        return;
    }
    icon = IconFetcher.getIcon("tag.png");
}

From source file:com.mg.framework.support.ui.UIProducer.java

License:Open Source License

private static Element copyElement(Element element, RuntimeMacrosLoader runtimeMacrosLoader) {
    //if (INCLUDE_TAG_NAME.equals(element.getQualifiedName()))
    //return includeMacros();

    Element result = DocumentHelper.createElement(element.getQualifiedName());
    result.setAttributes(element.attributes());
    List<Element> childElements = MiscUtils.convertUncheckedList(Element.class, element.elements());
    for (Element childElement : childElements) {
        Element copy;/*from   w  w  w  .ja  v  a 2s.  com*/
        if (childElement.getQualifiedName().equals(INCLUDE_TAG_NAME)) {
            copy = includeMacros(result, childElement, runtimeMacrosLoader);
            /*Document macros = null;
            String runtimeMacros = childElement.attributeValue(RUNTIME_MACROS_NAME_ATTR);
            if (runtimeMacros == null)
            macros = loadMacros(childElement.attributeValue(MACROS_NAME_ATTR));
            else
            macros = loadRuntimeMacros(runtimeMacros, runtimeMacrosLoader);
            String macrosType = macros.getRootElement().getQualifiedName();
            if (macrosType.equals(EMPTY_MACROS))
            copy = null; //handle special case for empty macros
            else if (macrosType.equals(WRAP_MACROS)) {
            //copy macros contents exclude root element
            List<Element> macrosChildElements = MiscUtils.convertUncheckedList(Element.class, macros.getRootElement().elements());
            for (Element macrosChild : macrosChildElements) {
              Element macrosElement = copyElement(macrosChild, runtimeMacrosLoader);
              if (macrosElement != null)
             result.add(macrosElement);
            }
            copy = null;
            }
            else
            copy = copyElement(macros.getRootElement(), runtimeMacrosLoader); //copy root element
            */
        } else {
            copy = copyElement(childElement, runtimeMacrosLoader);
        }
        if (copy != null)
            result.add(copy);
    }
    return result;
}

From source file:com.nokia.helium.diamonds.XMLMerger.java

License:Open Source License

/**
 * Compare two elements name and attributes. Returns true if name and all
 * attributes are matching, false otherwise.
 * //from ww w  . j  a  va 2s  .c  o  m
 * @param a
 * @param b
 * @return boolean
 */
@SuppressWarnings("unchecked")
protected boolean areSame(Element a, Element b) {
    log.debug("areSame:" + a + " <=> " + b);
    if (!a.getName().equals(b.getName())) {
        return false;
    }
    log.debug("same attribute list size?");
    if (a.attributes().size() != b.attributes().size()) {
        return false;
    }
    log.debug("same attribute list?");
    for (Iterator<Attribute> at = a.attributes().iterator(); at.hasNext();) {
        Attribute attra = at.next();
        Attribute attrb = b.attribute(attra.getName());
        if (attrb == null || !attra.getValue().equals(attrb.getValue())) {
            return false;
        }
    }
    if (!a.getTextTrim().equals(b.getTextTrim())) {
        return false;
    }
    return true;
}

From source file:com.pureinfo.force.junit.AssertUtil.java

License:Open Source License

/**
 * ElementnameElement<br>//from www  .j  a  va 2  s .c o  m
 * Attribute
 * 
 * @param _expectedElement
 * @param _actualElement
 */
public static void assertXMLEquals(Element _expectedElement, Element _actualElement) {
    Assert.assertEquals("element name", _expectedElement.getName(), _actualElement.getName());
    List expectedAttributes = _expectedElement.attributes();
    List actualAttributes = _actualElement.attributes();
    Assert.assertEquals("attribute size", expectedAttributes.size(), actualAttributes.size());

    Map expectedNames = new HashMap();
    Map actualNames = new HashMap();
    for (Iterator iter = expectedAttributes.iterator(); iter.hasNext();) {
        Attribute attr = (Attribute) iter.next();
        expectedNames.put(attr.getName(), attr.getValue());
    }
    for (Iterator iter = actualAttributes.iterator(); iter.hasNext();) {
        Attribute attr = (Attribute) iter.next();
        actualNames.put(attr.getName(), attr.getValue());
    }
    Assert.assertEquals("attribute", expectedNames, actualNames);

    List expectedElements = _expectedElement.elements();
    List actualElements = _actualElement.elements();
    Assert.assertEquals("element size", expectedElements.size(), actualElements.size());

    for (Iterator iter1 = expectedElements.iterator(), iter2 = actualElements.iterator(); iter1.hasNext();) {
        Element expecteElement = (Element) iter1.next();
        Element actualElement = (Element) iter2.next();
        assertXMLEquals(expecteElement, actualElement);
    }
}