猿问

在xml元素中获取属性值

我有一个像这样的xml字符串,我想在每个元素的循环中获取“名称”的属性值。我怎么做?我正在使用javax.xml.parsers库。


<xml>

    <Item type="ItemHeader" name="Plan Features" id="id_1"/>

    <Item type="Deductible" name="Deductible" id="a">Calendar Year

        <Item type="Text" name="Individual" id="b">200</Item>

        <Item type="Text" name="Family" id="c">350</Item>

    </Item>

    <Item lock="|delete|" type="Empty" name="Out-of-Pocket Annual Maximum" id="id_2">

        <Item type="Text" name="Individual" id="d">400</Item>

        <Item type="Currency" name="Individual Out-of-Network" id="id_5">$320.00</Item>

        <Item type="Text" name="Family" id="e">670</Item>

    </Item>

    <Item type="Text" name="Life Time Maximum" id="u">8000</Item>

    <Item type="Text" name="Coinsurance" id="f">60</Item>

    <Item type="Text" name="Office Visits" id="g">10</Item>

    <Item type="Text" name="Routine Physicals" id="h">12</Item>

    <Item type="Text" name="Preventive Care" id="m"/>

    <Item type="Text" name="Physician Services" id="i"/>

    <Item type="Text" name="Emergency Room Services / Urgent Care" id="j"/>

    <Item type="Text" name="Hospital Admission Services" id="k"/>

    <Item type="Text" name="Chiropractic" id="n"/>

    <Item type="Text" name="Prescription Drugs" id="l"/>

    <Item type="Text" name="Specialty Drugs" id="o"/>

    <Item type="Currency" name="Custom Field 2" id="id_4">$250.00</Item>

    <Item type="Boolean" name="Pre Tax Reduction Available" id="t">false</Item>

    <Item type="Boolean" name="Conversion Privilege" id="p">false</Item>

    <Item type="ItemHeader" name="Plan Setup" id="id_3"/>

    <Item type="Termination" name="Benefit Termination Date" id="q">Immediate</Item>

    <Item type="Determination" name="Premium Redetermination Date" id="r">Not Applicable</Item>

    <Item type="Participation" name="Participation Requirement" id="s"/>

</xml>

弑天下
浏览 699回答 3
3回答

互换的青春

怎么样:import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.NodeList;public class Demo {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();&nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilder db = dbf.newDocumentBuilder();&nbsp; &nbsp; &nbsp; &nbsp; Document document = db.parse(new File("input.xml"));&nbsp; &nbsp; &nbsp; &nbsp; NodeList nodeList = document.getElementsByTagName("Item");&nbsp; &nbsp; &nbsp; &nbsp; for(int x=0,size= nodeList.getLength(); x<size; x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(nodeList.item(x).getAttributes().getNamedItem("name").getNodeValue());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕森卡

我想我明白了。我必须org.w3c.dom.Element明确使用。我也有一个不同的Element字段。

繁星coding

下面是在vtd-xml中执行此操作的代码。它基本上使用“ / xml / item / @ name”的XPath查询XML。import com.ximpleware.*;public class getAttrs{&nbsp; &nbsp;public static void main(String[] s) throws VTDException{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VTDGen vg = new VTDGen();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!vg.parseFile("input.xml",false)) // turn off namespace&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VTDNav vn = vg.getNav();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AutoPilot ap =&nbsp; new AutoPilot(vn);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ap.selectXPath("/xml/item/@name");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int i=0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while( (i=ap.evalXPath())!=-1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" item name is ===>"+vn.toString(i+1));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}}
随时随地看视频慕课网APP
我要回答