在xml元素中获取属性值 [英] Getting an attribute value in xml element

查看:67
本文介绍了在xml元素中获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an xml string like this and I want to get attribute value of "name" in a loop for each element. How do I do that? I am using javax.xml.parsers library.

<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>

这就是我现在要做的事情

This is what I am trying till now

DocumentBuilderFactory dbc = DocumentBuilderFactory.newInstance();
        DocumentBuilder dbuilder;
        try {
            dbuilder = dbc.newDocumentBuilder();
            Document doc = dbuilder.parse(new InputSource(new StringReader(plan.getProvisions())));
            NodeList nl = doc.getElementsByTagName("Item");
            for(int i = 0 ; i < nl.getLength(); i++){
                if(i == row){                   
                    Element e = (Element)nl.item(i);
                    description = e.getAttribute("name");
                }
            }
        } catch (ParserConfigurationException e) {          
            e.printStackTrace();
        } catch (SAXException e) {          
            e.printStackTrace();
        } catch (IOException e) {           
            e.printStackTrace();
        }


推荐答案

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

I think I got it. I have to use org.w3c.dom.Element explicitly. I had a different Element field too.

这篇关于在xml元素中获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