V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
Spectre

Xpath 选取 text 值

  •  1
     
  •   Spectre · Oct 20, 2016 · 4748 views
    This topic created in 3490 days ago, the information mentioned may be changed or developed.

    ddd aaa bbb

    如何选取 aaa 的值哇!!

    11 replies    2016-10-22 14:54:16 +08:00
    imn1
        1
    imn1  
       Oct 20, 2016
    不能,正则吧
    Spectre
        2
    Spectre  
    OP
       Oct 20, 2016
    @imn1 好像上面没显示出来 是这种<span></span>aaa<span></span> 我要获取 aaa 的值
    imn1
        3
    imn1  
       Oct 20, 2016
    /xpath/text()
    hw20686832
        4
    hw20686832  
       Oct 20, 2016
    xpath2.0 有个 tokenize 方法,类似于 Python 的 split
    argsno
        5
    argsno  
       Oct 20, 2016
    text()
    anguskwan
        6
    anguskwan  
       Oct 20, 2016
    楼主要取 xml 里的第二个孩子,还是 text 里第二列?
    cuebyte
        7
    cuebyte  
       Oct 20, 2016
    text() 取出来自己做个 split(' ')[1] 啊
    Honwhy
        8
    Honwhy  
       Oct 20, 2016
    ```
    NodeList spanNode = (NodeList) xpath.evaluate("/widgets/widget[3]/node()", inputSource, XPathConstants.NODESET);
    for(int i = 0; i < spanNode.getLength(); i++) {
    Node node = spanNode.item(i);
    if(node.getNodeType() == Node.TEXT_NODE) {
    System.out.println(spanNode.item(i).getTextContent());
    }
    }
    ```
    ```
    <?xml version="1.0" encoding="UTF-8" ?>
    <widgets xmlns:prefix="http://www.eclipse.org/birt/2005/design">
    <widget>
    <manufacturer/>
    <dimensions/>
    </widget>
    <widget>
    <manufacturer/>
    <dimensions>101</dimensions>
    <attr1 name="honey">10333</attr1>
    </widget>
    <widget>
    <span>bbb</span>
    aaa
    <span>ccc</span>
    </widget>
    </widgets>
    ```
    dsg001
        9
    dsg001  
       Oct 21, 2016
    dom 操作用 xpath , text 操作用 re
    Spectre
        10
    Spectre  
    OP
       Oct 22, 2016
    @dsg001 re 是啥。。没用过。。
    dsg001
        11
    dsg001  
       Oct 22, 2016
    @Spectre 正则
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3273 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 167ms · UTC 13:08 · PVG 21:08 · LAX 06:08 · JFK 09:08
    ♥ Do have faith in what you're doing.