2010-11-09 14 views
23

में एक विशेषता मान प्राप्त करना मेरे पास एक XML स्ट्रिंग है और मैं प्रत्येक तत्व के लिए लूप में "नाम" का विशेषता मान प्राप्त करना चाहता हूं। मैं उसको कैसे करू? मैं javax.xml.parsers लाइब्रेरी का उपयोग कर रहा हूँ।xml तत्व

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

यह मैं क्या अब

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(); 
     } 

उत्तर

4

तक कोशिश कर रहा हूँ है मैं मुझे मिल गया लगता है। मुझे स्पष्ट रूप से org.w3c.dom.Element का उपयोग करना होगा। मेरे पास एक अलग तत्व क्षेत्र भी था।

64

कैसे के बारे में:

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()); 
     } 
    } 
} 
1

नीचे vtd-xml में यह करने के लिए कोड है। यह मूल रूप से XML/"xml/item/@ name" के XPath के साथ XML से पूछताछ करता है।

import com.ximpleware.*; 

public class getAttrs{ 

    public static void main(String[] s) throws VTDException{ 
     VTDGen vg = new VTDGen(); 
     if (!vg.parseFile("input.xml",false)) // turn off namespace 
       return; 
     VTDNav vn = vg.getNav(); 
     AutoPilot ap = new AutoPilot(vn); 
     ap.selectXPath("/xml/item/@name"); 
     int i=0; 
     while((i=ap.evalXPath())!=-1){ 
       System.out.println(" item name is ===>"+vn.toString(i+1)); 
     } 
    } 
} 
संबंधित मुद्दे