Example usage for org.w3c.dom NamedNodeMap getLength

List of usage examples for org.w3c.dom NamedNodeMap getLength

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap getLength.

Prototype

public int getLength();

Source Link

Document

The number of nodes in this map.

Usage

From source file:com.microsoft.exchange.impl.ExchangeResponseUtilsImpl.java

private String parseInnerResponse(ResponseMessageType responseMessage) {
    StringBuilder responseBuilder = new StringBuilder("Response[");

    ResponseCodeType responseCode = responseMessage.getResponseCode();
    if (null != responseCode) {
        responseBuilder.append("code=" + responseCode + ", ");
    }//from   www . ja  va  2  s. c o m
    ResponseClassType responseClass = responseMessage.getResponseClass();
    if (null != responseClass) {
        responseBuilder.append("class=" + responseClass + ", ");
    }
    String messageText = responseMessage.getMessageText();
    if (StringUtils.isNotBlank(messageText)) {
        responseBuilder.append("txt=" + messageText + ", ");
    }
    MessageXml messageXml = responseMessage.getMessageXml();
    if (null != messageXml) {
        StringBuilder xmlStringBuilder = new StringBuilder("messageXml=");
        List<Element> anies = messageXml.getAnies();
        if (!CollectionUtils.isEmpty(anies)) {
            for (Element element : anies) {
                String elementNameString = element.getNodeName();
                String elementValueString = element.getNodeValue();
                xmlStringBuilder.append(elementNameString + "=" + elementValueString + ";");

                if (null != element.getAttributes()) {
                    NamedNodeMap attributes = element.getAttributes();
                    for (int i = 0; i < attributes.getLength(); i++) {
                        Node item = attributes.item(i);
                        String nodeName = item.getNodeName();
                        String nodeValue = item.getNodeValue();
                        xmlStringBuilder.append(nodeName + "=" + nodeValue + ",");
                    }
                }
            }
        }
        responseBuilder.append("xml=" + xmlStringBuilder.toString() + ", ");
    }
    Integer descriptiveLinkKey = responseMessage.getDescriptiveLinkKey();
    if (null != descriptiveLinkKey) {
        responseBuilder.append("link=" + descriptiveLinkKey);
    }

    responseBuilder.append("]");
    return responseBuilder.toString();
}

From source file:fr.gouv.finances.dgfip.xemelios.utils.TextWriter.java

private void element(Writer writer, Element elem) throws IOException {
    if (prettyPrint) {
        checkTextBuffer(writer);//from  w  w  w .  j av a 2  s .com
        indent(writer);
    }

    String n = elem.getTagName();
    writer.write('<');
    writeText(writer, n);

    NamedNodeMap a = elem.getAttributes();
    int size = a.getLength();
    for (int i = 0; i < size; i++) {
        Attr att = (Attr) a.item(i);
        writer.write(' ');
        writeNode(writer, att);
    }

    if (elem.hasChildNodes()) {
        writer.write('>');

        if (prettyPrint) {
            String text = getChildText(elem);
            if (text != null)
                writeEscapedText(writer, normalizeString(text), false);
            else {
                writer.write('\n');
                indentLevel++;
                writeChildren(writer, elem);
                checkTextBuffer(writer);
                indentLevel--;
                indent(writer);
            }
        } else
            writeChildren(writer, elem);

        writer.write("</");
        writeText(writer, n);
        writer.write('>');
    } else {
        if (produceHTML)
            writer.write(">");
        else
            writer.write("/>");
    }

    if (prettyPrint)
        writer.write('\n');
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test/* ww  w  .  j  av a  2s.  c o  m*/
public void testRemoveAttributeByQNameNonExisting() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    element.setAttributeNS("urn:ns1", "ns1:attr", "value");
    assertFalse(element.removeAttribute(new QName("urn:ns2", "test")));
    NamedNodeMap attributes = element.getAttributes();
    assertEquals(1, attributes.getLength());
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test//from   ww w . j  a  va 2  s  .  c  o  m
public void testRemoveAttributeByQNameWithNamespace() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    element.setAttributeNS("urn:ns1", "ns1:attr", "value");
    element.setAttributeNS("urn:ns2", "ns2:attr", "value");
    assertTrue(element.removeAttribute(new QName("urn:ns1", "attr")));
    NamedNodeMap attributes = element.getAttributes();
    assertEquals(1, attributes.getLength());
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

/**
 * Tests the behavior of {@link SOAPElement#removeAttribute(QName)} for an attribute created
 * with {@link Element#setAttributeNS(String, String, String)}, but without namespace.
 * //from  w w w  .  j  a  v  a  2  s . c  o  m
 * @throws Exception
 */
@Validated
@Test
public void testRemoveAttributeByQNameWithoutNamespace1() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    element.setAttributeNS("urn:ns1", "ns1:attr", "value");
    element.setAttributeNS(null, "attr", "value");
    assertTrue(element.removeAttribute(new QName("attr")));
    NamedNodeMap attributes = element.getAttributes();
    assertEquals(1, attributes.getLength());
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

/**
 * Tests the behavior of {@link SOAPElement#removeAttribute(QName)} for an attribute created
 * with {@link Element#setAttribute(String, String)}.
 * //ww  w  .  j  a va  2 s. c  o m
 * @throws Exception
 */
@Validated
@Test
public void testRemoveAttributeByQNameWithoutNamespace2() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    element.setAttributeNS("urn:ns1", "ns1:attr", "value");
    element.setAttribute("attr", "value");
    assertTrue(element.removeAttribute(new QName("attr")));
    NamedNodeMap attributes = element.getAttributes();
    assertEquals(1, attributes.getLength());
}

From source file:betullam.xmlmodifier.XMLmodifier.java

/**
 * @param args// ww  w  . j  a v a  2 s.  co m
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 * @throws XPathExpressionException 
 * @throws TransformerException 
 */
public void modify(String mdFolder, String fileName, String condStructureType, String condMdNameFile,
        String condMdValueFile, String condMdNameElement, String condMdValueElement, String newMdName,
        String newMdValue) {

    // Get the files that should be modified:
    Set<File> filesToModify = getFilesToModify(mdFolder, fileName, condStructureType, condMdNameFile,
            condMdValueFile);

    // Backup XML-files before modifing them:
    makeBackupFiles(filesToModify);

    // Iterate over all files in given directory and it's subdirectories. Works with Apache commons-io library (FileUtils).
    for (File xmlFile : filesToModify) {

        // DOM Parser:
        String filePath = xmlFile.getAbsolutePath();
        DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
        Document xmlDoc = null;
        try {
            DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
            xmlDoc = documentBuilder.parse(filePath);

            // Get the elements to change. The name-attribute of the <goobi:metadata ...>-tag is important.
            // E. g. to change metadata "AccessLicense", the value of "mdName" must be the same as the name-attribute: <goobi:metadata name="AccessLicense">
            List<Node> nodeArrayList = getElementsToModify(condMdNameElement, condMdValueElement, xmlDoc);

            // Set the metadata (modMdName) to the given value (modMdValue):
            //for (int i = 0; i < node.getLength(); i++) {
            for (Node node : nodeArrayList) {
                Element xmlElement = (Element) node;

                // Change element text content:
                if (!newMdValue.equals("null")) {
                    xmlElement.setTextContent(newMdValue);
                }

                // Change element content
                if (!newMdName.equals("null")) {
                    NamedNodeMap attributes = xmlElement.getAttributes();
                    for (int j = 0; j < attributes.getLength(); j++) {
                        //Element attributeElement = (Attribute)attributes.item(j);
                        //xmlElement.setAttribute(attributes.item(j).getNodeName(), newMdName);
                        xmlElement.setAttribute("name", newMdName);
                    }
                }
            }

            // Save modifications to XML file:
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource domSource = new DOMSource(xmlDoc);
            StreamResult streamResult = new StreamResult(new File(filePath));
            transformer.transform(domSource, streamResult);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }

        System.out.println("File modified: " + xmlFile.getAbsoluteFile());
    }
}

