nu.xom.Element.query()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(130)

本文整理了Java中nu.xom.Element.query()方法的一些代码示例,展示了Element.query()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Element.query()方法的具体详情如下:
包路径:nu.xom.Element
类名称:Element
方法名:query

Element.query介绍

暂无

代码示例

代码示例来源:origin: org.xml-cml/cmlxom

/** 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);
}

代码示例来源:origin: stackoverflow.com

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

代码示例来源:origin: concordion/concordion

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]);
}

代码示例来源:origin: stackoverflow.com

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);

代码示例来源:origin: org.xml-cml/cmlxom

public static Element getSingleElement(Element element, String xpath) {
  Nodes nodes = element.query(xpath);
  return (nodes.size() == 1) ? (Element) nodes.get(0) : null;
}

代码示例来源:origin: org.concordion/concordion

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]);
}

代码示例来源:origin: org.xml-cml/cmlxom

/**
 * 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;
}

代码示例来源:origin: org.xml-cml/cmlxom

/**
 * 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;
}

代码示例来源:origin: org.xml-cml/cmlxom

/**
 * 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;
}
/**

代码示例来源:origin: org.xml-cml/cmlxom

/**
 * 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;
}

代码示例来源:origin: org.xml-cml/cmlxom

/**
 * 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;
}

代码示例来源:origin: org.concordion/concordion

/**
 * 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));
}

代码示例来源:origin: concordion/concordion

/**
 * 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));
}

代码示例来源:origin: org.xml-cml/cmlxom

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;
}

代码示例来源:origin: org.xml-cml/cmlxom

/**
 * 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);
  }
}

代码示例来源:origin: concordion/concordion

private void removeIrrelevantFooter(Element rootElement) {
    Element body = rootElement.getFirstChildElement("body");
    body.removeChild(rootElement.query("//div[@class='footer']").get(0));
  }
}

代码示例来源:origin: concordion/concordion

private void removeIrrelevantFooter(Element rootElement) {
    Element body = rootElement.getFirstChildElement("body");
    body.removeChild(rootElement.query("//div[@class='footer']").get(0));
  }
}

代码示例来源:origin: concordion/concordion

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

代码示例来源:origin: concordion/concordion

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;
}

代码示例来源:origin: concordion/concordion

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;
}

相关文章