Search in sources :

Example 81 with NodeList

use of org.w3c.dom.NodeList in project cw-android by commonsguy.

the class WeatherDemo method buildForecasts.

void buildForecasts(String raw) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(raw)));
    NodeList times = doc.getElementsByTagName("start-valid-time");
    for (int i = 0; i < times.getLength(); i++) {
        Element time = (Element) times.item(i);
        Forecast forecast = new Forecast();
        forecasts.add(forecast);
        forecast.setTime(time.getFirstChild().getNodeValue());
    }
    NodeList temps = doc.getElementsByTagName("value");
    for (int i = 0; i < temps.getLength(); i++) {
        Element temp = (Element) temps.item(i);
        Forecast forecast = forecasts.get(i);
        forecast.setTemp(new Integer(temp.getFirstChild().getNodeValue()));
    }
    NodeList icons = doc.getElementsByTagName("icon-link");
    for (int i = 0; i < icons.getLength(); i++) {
        Element icon = (Element) icons.item(i);
        Forecast forecast = forecasts.get(i);
        forecast.setIcon(icon.getFirstChild().getNodeValue());
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 82 with NodeList

use of org.w3c.dom.NodeList in project hackpad by dropbox.

the class XmlNode method getMatchingChildren.

XmlNode[] getMatchingChildren(Filter filter) {
    ArrayList<XmlNode> rv = new ArrayList<XmlNode>();
    NodeList nodes = this.dom.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (filter.accept(node)) {
            rv.add(createImpl(node));
        }
    }
    return rv.toArray(new XmlNode[rv.size()]);
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList)

Example 83 with NodeList

use of org.w3c.dom.NodeList in project che by eclipse.

the class Element method hasChildren.

/**
     * Returns {@code true} if element has at least one child or {@code false} if doesn't
     *
     * @return {@code true} if element has at least one child or {@code false} if doesn't
     * @throws XMLTreeException
     *         when this element has been removed from xml tree
     */
public boolean hasChildren() {
    checkNotRemoved();
    final NodeList childNodes = delegate.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i).getNodeType() == ELEMENT_NODE) {
            return true;
        }
    }
    return false;
}
Also used : NodeList(org.w3c.dom.NodeList)

Example 84 with NodeList

use of org.w3c.dom.NodeList in project che by eclipse.

the class Element method removeChildren.

/**
     * Removes children which names equal to given name
     *
     * @param name
     *         name to remove children
     * @return this element instance
     * @throws XMLTreeException
     *         when this element has been removed from xml tree
     */
public Element removeChildren(String name) {
    checkNotRemoved();
    final List<Node> matched = new LinkedList<>();
    final NodeList nodes = delegate.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        if (name.equals(nodes.item(i).getNodeName())) {
            matched.add(nodes.item(i));
        }
    }
    for (Node node : matched) {
        asElement(node).remove();
    }
    return this;
}
Also used : Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) LinkedList(java.util.LinkedList)

Example 85 with NodeList

use of org.w3c.dom.NodeList in project che by eclipse.

the class Element method fetchText.

private String fetchText() {
    final StringBuilder sb = new StringBuilder();
    final NodeList childNodes = delegate.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i).getNodeType() == TEXT_NODE) {
            sb.append(childNodes.item(i).getTextContent());
        }
    }
    return sb.toString();
}
Also used : NodeList(org.w3c.dom.NodeList)

Aggregations

NodeList (org.w3c.dom.NodeList)1806 Node (org.w3c.dom.Node)1059 Element (org.w3c.dom.Element)902 Document (org.w3c.dom.Document)636 ArrayList (java.util.ArrayList)314 DocumentBuilder (javax.xml.parsers.DocumentBuilder)268 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)208 IOException (java.io.IOException)183 NamedNodeMap (org.w3c.dom.NamedNodeMap)144 InputSource (org.xml.sax.InputSource)131 HashMap (java.util.HashMap)121 Test (org.junit.Test)117 SAXException (org.xml.sax.SAXException)117 StringReader (java.io.StringReader)106 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)100 XPath (javax.xml.xpath.XPath)99 Attr (org.w3c.dom.Attr)80 XPathExpressionException (javax.xml.xpath.XPathExpressionException)76 File (java.io.File)64 HashSet (java.util.HashSet)59