JOOX:获得Element的价值

时间:2016-12-20 15:14:36

标签: java xml joox

在玩JOOX时,我似乎很难实现从元素中获取某些值的概念。

考虑以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <gwm:getAddressBookListResponse xmlns:gwm="http://schemas.novell.com/2005/01/GroupWise/methods" xmlns:gwt="http://schemas.novell.com/2005/01/GroupWise/types" xmlns:gwe="http://schemas.novell.com/2005/01/GroupWise/events">
            <gwm:books>
                <gwt:book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="gwt:AddressBook">
                    <gwt:id>584FB626.hbo.abp_poa1.104.16E3363.1.1.1@53</gwt:id>
                    <gwt:sid>1</gwt:sid>
                    <gwt:name>Contacts1</gwt:name>
                    <gwt:version>3</gwt:version>
                    <gwt:modified>2016-12-13T07:49:42Z</gwt:modified>
                    <gwt:isPersonal>1</gwt:isPersonal>
                    <gwt:isFrequentContacts>1</gwt:isFrequentContacts>
                </gwt:book>
                <gwt:book>
                    <gwt:id>GroupWiseSystemAddressBook@52</gwt:id>
                    <gwt:name>Contacts2</gwt:name>
                    <gwt:isPersonal>0</gwt:isPersonal>
                    <gwt:isFrequentContacts>0</gwt:isFrequentContacts>
                </gwt:book>
                <gwt:book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="gwt:AddressBook">
                    <gwt:id>584FB626.hbo.haporo_poa1.104.16E3363.1.3.1@53</gwt:id>
                    <gwt:sid>3</gwt:sid>
                    <gwt:name>Contacts3</gwt:name>
                    <gwt:version>3</gwt:version>
                    <gwt:modified>2016-12-13T07:49:42Z</gwt:modified>
                    <gwt:isPersonal>1</gwt:isPersonal>
                    <gwt:isFrequentContacts>0</gwt:isFrequentContacts>
                </gwt:book>
                <gwt:book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="gwt:AddressBook">
                    <gwt:id>584FB629.hbo.haporo_poa1.104.16E3363.1.5.1@53</gwt:id>
                    <gwt:sid>5</gwt:sid>
                    <gwt:name>Contacts4</gwt:name>
                    <gwt:version>2</gwt:version>
                    <gwt:modified>2016-12-13T07:49:45Z</gwt:modified>
                    <gwt:description>Bazinga !</gwt:description>
                    <gwt:isPersonal>1</gwt:isPersonal>
                    <gwt:isFrequentContacts>0</gwt:isFrequentContacts>
                </gwt:book>
            </gwm:books>
            <gwm:status>
                <gwt:code>0</gwt:code>
            </gwm:status>
        </gwm:getAddressBookListResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我对gwt:id元素感兴趣,或者更感兴趣的是它们的值。因此:

$(document).find("book").find("id").forEach(element -> {
        element... // now what?
    });

没有像element.getValue()和toString()那样的东西,因为它打印aut元素名称和值,虽然我只对值感兴趣。此外,getNodeValue()返回null

如何获取我所追求的元素的价值(例如 584FB629.hbo.haporo_poa1.104.16E3363.1.5.1@53

1 个答案:

答案 0 :(得分:1)

Element类实现了Node接口,因此您可以使用getTextContent()

请记住,在XML中,任何元素(标记)中包含的文本也是节点本身。因此,Element实际上没有&#34; text&#34;,只有(可能)后代文本节点。