From source file:com.esri.geoevent.solutions.adapter.cot.CoTAdapter.java

private String getAttribute(Node node, String attributeName) {
    NamedNodeMap attributes = node.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node attributeNode = attributes.item(i);
        if (attributeNode.getNodeName().equals(attributeName)) {
            return attributeNode.getNodeValue();
        }/*ww w .j  a v  a2s  .  c om*/
    }
    return null;
}

From source file:uk.co.markfrimston.tasktree.TaskTree.java

protected boolean nodesEqual(Node a, Node b) {
    if ((a == null) != (b == null)) {
        return false;
    }/*www.ja  v a2s .c o m*/
    if (a != null) {
        if (a.getNodeType() != b.getNodeType()) {
            return false;
        }
        if ((a.getNodeName() == null) != (b.getNodeName() == null)) {
            return false;
        }
        if (a.getNodeName() != null && !a.getNodeName().equals(b.getNodeName())) {
            return false;
        }
        if ((a.getNodeValue() == null) != (b.getNodeValue() == null)) {
            return false;
        }
        if (a.getNodeValue() != null && !a.getNodeValue().equals(b.getNodeValue())) {
            return false;
        }
        NamedNodeMap attrsA = a.getAttributes();
        Map<String, String> attrMapA = new HashMap<String, String>();
        if (attrsA != null) {
            for (int i = 0; i < attrsA.getLength(); i++) {
                Attr att = (Attr) attrsA.item(i);
                attrMapA.put(att.getName(), att.getValue());
            }
        }
        NamedNodeMap attrsB = b.getAttributes();
        Map<String, String> attrMapB = new HashMap<String, String>();
        if (attrsB != null) {
            for (int i = 0; i < attrsB.getLength(); i++) {
                Attr att = (Attr) attrsB.item(i);
                attrMapB.put(att.getName(), att.getValue());
            }
        }
        if (!attrMapA.equals(attrMapB)) {
            return false;
        }

        Node childA = a.getFirstChild();
        Node childB = b.getFirstChild();
        while (childA != null) {
            if (!nodesEqual(childA, childB)) {
                return false;
            }
            childA = childA.getNextSibling();
            childB = childB.getNextSibling();
        }
        if (childB != null) {
            return false;
        }
    }
    return true;
}

From source file:org.pegadi.server.article.ArticleServerImpl.java

/**
 * Change the main tag name of an article. Used when the article type changes.
 *
 * @param art The article.//from  w w w .j  a  v  a2s.c  om
 * @param ID  The ID of the new article type.
 */
protected void changeArticleType(Article art, int ID) {
    Map<String, Object> map = template.queryForMap("SELECT tagname FROM ArticleType WHERE ID=:id",
            Collections.singletonMap("id", ID));
    String tag = (String) map.get("tagname");

    Document doc = art.getDocument();
    if (doc == null) {
        art.parseText();
        doc = art.getDocument();
    }

    if (doc == null) {
        log.error("changeArticleType: Can't get document for article, article is NOT changed.");
        return;
    }

    Element root = doc.getDocumentElement();
    log.info("Root before: " + doc.getDocumentElement().toString());

    Element replace = doc.createElement(tag);

    NamedNodeMap att = root.getAttributes();

    for (int i = 0; i < att.getLength(); i++) {
        Node n = att.item(i);
        if (n instanceof Attr) {
            replace.setAttribute(((Attr) n).getName(), ((Attr) n).getValue());
        }
    }

    NodeList nl = root.getChildNodes();
    log.info("changeArticleType: Root node has {} children.", nl.getLength());
    for (int i = 0; i < nl.getLength(); i++) {
        Node clone = nl.item(i).cloneNode(true);
        log.info("Adding node {} to replace", (i + 1));
        replace.appendChild(clone);
    }

    log.info("Replacement node: {}", replace.toString());

    doc.replaceChild(replace, root);

    log.info("Root after: {}", doc.getDocumentElement().toString());

    if (!art.serialize()) {
        log.error("changeArticleType: Can't serialize the changed XML.");
    }
}