获取特定名称标签的价值

时间:2012-12-17 10:50:10

标签: java xml dom xpath

我有这个xml:

<ns0:data>
<ns0:row>
    <ns0:column>
        <ns0:name>F_DOCNUMBER</ns0:name>
        <ns0:value>617271</ns0:value>
    </ns0:column>
    <ns0:column>
        <ns0:name>DOCDATE</ns0:name>
        <ns0:value>15685</ns0:value>
    </ns0:column>
    <ns0:column>
        <ns0:name>DOCTYPE</ns0:name>
        <ns0:value>1,999,152</ns0:value>
    </ns0:column>
    <ns0:column>
        <ns0:name>INSUREDIDNUMBER</ns0:name>
        <ns0:value>310794763</ns0:value>
    </ns0:column>
    <ns0:column>
        <ns0:name>BATCHNAME</ns0:name>
        <ns0:value>L1000024831310001</ns0:value>
    </ns0:column>
</ns0:row>
<ns0:row>
    <ns0:column>
        <ns0:name>F_DOCNUMBER</ns0:name>
        <ns0:value>617284</ns0:value>
    </ns0:column>
    <ns0:column>
        <ns0:name>DOCDATE</ns0:name>
        <ns0:value>15685</ns0:value>
    </ns0:column>
    <ns0:column>
        <ns0:name>DOCTYPE</ns0:name>
        <ns0:value>1,999,403</ns0:value>
    </ns0:column>
    <ns0:column>
        <ns0:name>INSUREDIDNUMBER</ns0:name>
        <ns0:value>310794763</ns0:value>
    </ns0:column>
    <ns0:column>
        <ns0:name>BATCHNAME</ns0:name>
        <ns0:value>L1000024831320002</ns0:value>
    </ns0:column>
</ns0:row>
<ns0:row>
    <ns0:column>
        <ns0:name>F_DOCNUMBER</ns0:name>
        <ns0:value>617283</ns0:value>
    </ns0:column>
    <ns0:column>
        <ns0:name>DOCDATE</ns0:name>
        <ns0:value>15685</ns0:value>
    </ns0:column>
    <ns0:column>
        <ns0:name>DOCTYPE</ns0:name>
        <ns0:value>1,999,235</ns0:value>
    </ns0:column>
    <ns0:column>
        <ns0:name>INSUREDIDNUMBER</ns0:name>
        <ns0:value>310794763</ns0:value>
    </ns0:column>
    <ns0:column>
        <ns0:name>BATCHNAME</ns0:name>
        <ns0:value>L1000024831320001</ns0:value>
    </ns0:column>
</ns0:row>
</ns0:data>

我需要获取名称为&#34; DOCTYPE&#34;的所有价值标签。

稍后我需要检查并更改其值。

如何获得此节点?

1 个答案:

答案 0 :(得分:0)

您可以使用LINQXML来获取所需的节点

XDocument doc = XDocument.Load("YourXmlFilePath");
XNamespace ns = "YourNameSpacegoesHere";

var query = from d in doc.Descendants(ns + "column")
            where d.Element(ns + "name").Value.ToString() == "DOCTYPE"                  
            select d;

现在query拥有您所需的所有节点。