如何从XPath中的子值节点获取父文本值?

时间:2013-03-11 21:23:59

标签: java xml xpath

如何从addressinformation(在本例中为 999123 )获取UUID,其中地址名称为“BILL_TO”(如果有),使用Java XPath?

这是我的XML:

    <Company>
        <Name>My Company</Name>
        <LastName>Smith</LastName>
        <FirstName>Jim</FirstName>
        <street>123 Main Street</street>
        <AddressInformation>
                <UUID>543233<UUID>
            <Address>
                <UUID>223223</UUID>
                <AddressID>
                    <AddressName>Default</AddressName>
                </AddressID>
            </Address>
        </AddressInformation>
        <AddressInformation>
                <UUID>999123<UUID>
            <Address>
                <UUID>443211</UUID>
                <AddressID>
                    <AddressName>BILL_TO</AddressName>
                </AddressID>
            </Address>
        </AddressInformation>
        <city>Mytown</city>
        <state>NY</state>
        <zip>11234</zip>
</Company>

1 个答案:

答案 0 :(得分:3)

好的,首先你的XML没有很好地形成,所以我为你解决了这个问题

 <Company>
    <Name>My Company</Name>
    <LastName>Smith</LastName>
    <FirstName>Jim</FirstName>
    <street>123 Main Street</street>
    <AddressInformation>
            <UUID>543233</UUID>
        <Address>
            <UUID>223223</UUID>
            <AddressID>
                <AddressName>Default</AddressName>
            </AddressID>
        </Address>
    </AddressInformation>
    <AddressInformation>
            <UUID>999123</UUID>
        <Address>
            <UUID>443211</UUID>
            <AddressID>
                <AddressName>BILL_TO</AddressName>
            </AddressID>
        </Address>
    </AddressInformation>
    <city>Mytown</city>
    <state>NY</state>
    <zip>11234</zip>
</Company>

然后这是xpath应该为你做的伎俩:

//Company/AddressInformation[Address/AddressID/AddressName='BILL_TO']/UUID