簡體   English   中英

JAVA:從XML文檔中提取元素

[英]JAVA : Extract element from XML Document

我正在使用javax.xml.parsers來瀏覽XML文檔,如下所示:

`

<ContextElement>
    <DimensionNode>Role</DimensionNode>
    <Value>Administration</Value>
    <TailoringExpressions>
        <TailoringExpression>
            <Relation>Student</Relation>
            <ProjAtt>
                <Attribute>Matr</Attribute>
                <Attribute>SName</Attribute>
                <Attribute>SSurname</Attribute>
                <Attribute>SDateOfBirth</Attribute>
                <Attribute>SEmail</Attribute>
                <Attribute>SAddress</Attribute>
            </ProjAtt>
            <Condition/>
            <SemiJoinRel/>
            <SemiJoinOn/>
            <SemiJoinCond/>
        </TailoringExpression>
    </TailoringExpressions>
</ContextElement>
<ContextElement>
    <DimensionNode>Deadline</DimensionNode>
    <Value>Lost</Value>
    <TailoringExpressions>
        <TailoringExpression>
            <Relation>Deadline</Relation>
            <ProjAtt>
                <Attribute>IdDeadline</Attribute>
                <Attribute>Student</Attribute>                  
                <Attribute>DeadlineDate</Attribute>
                <Attribute>Description</Attribute>
                <Attribute>IsMet</Attribute>
            </ProjAtt>
            <Condition>DeadlineDate LT CurrentDate AND IsMet=False</Condition>
            <SemiJoinRel/>
            <SemiJoinOn/>
            <SemiJoinCond/>
        </TailoringExpression>
    </TailoringExpressions>
</ContextElement>

`

我有一個問題,因為我需要提取對象/節點ContextElement,它的DimensionNode值為“ Role”,而Value值為“ Administration”,因此我無法編寫有效的代碼! 有人可以告訴我該怎么做嗎? 謝謝

我認為提取值的最佳方法是使用xpath:

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/widgets/widget";
InputSource inputSource = new InputSource("widgets.xml");
NodeSet nodes = (NodeSet) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);

有關更多信息,請參見oracle文檔

暫無
暫無

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

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