转自:https://blog.csdn.net/weixin_39285616/article/details/78463091

转自: weixin_39285616的博客

大家想要看详细的对比,可以点上面的连接,查看原文章
这里只列出结果:
举例:

点击(此处)折叠或打开

  1. <book>
  2.     <author>Tom <em>John</em> cat</author>
  3.     <pricing>
  4.         <price>20</price>
  5.         <discount>0.8</discount>
  6.     </pricing>
  7. </book>

author.text()  返回的是 Tom cat
author.string() 返回的是 Tom John cat
author.data() 返回的是 
Tom John cat
但是data()函数性能不好,一般使用string()就好
另外,如果需要对取出来的数字进行运算,就只能使用data(),不能使用text()或 string(),因为XPath不支持字符串做数学运算。

总结

text()不是函数,XML结构的细微变化,可能会使得结果与预期不符,应该尽量少用,data()作为特殊用途的函数,可能会出现性能问题,如无特殊需要尽量不用,string()函数可以满足大部分的需求。

11-13 19:30
查看更多