Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an attribute value in xml element

Tags:

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();         } 
like image 358
yogsma Avatar asked Nov 09 '10 21:11

yogsma


People also ask

How do I get the value of a tag in XML?

Get the Value of an Element Element nodes do not have a text value. The text value of an element node is stored in a child node. This node is called a text node. To retrieve the text value of an element, you must retrieve the value of the elements' text node.

What is attribute value XML?

This syntax element is the default name-value element supported by the XML parser. Use it to represent the attributes that are associated with its parent element. The name and value of the syntax element correspond to the name and value of the attribute that is being represented.

How are attribute values in XML represented?

An attribute should be declared using the attribute-list declaration in the DTD (Document Type Definition). An attribute element is used without any quotation and the attribute value is used in a single (' ') or double quotation (” “). An attribute name and its value should always appear in pair.

What is attribute and element in XML?

Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements. An XML attribute is always a name-value pair.


1 Answers

How about:

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 {      public static void main(String[] args) throws Exception {         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();         DocumentBuilder db = dbf.newDocumentBuilder();         Document document = db.parse(new File("input.xml"));         NodeList nodeList = document.getElementsByTagName("Item");         for(int x=0,size= nodeList.getLength(); x<size; x++) {             System.out.println(nodeList.item(x).getAttributes().getNamedItem("name").getNodeValue());         }     } } 
like image 58
bdoughan Avatar answered Oct 21 '22 16:10

bdoughan