com.jcabi.xml.XML.xpath()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(6.1k)|赞(0)|评价(0)|浏览(113)

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

XML.xpath介绍

[英]Find and return text elements or attributes matched by XPath address.

The XPath query should point to text elements or attributes in the XML document. If any nodes of different types (elements, comments, etc.) are found in result node list - a RuntimeException will be thrown.

Alternatively, the XPath query can be a function or expression that returns a single value instead of pointing to a set of nodes. In this case, the result will be a List containing a single String, the content of which is the result of the evaluation. If the expression result is not a String, it will be converted to a String representation and returned as such. For example, a document containing three <a> elements, the input query "count(//a)", will return a singleton List with a single string value "3".

This is a convenient method, which is used (according to our experience) in 95% of all cases. Usually you don't need to get anything else but a text value of some node or an attribute. And in most cases you are interested to get just the first value (use xpath(..).get(0)). But when/if you need to get more than just a plain text - use #nodes(String).

The List returned will throw IndexOutOfBoundsExceptionif you try to access a node which wasn't found by this XPath query.

An IllegalArgumentException is thrown if the parameter passed is not a valid XPath expression.
[中]查找并返回与XPath地址匹配的文本元素或属性。
XPath查询应该指向XML文档中的文本元素或属性。如果在结果节点列表中找到任何不同类型(元素、注释等)的节点,将引发RuntimeException。
或者,XPath查询可以是返回单个值而不是指向一组节点的函数或表达式。在本例中,结果将是一个包含单个字符串的列表,其内容是评估的结果。如果表达式结果不是字符串,它将被转换为字符串表示形式并返回。例如,一个包含三个<a>元素的文档,即输入查询“count(//a)”,将返回一个带有单个字符串值“3”的单例列表。
这是一种方便的方法,根据我们的经验,95%的病例都使用这种方法。通常,除了某个节点或属性的文本值之外,不需要获取任何其他内容。在大多数情况下,您只想获得第一个值(使用xpath(…)。得到(0)。但是,当/如果您需要获取的不仅仅是纯文本,请使用#节点(字符串)。
如果试图访问此XPath查询未找到的节点,则返回的列表将抛出IndexOutofBoundsException。
如果传递的参数不是有效的XPath表达式,则会引发IllegalArgumentException。

代码示例

代码示例来源:origin: yegor256/netbout

@Override
  public String author() {
    return xml.xpath("author/text()").get(0);
  }
};

代码示例来源:origin: jcabi/jcabi-github

@Override
  public long number() {
    return Long.valueOf(this.data.xpath("//id/text()").get(0));
  }
}

代码示例来源:origin: yegor256/netbout

@Override
public long number() {
  return Long.parseLong(
    xml.xpath("number/text()").get(0)
  );
}
@Override

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

XML xml = new XMLDocument(new StreamSource(reader));
System.out.printf(
  "rise=%s, set=%s%n",
  xml.xpath("/weatherdata/sun/@rise").get(0),
  xml.xpath("/weatherdata/sun/@set").get(0)
);

代码示例来源:origin: jcabi/jcabi-github

@Override
  public Event map(
    final XML xml
  ) {
    return this.evts.get(
      Integer.parseInt(xml.xpath("number/text()").get(0))
    );
  }
}

代码示例来源:origin: yegor256/netbout

@Override
public Date date() throws IOException {
  try {
    return DateFormatUtils.ISO_DATETIME_FORMAT.parse(
      xml.xpath("date/text()").get(0)
    );
  } catch (final ParseException ex) {
    throw new IOException(ex);
  }
}
@Override

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

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
XML xml = new XMLDocument("<resp>...</resp>");
String status = xml.xpath("/resp/status/text()").get(0);

代码示例来源:origin: jcabi/jcabi-github

@Override
  public Organization map(final XML xml) {
    return this.organizations.get(
      xml.xpath("login/text()").get(0)
    );
  }
}

