Search in sources :

Example 86 with NodeList

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

the class Element method hasChild.

/**
     * Returns {@code true} if element has at least one child with given name,
     * otherwise returns {@code false}.
     *
     * @param name
     *         child name to check
     * @return {@code true} if element has at least one child with given name, otherwise {@code false}
     * @throws XMLTreeException
     *         when this element has been removed from xml tree
     * @throws NullPointerException
     *         when name parameter is {@code null}
     */
public boolean hasChild(String name) {
    checkNotRemoved();
    requireNonNull(name, "Required not null child name");
    final NodeList nodes = delegate.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        if (name.equals(nodes.item(i).getNodeName())) {
            return true;
        }
    }
    return false;
}
Also used : NodeList(org.w3c.dom.NodeList)

Example 87 with NodeList

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

the class Element method createElement.

private Element createElement(Node node) {
    final Element element = new Element(xmlTree);
    element.delegate = (org.w3c.dom.Element) node;
    node.setUserData("element", element, null);
    if (node.hasChildNodes()) {
        final NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i).getNodeType() == ELEMENT_NODE) {
                createElement(children.item(i));
            }
        }
    }
    return element;
}
Also used : XMLTreeUtil.asElement(org.eclipse.che.commons.xml.XMLTreeUtil.asElement) NodeList(org.w3c.dom.NodeList)

Example 88 with NodeList

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

the class XMLTree method retrieveText.

/**
     * Evaluates xpath expression and maps result as list of strings
     * using {@link Node#getTextContent()} method
     */
private List<String> retrieveText(String expression) {
    final NodeList nodeList = (NodeList) evaluateXPath(expression, NODESET);
    final List<String> elementsText = new ArrayList<>(nodeList.getLength());
    for (int i = 0; i < nodeList.getLength(); i++) {
        elementsText.add(nodeList.item(i).getTextContent());
    }
    return elementsText;
}
Also used : NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList)

Example 89 with NodeList

use of org.w3c.dom.NodeList in project moco by dreamhead.

the class XPathRequestExtractor method doExtract.

@Override
protected Optional<String[]> doExtract(final HttpRequest request) {
    try {
        Optional<InputSource> source = helper.extractAsInputSource(request, extractor);
        if (!source.isPresent()) {
            return absent();
        }
        NodeList list = (NodeList) xPathExpression.evaluate(source.get(), XPathConstants.NODESET);
        if (list.getLength() == 0) {
            return absent();
        }
        return doExtract(list);
    } catch (XPathExpressionException e) {
        return absent();
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList)

Example 90 with NodeList

use of org.w3c.dom.NodeList in project lucida by claritylab.

the class TREC13To16Parser method loadTargets.

/**
	 * Loads the target objects from a file.
	 * 
	 * @param filename file that contains the targets
	 * @return targets or <code>null</code>, if the file could not be parsed
	 */
public static TRECTarget[] loadTargets(String filename) {
    try {
        // create factory object
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // create DOM parser
        DocumentBuilder parser = factory.newDocumentBuilder();
        // parse file and build tree
        Document trecD = parser.parse(new File(filename));
        NodeList targetL = trecD.getElementsByTagName("target");
        TRECTarget[] targets = new TRECTarget[targetL.getLength()];
        for (int i = 0; i < targets.length; i++) {
            Element targetE = (Element) targetL.item(i);
            String targetId = targetE.getAttribute("id").trim();
            String targetDesc = targetE.getAttribute("text").trim();
            NodeList questionL = targetE.getElementsByTagName("q");
            TRECQuestion[] questions = new TRECQuestion[questionL.getLength()];
            for (int j = 0; j < questions.length; j++) {
                Element questionE = (Element) questionL.item(j);
                String questionId = questionE.getAttribute("id").trim();
                String type = questionE.getAttribute("type").trim();
                String questionString = questionE.getFirstChild().getNodeValue().trim();
                questions[j] = new TRECQuestion(questionId, type, questionString);
            }
            targets[i] = new TRECTarget(targetId, targetDesc, questions);
        }
        return targets;
    } catch (Exception e) {
        MsgPrinter.printErrorMsg("Failed to load or parse question file:");
        MsgPrinter.printErrorMsg(e.toString());
        return null;
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) IOException(java.io.IOException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File)

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