package com.chinaitop.depot.intelligent.SendMessagePT;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/** 
 * 用DOM4J方法读取xml文件 
 * @author lfx 
 */  
public class ReadXMLByJDom {  
      
    private static List<SmsDataVO> bookList = null;
    private static SmsDataVO book = null;
      
    public static List<SmsDataVO> getBooks(String xml) throws Exception{
          
        SAXReader reader = new SAXReader();  
        try {  
            Document document = reader.read(new ByteArrayInputStream(xml.getBytes("UTF-8"))); 
            Element bookstore = document.getRootElement();  
            Iterator bookit = bookstore.elementIterator();  
              
            bookList = new ArrayList<SmsDataVO>();
            while(bookit.hasNext()){  
                book = new SmsDataVO();
                //遍历bookElement的属性  
                Element child = (Element) bookit.next();  
                String nodeName = child.getName();  
                if(("State").equals(nodeName)){
                    String state = child.getStringValue();  
                    book.setState(Integer.parseInt(state)); 
                }else if(("MsgID").equals(nodeName)){
                    String msgID = child.getStringValue();  
                    book.setMsgId(msgID);  
                }else if(("MsgState").equals(nodeName)){
                    String msgState = child.getStringValue();  
                    book.setMsgState(msgState); 
                }else if(("Reserve").equals(nodeName)){
                    String reserve = child.getStringValue();  
                    book.setReserve(reserve);  
                }  
                bookList.add(book);  
                book = null;  
            }  
        } catch (DocumentException e) {  
          
            e.printStackTrace();  
        }  
        return bookList;  
    }  
   
}
