2012-07-03 120 views
5

我用簡單的XML(簡單的XML-2.6.2.jar)來分析,如XML文件:簡單的XML解析XML列出

<?xml version="1.0" encoding="UTF-8" ?> 
<orderList> 
    <order id="1"> 
     <name>NAME1</name> 
    </order> 
    <order id="2"> 
     <name>NAME2</name> 
    </order> 
</orderList> 

根元素包含的子元素。 我想它是ArrayList,怎麼做?

回答

0

列表是一個接口,ArrayList的是其實現的一個,如:

List<Order> l = new ArrayList<Order>() 

所以,如果你有一個清單,你基本上你想要什麼。

+0

對不起,這不是匹配的意思。我想要的結果是列表或ArrayList ,而不是Orderlist.class。 – YETI

+0

因爲你的根對象是OrderList,所以你不能。 – Tomer

12

這裏有一個可能的解決方案,希望它可以幫助你:

註釋Order類:

@Root(name="order") 
public class Order 
{ 
    @Attribute(name="id", required=true) 
    private int id; 
    @Element(name="name", required=true) 
    private String name; 


    public Order(int id, String name) 
    { 
     this.id = id; 
     this.name = name; 
    } 


    public Order() { } 


    // Getter/Setter 
} 

Example類,包含列表:

@Root(name="elementList") 
public class Example 
{ 
    @ElementList(required=true, inline=true) 
    private List<Order> list = new ArrayList<>(); 

    // ... 
} 

而這裏有一些用於閱讀的代碼荷蘭國際集團代碼:

Serializer ser = new Persister(); 
Example example = ser.read(Example.class, file); // file = your xml file 
// 'list' now contains all your Orders 
+1

它應該被標記爲答案@YETI – FarOoOosa

+0

簡而言之:@ElementList(inline = true) – Kaito

+0

應該在Example類的@Root變換中使用「elementList」而不是「orderList?」。 – lustig

-1

如果我正確地解釋你的問題,你想命令的列表。我沒有測試此爲您的設置,但是這對我的作品進行了類似的XML結構(假設你有一個自定義類名爲Order):

List<Order> orders = new ArrayList<Order>(); 
XMLDOMParser parser = new XMLDOMParser(); 
AssetManager manager = context.getAssets(); 
InputStream stream; 
try {  
    stream = manager.open("test.xml"); //need full path to your file here - mine is stored in assets folder 
    Document doc = parser.getDocument(stream); 
}catch(IOException ex){ 
    System.out.printf("Error reading xml file %s\n", ex.getMessage()); 
} 
NodeList nodeList = doc.getElementsByTagName("order"); 
for (int i = 0; i < nodeList.getLength(); i++) { 
    Element e = (Element) nodeList.item(i); //each order item 
    Node order=nodeList.item(i); 
    subList = order.getFirstChild(); //get the name child node 
    orders.add(order); 
} 

//XMLDOMParser Class 
public class XMLDOMParser { 
    //Returns the entire XML document 
    public Document getDocument(InputStream inputStream) { 
     Document document = null; 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     try { 
      DocumentBuilder db = factory.newDocumentBuilder(); 
      InputSource inputSource = new InputSource(inputStream); 
      document = db.parse(inputSource); 
     } catch (ParserConfigurationException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (SAXException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (IOException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } 
     return document; 
    } 

    /* 
    * I take a XML element and the tag name, look for the tag and get 
    * the text content i.e for <employee><name>Kumar</name></employee> 
    * XML snippet if the Element points to employee node and tagName 
    * is name I will return Kumar. Calls the private method 
    * getTextNodeValue(node) which returns the text value, say in our 
    * example Kumar. */ 
    public String getValue(Element item, String name) { 
     NodeList nodes = item.getElementsByTagName(name); 
     return this.getTextNodeValue(nodes.item(0)); 
    } 

    private final String getTextNodeValue(Node node) { 
     Node child; 
     if (node != null) { 
      if (node.hasChildNodes()) { 
       child = node.getFirstChild(); 
       while(child != null) { 
        if (child.getNodeType() == Node.TEXT_NODE) { 
         return child.getNodeValue(); 
        } 
        child = child.getNextSibling(); 
       } 
      } 
     } 
     return ""; 
    } 
}