/** ensures queries which may have cml namespace prefix have XPath context * @param query * @return nodes */ public Nodes cmlQuery(String query) { return super.query(query, CMLConstants.CML_XPATH); }
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());
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]); }
Document document = new Builder().build(responseString, "test.xml"); Element rootElem = document.getRootElement(); XPathContext xc = XPathContext.makeNamespaceContext(rootElem); xc.addNamespace("fev1", "http://ar.gov.afip.dif.FEV1/"); Nodes matchedNodes = rootElem.query("/soap:Envelope/soap:Body/fev1:FECompUltimoAutorizadoResponse/fev1:FECompUltimoAutorizadoResult/fev1:CbteNro", xc);
public static Element getSingleElement(Element element, String xpath) { Nodes nodes = element.query(xpath); return (nodes.size() == 1) ? (Element) nodes.get(0) : null; }
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; }
/** * convenience routine to get query CMLelements (iterating thorugh get(i) is * fragile if nodes are removed) * if query result is not a CMLElement it is omitted form list, so be careful * * @param element * @param xpath xpath relative to node * @param context * @return list of CMLelements - empty if none */ public static List<CMLElement> getCMLElements(Element node, String xpath, XPathContext context) { List<CMLElement> nodeList = new ArrayList<CMLElement>(); if (node != null) { Nodes nodes = node.query(xpath, context); for (int i = 0; i < nodes.size(); i++) { if (nodes.get(i) instanceof CMLElement) { nodeList.add((CMLElement)nodes.get(i)); } } } return nodeList; }
/** * convenience method to extract value of exactly one node. * uses element.query(xpath, xPathContext); * @param element * @param xpath * @param xPathContext defines prefix/namespace used in query * @return value if exactly 1 node (0 or many returns null) */ public static String getSingleValue(Element element, String xpath, XPathContext xPathContext) { String s = null; if (element == null) { LOG.warn("Null element"); } else { Nodes nodes = element.query(xpath, xPathContext); s = (nodes.size() == 1) ? nodes.get(0).getValue() : null; } return s; } /**
/** * convenience method to extract value of exactly one node.. * uses element.query(xpath, xPathContext); * @param element * @param xpath * @param xPathContext defines prefix/namespace used in query * @return value if exactly 1 node (0 or many returns null) */ public static String getSingleValue(Element element, String xpath) { String s = null; if (element == null) { LOG.warn("Null element"); } else { Nodes nodes = element.query(xpath); s = (nodes.size() == 1) ? nodes.get(0).getValue() : null; } return s; }
/** * convenience method to extract value of the first of one-or-more nodes. * uses element.query(xpath, xPathContext); * @param element * @param xpath * @param xPathContext defines prefix/namespace used in query * @return value if exactly 1 node (0 or many returns null) */ public static String getFirstValue(Element element, String xpath, XPathContext xPathContext) { String s = null; if (element == null) { LOG.warn("Null element"); } else { Nodes nodes = element.query(xpath, xPathContext); s = (nodes.size() >= 1) ? nodes.get(0).getValue() : null; } return s; }
/** * Returns the first child Element with the specified "id" attribute, or null, * if no matching element is found. * * @param id the id of the element to get * @return the element - or null if not found */ public Element getElementById(String id) { String query = ".//*[@id='" + id + "']"; Nodes nodes = xomElement.query(query); if (0 == nodes.size()) { return null; } return new Element((nu.xom.Element) nodes.get(0)); }
/** * Returns the first child Element with the specified "id" attribute, or null, * if no matching element is found. * * @param id the id of the element to get * @return the element - or null if not found */ public Element getElementById(String id) { String query = ".//*[@id='" + id + "']"; Nodes nodes = xomElement.query(query); if (0 == nodes.size()) { return null; } return new Element((nu.xom.Element) nodes.get(0)); }
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; }
/** * some formatted XML introduces spurious WS after text strings * @param element */ public static void stripTrailingWhitespaceinTexts(Element element) { Nodes texts = element.query("//text()"); for (int i = 0; i < texts.size(); i++) { Text text = (Text) texts.get(i); String value = text.getValue(); value = Util.rightTrim(value); text.setValue(value); } }
private void removeIrrelevantFooter(Element rootElement) { Element body = rootElement.getFirstChildElement("body"); body.removeChild(rootElement.query("//div[@class='footer']").get(0)); } }
private void removeIrrelevantFooter(Element rootElement) { Element body = rootElement.getFirstChildElement("body"); body.removeChild(rootElement.query("//div[@class='footer']").get(0)); } }
private String categorize(Element row) { String cssClass = row.getAttributeValue("class"); if (cssClass == null) { Element cell = (Element) row.query("td").get(0); cssClass = cell.getAttributeValue("class"); } Check.notNull(cssClass, "cssClass is null"); return cssClass.toUpperCase(); }
public String processFragment(String fragment, String csv) throws Exception { usernames = csvToCollection(csv); Document document = new TestRig() .withFixture(this) .processFragment(fragment) .getXOMDocument(); String result = ""; Element table = (Element) document.getRootElement().query("//table").get(0); Nodes rows = table.query(".//tr"); for (int i = 1; i < rows.size(); i++) { if (!result.equals("")) { result += ", "; } result += categorize((Element)rows.get(i)); } return result; }
public String processFragment(String fragment, String actualData) throws Exception { users = parse(actualData); Document document = new TestRig() .withFixture(this) .withResource(new Resource("/spec/concordion/common/command/verifyRows/strategies/toggle_html.js"), "") .withResource(new Resource("/spec/concordion/common/command/verifyRows/strategies/toggle_html.css"), "") .processFragment(fragment) .getXOMDocument(); String xml = document.getRootElement().query("//table").get(0).toXML(); return xml; }