public void outputPretty(PrintWriter out, Annotation annotation) throws ServletException { try { Document input = XMLOutputter.annotationToDoc(annotation, pipeline); Nodes output = corenlpTransformer.transform(input); for (int i = 0; i < output.size(); i++) { out.print(output.get(i).toXML()); } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new ServletException(e); } }
public static Nodes extractNodesByXpath(final String xpath, final Node node, final int expectedNumberOfNodes, final boolean throwException) throws XMLParsingException { Nodes nodes = node.query(xpath); if (nodes.size() == expectedNumberOfNodes) { return nodes; } if (throwException) { throw new XMLParsingException("Expected " + expectedNumberOfNodes + " to be found with xPath " + xpath + ", found " + nodes.size()); } return nodes; }
/** * @return list of peak or peakGroup children (not descendants) */ public List<PeakOrGroup> getPeakOrGroupChildren() { Nodes nodes = this.query("./cml:peak | ./cml:peakGroup", CMLConstants.CML_XPATH); List<PeakOrGroup> peaks = new ArrayList<PeakOrGroup>(); for (int i = 0; i < nodes.size(); i++) { peaks.add((PeakOrGroup) nodes.get(i)); } return peaks; }
/** * @return list of peak children (not peakGroups or descendants) */ public List<CMLPeak> getPeakChildren() { Nodes nodes = this.query("./cml:peak", CMLConstants.CML_XPATH); List<CMLPeak> peaks = new ArrayList<CMLPeak>(); for (int i = 0; i < nodes.size(); i++) { peaks.add((CMLPeak) nodes.get(i)); } return peaks; }
Element rootElem = new Builder().build(xml).getRootElement(); xc = XPathContext.makeNamespaceContext(rootElem); xc.addNamespace("ex", "http://www.edankert.com/examples/"); Nodes matchedNodes = rootElem.query("ex:cd/ex:artist", xc); System.out.println(matchedNodes.size());
XMLReader tagsoup = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser"); Builder bob = new Builder(tagsoup); Document html = bob.build("http://www.yahoo.com"); Nodes images = html.query("//img"); for (int index = 0; index < images.size(); index++) { Element image = (Element) images.get(index); String src = image.getAttribute("src").getValue(); // do something with it... }
/** process all instances of arg in element. *TODO currently doesn't do anything * @param element */ public static void processArgs(CMLElement element) { Nodes parameterNameArgs = element.query(CMLArg.NS+"[@parameterName]", CMLConstants.CML_XPATH); for (int i = 0; i < parameterNameArgs.size(); i++) { @SuppressWarnings("unused") CMLArg arg = (CMLArg) parameterNameArgs.get(i); } }
public Element[] getDescendantElements(String name) { List<Element> descendants = new ArrayList<Element>(); Nodes nodes = xomElement.query(xpathForElementName(name), namespaceMappings); for (int i = 0; i < nodes.size(); i++) { descendants.add(new Element((nu.xom.Element)nodes.get(i))); } descendants.remove(this); return descendants.toArray(new Element[0]); }
public Element[] getDescendantElements(String name) { List<Element> descendants = new ArrayList<Element>(); Nodes nodes = xomElement.query(xpathForElementName(name), namespaceMappings); for (int i = 0; i < nodes.size(); i++) { descendants.add(new Element((nu.xom.Element)nodes.get(i))); } descendants.remove(this); return descendants.toArray(new Element[0]); }
/** * convenience method to get exactly one element. * uses element.query(xpath, xPathContext); * @param element * @param xpath * @param xPathContext defines prefix/namespace used in query * @return value if exactly 1 element (0 or many returns null) */ public static Element getSingleElement(Element element, String xpath, XPathContext xPathContext) { Nodes nodes = element.query(xpath, xPathContext); return (nodes.size() == 1) ? (Element) nodes.get(0) : null; }
protected static Iterator<Element> getElementsFromXPath(final String xPath, final Document document) { Nodes regNodes = document.query(xPath); List<Element> elements = new ArrayList<>(); for (int i = 0; i < regNodes.size(); i++) { try { elements.add((Element) regNodes.get(i)); } catch (ClassCastException ex) { ExceptionPrinter.printHistory(new CouldNotPerformException("Invalide data from XPath[" + xPath + "]!", ex), LOGGER); } } return elements.iterator(); }
public static Node getNode(Document doc, String entityType, String entityName) { String query = createQueryStringByName(entityType, entityName); Nodes nodes = doc.query(query); if (nodes != null && nodes.size() > 0) { return nodes.get(0); } return null; }
/** Wraps each item in the result sequence into a decorated element wrapper. */ private static Document wrapSequence(Nodes nodes) { // make a copy of the template for sequences: Element items = (Element) TEMPLATES.get(Nodes.class.getName()); items = new Element(items); int size = nodes.size(); for (int i=0; i < size; i++) { items.appendChild(wrap(nodes.get(i))); } return new Document(items); }
protected Element getElementFromXPath(final String xPath, final Document document) throws VerificationFailedException { Nodes regNodes = document.query(xPath); if (regNodes.size() != 1) { throw new VerificationFailedException("Invalide data from XPath[" + xPath + "]!"); } return (Element) regNodes.get(0); }
Nodes childNodes = parent.query("child"); for (int i = 0; i < childNodes.size(); i++) { Element currentChild = (Element) childNodes.get(i); if (someCondition) { ParentNode parent = currentChild.getParent(); int currentIndex = parent.indexOf(currentChild); currentChild.detach(); Element extraParent = new Element("extraParent"); extraParent.appendChild(currentChild); parent.insertChild(extraParent,currentIndex); } }
@Override protected void appendChildrenToNewParent(Element oldParent, Element newParent) throws SAXException { try { Nodes children = oldParent.removeChildren(); for (int i = 0; i < children.size(); i++) { newParent.appendChild(children.get(i)); } } catch (XMLException e) { fatal(e); } }
@Override protected void appendChildrenToNewParent(Element oldParent, Element newParent) throws SAXException { try { Nodes children = oldParent.removeChildren(); for (int i = 0; i < children.size(); i++) { newParent.appendChild(children.get(i)); } } catch (XMLException e) { fatal(e); } }
public static Nodes normalizeFormatting(final Nodes docs) { Nodes n = new Nodes(); for (int i = 0; i < docs.size(); i++) { n.append(normalizeFormatting(docs.get(i))); } return n; }
public static Element normalizeWhitespaceInTextNodes(Element element) { Nodes texts = element.query(".//text()"); for (int i = 0; i < texts.size(); i++) { Text text = (Text) texts.get(i); text.setValue(normalizeSpace(text.getValue())); } return element; }
@Override public ISource transform(ISource source) throws SourceException { Document d = source.getDocument(); Nodes tables = d.query("//table"); for (int i = 0; i < tables.size(); i++) { Element table = (Element) tables.get(i); table.addAttribute(new Attribute("class", getValue())); } return source; }