簡體   English   中英

如何避免在帶有xpath的節點之間返回空格和行返回?

[英]How can I keep from returning white spaces and line returns in-between nodes with xpath?

我正在嘗試學習使用Java xpath,但是遇到了問題。 當我使用getNodeName和getTextContent時,我最終抓住了出現在節點之間的空格和行返回。 例如,如果我的XML看起來像:

<node-i-am-looking-for-in-my-xml>
    <parent-node-01>
        <child-node-01>
            some text
        </child-node>
        <child-node-02>
            some more text
        </child-node>
        <child-node-03>
            even more text
        </child-node>
    </parent-node-01>
    <parent-node-02>
        <child-node-01>
            some text
        </child-node>
        <child-node-02>
            some more text
        </child-node>
        <child-node-03>
            even more text
        </child-node>
    </parent-node-02>
    <parent-node-03>
        <child-node-01>
            some text
        </child-node>
        <child-node-02>
            some more text
        </child-node>
        <child-node-03>
            even more text
        </child-node>
    </parent-node-03>
</node-i-am-looking-for-in-my-xml>

使用getNodeName時得到的內容如下:

child-node-01
#text
child-node-02
#text
child-node-03
#text

當我使用getTextContent時,它看起來像:

some text

some more text

even more text

這是我正在使用的代碼:

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    DocumentBuilder db = dbf.newDocumentBuilder();
    String filename = "C:\\Users\\Me\\file.xml";
    Document doc = db.parse(new FileInputStream(new File(filename)));
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    String expression;
    Node node;
    NodeList nodeList;

    expression = "//node-i-am-looking-for/*";
    nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
    System.out.println("nodeList.getLength(): " + nodeList.getLength());
    for (int i = 0; i < nodeList.getLength(); i++) {
        for(int j=1; j<(nodeList.item(i).getChildNodes().getLength()); j++){
            Node nowNode = nodeList.item(i).getChildNodes().item(j);
            System.out.println(nowNode.getNodeName() + ":" + nowNode.getTextContent());
        }
    }
}

在環顧Google時,我似乎需要使用“規范化空間”,但是我不知道該如何實現。

如您所見,空格在XML文本節點中很重要。 child-node-01的文本內容(或更准確地說,其父節點為child-node-01的文本節點的內容)實際上是'\\n some text\\n '

僅當需要在XPath表達式中處理此空格時,才使用normalize-space ,因為normalize-space是XPath函數。 例如,如果要選擇文本內容(除去前導/尾隨空白)為'some data'所有節點,則可以使用如下XPath:

//*[normalize-space(.) = 'some data']

但是,當您檢索文本內容時,您已經不在XPath世界中,而是回到Java中,因此使用以下方法可能會更好:

nowNode.getTextContent().trim()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM