2013-04-19 2 views
0

내 안드로이드 폰에 파서에 문제가 있습니다! 여기 여기 코드는 XMLandroid 용 XML 파서가 마지막 자식을 읽지 않습니다.

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<file> 
    <Staff> 
     <connection>wifi</connection> 
     <timestamp>20</timestamp> 
     <sport>0</sport> 
    </Staff> 
    <Staff> 
     <connection>3g</connection> 
     <timestamp>40</timestamp> 
     <sport>0</sport> 
    </Staff> 
    <Staff> 
     <connection>wifi</connection> 
     <timestamp>60</timestamp> 
     <sport>0</sport>   
    </Staff> 
    <Staff> 
     <connection>3g</connection> 
     <timestamp>80</timestamp> 
     <sport>0</sport>   
    </Staff> 
</file> 

위한 것이며 내가 구문 분석이

try { 
        InputStream filename = null; 
       Document obj_doc = null; 
       DocumentBuilderFactory doc_build_fact = null; 
       DocumentBuilder doc_builder = null; 
       filename = new FileInputStream("/sdcard/data.xml"); 
       doc_build_fact = DocumentBuilderFactory.newInstance(); 
       doc_builder = doc_build_fact.newDocumentBuilder(); 
       System.out.println("readed data.xml"); 
       obj_doc = doc_builder.parse(filename); 

       NodeList obj_nod_list = null; 
       if (null != obj_doc) { 
        org.w3c.dom.Element feed = obj_doc.getDocumentElement(); 
        obj_nod_list = feed.getElementsByTagName("file"); 
       } 

       Element root = obj_doc.getDocumentElement(); 
       NodeList items = root.getElementsByTagName("Staff"); 
       System.out.println("items "+items.getLength()); 



       for (int i = 0; i < items.getLength(); i++) { 
        Node item = items.item(i); 
        NodeList properties = item.getChildNodes(); 
        System.out.println("properties length "+item.getChildNodes().getLength()); 
//     System.out.println("properties "+properties.getLength()); 
        for (int j = 0; j < items.getLength(); j++) { 
         Node property = properties.item(j); 
         // System.out.println("properties "+property.getNodeName()); 
         String name = property.getNodeName(); 

         if (name.equalsIgnoreCase("connection")) { 
          // Store it where you want 
           connection.add(property.getFirstChild().getNodeValue()); 
          System.out.println("connection "+connection); 
//       System.out.println("connection "+connection.get(i)); 
         } 
          if (name.equalsIgnoreCase("timestamp")) { 
           int inttimestamp = Integer.parseInt(property.getFirstChild().getNodeValue()); 
           timestamp.add(inttimestamp); 

          System.out.println("timestamp "+timestamp); 
         } 
          if (name.equalsIgnoreCase("sport")) { 
           int inttimestamp = Integer.parseInt(property.getFirstChild().getNodeValue()); 
           capacity.add(inttimestamp); 

          System.out.println("capacity "+capacity); 
         } 


        } 
       } 

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

을 수행 할 때이 파서 코드입니다! 그것은 마지막 아이를 읽을 수 없다! 그것은 스포츠 다! 당신은 나에게 그것을위한 해결책을 제공해 줄 수 있겠습니까! 또한 내가 XML을 변경하고 마지막 노드를 읽을 수 없다면 "스포츠"를 넣어! 감사합니다

+0

변수 –

답변

0

jsoup html 파서를 사용하십시오. Here is the official site. 당신은 HTML, XML 등을 구문 분석하는 데 사용할 수 있습니다. 이것은 태그 기반 파서입니다.

public class XMLParser extends DefaultHandler 
{ 
    // xml Tags name 
    private final String TENDER_TYPE = "TenderType"; 
    private final String TRX_TYPE = "TransactionType"; 
    private final String DATA_ELEM = "DataElement"; 
    //xml Values 
    private final String NAME  = "Name"; 
    private final String VALUE  = "Value"; 
    private final String AMOUNT  = "Amount"; 
    private final String T_TYPE  = "TenderType"; 
    private final String CLRK_ID  = "ClerkId"; 
    private final String INV_NUM  = "InvoiceNum"; 
    private final String AUTH  = "Authorization"; 
    private final String ORIG_SEQ = "OriginalSequence"; 
    private final String ORIG_REF = "OriginalReference"; 
    private final String TAG   = "Tag"; 
    private final String DESCRIPTION = "Description"; 


    //list for imported Config data 
    private ArrayList<TenderType> theTenderTypeList = null; 
    private TenderType currentTenderType    = null; 

    private ArrayList<TransactionType> theTrxTypeList = null; 
    private TransactionType currentTrxType   = null; 

    private ArrayList<DataElement> theDataElementList = null; 
    private DataElement currentDataElement   = null; 


    @Override 
    public void startDocument() throws SAXException 
    { 
     super.startDocument(); 
     if(theTenderTypeList == null) 
     { 
      theTenderTypeList = new ArrayList<TenderType>(); 
     } 
     if(theTrxTypeList == null) 
     { 
      theTrxTypeList = new ArrayList<TransactionType>(); 
     } 
     if(theDataElementList == null) 
     { 
      theDataElementList = new ArrayList<DataElement>(); 
     } 
    } 

    @Override 
    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException 
    { 
     if (localName.equalsIgnoreCase(TRX_TYPE)) 
     {   
      this.currentTrxType = new TransactionType(attributes.getValue(NAME), 
        attributes.getValue(VALUE),attributes.getValue(AMOUNT), 
        attributes.getValue(T_TYPE),attributes.getValue(CLRK_ID), 
        attributes.getValue(INV_NUM), attributes.getValue(AUTH), 
        attributes.getValue(ORIG_SEQ), attributes.getValue(ORIG_REF)); 

      this.theTrxTypeList.add(currentTrxType); 
     } 
     else if (localName.equalsIgnoreCase(TENDER_TYPE)) 
     {   
      this.currentTenderType = new TenderType(attributes.getValue(NAME), 
                attributes.getValue(VALUE)); 
      theTenderTypeList.add(currentTenderType); 
     } 
     else if (localName.equalsIgnoreCase(DATA_ELEM)) 
     {   
      this.currentDataElement = new DataElement(attributes.getValue(TAG), 
                attributes.getValue(DESCRIPTION)); 
      theDataElementList.add(currentDataElement); 
     } 

    } 

    public ArrayList<TenderType> getTenderTypeList() 
    { 
     return theTenderTypeList; 
    } 

    public ArrayList<TransactionType> getTrxTypeList() 
    { 
     return theTrxTypeList; 
    } 

    public ArrayList<DataElement> getDataElementList() 
    { 
     return theDataElementList; 
    } 

} 

을 다음 유를 호출 할 수 있습니다 :

SAXParserFactory fabrique = SAXParserFactory.newInstance(); 
     SAXParser parseur = null; 

     try 
     { 
      parseur = fabrique.newSAXParser(); 
     } 
     catch (ParserConfigurationException e) 
     { 
      System.out.println("Parse Config Exception"); 
     } 
     catch (SAXException e) 
     { 
      System.out.println("SAX Parse Exception"); 
     } 

     handler = new XMLParser(); 

     try 
     { 
      /* Parse Config File */ 
      myFileCfg = new File(FolderPath + CFG_FILE_NAME); 
      parseur.parse(myFileCfg, handler); 

     } 
     catch (SAXException e) 
+0

완전한 자바 파일을 넣어 여기에 그 코드가 잘못되었다는 것을 말해 줄 수 있습니까? – Cbour

0

여기 당신에게

파서 클래스를 도울 수있는 SAX 파서를 사용하는 예제입니다! XML 파일 구조가 좋지 않았습니다! 내가 그것을 바꾼 후에! 그것은 효과가 있었다. 귀하의 awnsers을 주셔서 감사합니다

+0

안드로이드에 넣을 수 있습니까? – Cbour

+0

예, 안드로이드 프로젝트에서 가져 왔습니다. –

0

내가 그것을 해결

관련 문제