代码示例来源:origin: jcabi/jcabi-github

@Override
public InputStream raw() throws IOException {
  return new ByteArrayInputStream(
    DatatypeConverter.parseBase64Binary(
      this.storage.xml().xpath(
        String.format("%s/content/text()", this.xpath())
      ).get(0)
    )
  );
}

代码示例来源:origin: jcabi/jcabi-github

@Override
public boolean starred() throws IOException {
  final List<String> xpath = this.storage.xml().xpath(
    String.format("%s/star/login/text()", this.xpath())
  );
  return !xpath.isEmpty()
    && StringUtils.equalsIgnoreCase(this.self, xpath.get(0));
}

代码示例来源:origin: jcabi/jcabi-github

@Override
public boolean exists() throws IOException {
  return this.storage.xml().xpath(
    String.format("%s/number/text()", this.xpath())
  ).size() == 1;
}

代码示例来源:origin: jcabi/jcabi-github

@Override
public boolean isCollaborator(
  final String user
) throws IOException {
  return !this.storage.xml().xpath(
    String.format("%s/user[login='%s']/text()", this.xpath(), user)
  ).isEmpty();
}

代码示例来源:origin: yegor256/netbout

@Override
public Locale locale() throws IOException {
  return new Locale(
    this.request.fetch()
      .as(XmlResponse.class)
      .xml()
      .xpath("/page/alias/locale/text()")
      .get(0)
  );
}

代码示例来源:origin: jcabi/jcabi-http

/**
 * Follow XML link.
 * @param query XPath query to fetch new URI
 * @return New request
 */
public Request rel(final String query) {
  this.assertXPath(query);
  return new RestResponse(this).jump(
    URI.create(this.xml().xpath(query).get(0))
  );
}

代码示例来源:origin: yegor256/netbout

@Override
public boolean subscription() throws IOException {
  return Boolean.valueOf(
    this.request.fetch()
      .as(RestResponse.class)
      .assertStatus(HttpURLConnection.HTTP_OK)
      .as(XmlResponse.class)
      .xml()
      .xpath("/page/bout/subscription/text()")
      .get(0)
  );
}

代码示例来源:origin: yegor256/netbout

@Override
public boolean unseen() throws IOException {
  return Boolean.parseBoolean(
    this.request.fetch()
      .as(RestResponse.class)
      .assertStatus(HttpURLConnection.HTTP_OK)
      .as(XmlResponse.class)
      .xml()
      .xpath(this.xpath("unseen/text()"))
      .get(0)
  );
}

代码示例来源:origin: yegor256/netbout

@Override
public String title() throws IOException {
  return this.request.fetch()
    .as(RestResponse.class)
    .assertStatus(HttpURLConnection.HTTP_OK)
    .as(XmlResponse.class)
    .xml()
    .xpath("/page/bout/title/text()")
    .get(0);
}

代码示例来源:origin: yegor256/netbout

@Override
public String author() throws IOException {
  return this.request.fetch()
    .as(RestResponse.class)
    .assertStatus(HttpURLConnection.HTTP_OK)
    .as(XmlResponse.class)
    .xml()
    .xpath(this.xpath("author/text()"))
    .get(0);
}

代码示例来源:origin: yegor256/netbout

@Override
public String ctype() throws IOException {
  return this.request.fetch()
    .as(RestResponse.class)
    .assertStatus(HttpURLConnection.HTTP_OK)
    .as(XmlResponse.class)
    .xml()
    .xpath(this.xpath("ctype/text()"))
    .get(0);
}

代码示例来源:origin: co.stateful/java-sdk

@Override
public Iterable<String> names() throws IOException {
  return this.request.fetch()
    .as(RestResponse.class)
    .assertStatus(HttpURLConnection.HTTP_OK)
    .as(XmlResponse.class)
    .xml()
    .xpath("/page/counters/counter/name/text()");
}

相关文章

微信公众号

最新文章

更多