2012-07-03 9 views
5

używać prostych XML (proste-xml-2.6.2.jar) do analizowania pliku xml jak:Proste XML parse XML do listy

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

Element główny zawiera podelementy. Chcę być ArrayList, jak to zrobić?

Odpowiedz

0

Lista jest interfejsem, ArrayList jest jednym z jego realizacji, jak:

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

Więc jeśli masz listę, musisz po prostu to, co chcesz.

+0

Niestety, to nie pasuje do znaczenia. Chcę wynik jest lista lub ArrayList , a nie lista orderlist.class. – YETI

+0

Nie możesz, ponieważ twoim głównym obiektem jest OrderList. – Tomer

12

Oto możliwe rozwiązanie, nadzieję, że to pomaga:

Adnotacje o Order Klasa:

@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 klasy, zawierający listę:

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

    // ... 
} 

I oto kod do przeczytania ing kod:

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

należy oznaczyć jako odpowiedź @YETI – FarOoOosa

+0

w skrócie: @ElementList (inline = true) – Kaito

+0

Czy "elementList" w anotacji @Root w klasie Example zamiast "orderList?" – lustig

-1

Jeśli mam interpretować swoje pytanie poprawnie, chcesz listę zamówień. Nie przetestowałem tego dla twojej konfiguracji, ale działa to dla mnie dla podobnej struktury xml (zakłada, że ​​masz niestandardową klasę o nazwie Zamówienie):

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 ""; 
    } 
}