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;
}
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;
}
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;
}
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();
}
}
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;
}
}
Aggregations