2014-12-18 16 views
6

मैं एक xml मैं पार्स करने के लिएxml को हैशपैप में कैसे पार्स करें?

<?xml version="1.0" encoding="utf-8"?> 

<Details> 
    <detail-a> 

     <detail> attribute 1 of detail a </detail> 
     <detail> attribute 2 of detail a </detail> 
     <detail> attribute 3 of detail a </detail> 

    </detail-a> 

    <detail-b> 
     <detail> attribute 1 of detail b </detail> 
     <detail> attribute 2 of detail b </detail> 

    </detail-b> 


</Details> 

मैं इस एक्सएमएल से अपनी पसंद की कोई विधि है कि यह पार्स जाएगा HashMap है कि कुंजी एक स्ट्रिंग और मूल्य है लिखने के लिए होगा चाहते हैं का एक उदाहरण है स्ट्रिंग की एक सूची है ।

उदाहरण के लिए

: कुंजी "विस्तार एक" मूल्य = { "विस्तार एक की विशेषता 1", "विस्तार एक की विशेषता 2", "विस्तार एक की विशेषता 3"}

और इतने पर ..

ऐसा करने का सबसे अच्छा तरीका क्या है? क्योंकि मैं उलझन में मिल गया: \

मैं इतनी दूर मुद्रित करने के लिए विस्तार से-एक और विस्तार-बी की कोशिश करने का मिला, लेकिन मैं खाली मिलता है ...

public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 

     DocumentBuilder builder; 
     try { 
      builder = factory.newDocumentBuilder(); 
      File f= new File("src/Details.xml"); 
      Document doc=builder.parse(f); 
      Element root=doc.getDocumentElement(); 
      NodeList children=root.getChildNodes(); 
      for(int i=0;i<children.getLength();i++) 
      { 
       Node child=children.item(i); 
       if (child instanceof Element) 
       { 
        Element childElement=(Element) child; 
        Text textNode=(Text)childElement.getFirstChild(); 
        String text=textNode.getData().trim(); 
        System.out.println(text); 

       } 
      } 

     } catch (ParserConfigurationException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (SAXException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
+0

क्या आपने कुछ भी कोशिश की? –

+0

एक्सस्ट्रीम उपयोगी होगा – sol4me

+0

आप एक्सएमएल को एक स्ट्रिंग में परिवर्तित कर सकते हैं और फिर आप इसे हैशैप –

उत्तर

3

उपयोग JAXBxml से पढ़ सकते हैं और एक करने के लिए इसे बचाने के लिए कस्टम वस्तु।

कस्टम वस्तु वर्ग:

import java.util.List; 

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlElementWrapper; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 

@XmlRootElement(name = "Details") 
@XmlType(propOrder = { "detailA", "detailB" }) 
public class Details { 
    private List<String> detailA; 
    private List<String> detailB; 

    public void setDetailA(List<String> detailA) { 
     this.detailA = detailA; 
    } 

    @XmlElementWrapper(name = "detail-a") 
    @XmlElement(name = "detail") 
    public List<String> getDetailA() { 
     return detailA; 
    } 

    public void setDetailB(List<String> detailB) { 
     this.detailB = detailB; 
    } 

    @XmlElementWrapper(name = "detail-b") 
    @XmlElement(name = "detail") 
    public List<String> getDetailB() { 
     return detailB; 
    } 
} 

वस्तु में अपने xml से डेटा निकालें, तब इच्छित रूप में एक नक्शा करने के लिए सामग्री जोड़ें:

public static void main(String[] args) throws JAXBException, FileNotFoundException { 
    System.out.println("Output from our XML File: "); 
    JAXBContext context = JAXBContext.newInstance(Details.class); 
    Unmarshaller um = context.createUnmarshaller(); 
    Details details = (Details)um.unmarshal(new FileReader("details.xml")); 
    List<String> detailA = details.getDetailA(); 
    List<String> detailB = details.getDetailB(); 

    Map<String, String[]> map = new HashMap<String, String[]>(); 
    map.put("detail-a", detailA.toArray(new String[detailA.size()])); 
    map.put("detail-b", detailB.toArray(new String[detailB.size()])); 


    for (Map.Entry<String, String[]> entry : map.entrySet()) { 
     //key "detail a" value={"attribute 1 of detail a","attribute 2 of detail a","attribute 3 of detail a"} 
     System.out.print("Key \"" +entry.getKey()+"\" value={"); 
     for(int i=0;i<entry.getValue().length;i++){ 
      if(i!=entry.getValue().length-1){ 
       System.out.print("\""+entry.getValue()[i]+"\","); 
      } 
      else{ 
       System.out.print("\""+entry.getValue()[i]+"\"}"); 
      } 
     } 
     System.out.println(); 
    } 
} 

आउटपुट होगा:

 
Output from our XML File: 
Key "detail-a" value={"attribute 1 of detail a","attribute 2 of detail a","attribute 3 of detail a"} 
Key "detail-b" value={"attribute 1 of detail b","attribute 2 of detail b"} 

एक नोट के रूप में: यह केवल आपके प्रश्न में इनपुट के रूप में प्रदान किए गए एक्सएमएल के लिए काम करेगा, अगर आपको अधिक जानकारी जोड़ने की आवश्यकता है detail-c की तरह है और इसलिए आपको उन्हें अपनी कस्टम ऑब्जेक्ट में भी परिभाषित करना होगा।

इस्तेमाल किया एक्सएमएल:

<?xml version="1.0" encoding="utf-8"?> 
<Details> 
    <detail-a> 
     <detail>attribute 1 of detail a</detail> 
     <detail>attribute 2 of detail a</detail> 
     <detail>attribute 3 of detail a</detail> 
    </detail-a> 
    <detail-b> 
     <detail>attribute 1 of detail b</detail> 
     <detail>attribute 2 of detail b</detail> 
    </detail-b> 
</Details> 
2

मैं XMLBeam कि "विस्तार-x" उसके उप-तत्व के किसी भी संख्या का उपयोग कर के साथ काम करता एक बहुत छोटे समाधान पेश करने के लिए विरोध नहीं कर सकते हैं।

public class Tetst { 

@XBDocURL("resource://test.xml") 
public interface Projection { 
    @XBRead("name()") 
    String getName(); 

    @XBRead("./detail") 
    List<String> getDetailStrings(); 

    @XBRead("/Details/*") 
    List<Projection> getDetails(); 
} 

@Test 
public void xml2Hashmap() throws IOException { 
    HashMap<String, List<String>> hashmap = new HashMap<String, List<String>>(); 
    for (Projection p : new XBProjector().io().fromURLAnnotation(Projection.class).getDetails()) { 
     System.out.println(p.getName() + ": " + p.getDetailStrings()); 
     hashmap.put(p.getName(), p.getDetailStrings()); 
    } 
} 
} 

यह आपके उदाहरण test.xml के लिए

detail-a: [ attribute 1 of detail a , attribute 2 of detail a , attribute 3 of detail a ] 
detail-b: [ attribute 1 of detail b , attribute 2 of detail b ] 

बाहर प्रिंट और एक hashmap भरता है।

संबंधित मुद्दे