2017-04-17 12 views
-1

I'am 일부 값을 얻기 위해 XML 파일을 구문 분석, 모든 것이 여기 they'are 빈얻기 빈 값이

public static void main(String[] args) throws Throwable { 
    SpringApplication.run(SwiftApplication.class, args); 

    XMLInputFactory factory = XMLInputFactory.newInstance(); 



     //On utilise notre fichier de test 

     File file = new File("/home/dhafer/Downloads/Connector's Files/file.xml"); 



     //Nous allons stocker deux feuilles et arrêter la lecture 

     ArrayList<String> listFruit = new ArrayList<>(); 



     try { 

     //Obtention de notre reader 

     XMLStreamReader reader = factory.createXMLStreamReader(new FileReader(file)); 

     while (reader.hasNext()) { 

      // Récupération de l'événement 

      int type = reader.next(); 





      switch (type) { 

       case XMLStreamReader.START_ELEMENT: 

        System.out.println("Nous sommes sur un élement " + reader.getLocalName()); 

        // Si c'est un début de balise, on vérifie qu'il s'agit d'une balise feuille 

        if(reader.getLocalName().equals("OptnTp")){ 



         //On récupère l'évenement suivant, qui sera le contenu de la balise 

         //et on stocke dans la collection 

         reader.next(); 

         listFruit.add(reader.getCharacterEncodingScheme()); 

         System.out.println("\t -> Fruit récupérée ! "); 

        } 



        break; 

      } 



      //Si nous avons deux feuilles, on stop la lecture 

      if(listFruit.size() > 2){ 

       System.out.println("\t --> Nombre de fruit > 1 => fin de boucle !"); 

       break; 

      } 



     } 

     } catch (FileNotFoundException e) { 

     e.printStackTrace(); 

     } catch (XMLStreamException e) { 

     e.printStackTrace(); 

     } 




      System.out.println(listFruit.get(0)); 



    } 

    } 

잘 exept 보인다 내가 사용하고있는 xml 파일입니다.

<CorpActnOptnDtls> 
    <OptnNb>004</OptnNb> 
    <OptnTp> 
     <Cd>NOAC</Cd> 
    </OptnTp> 
    <FrctnDspstn> 
     <Cd>DIST</Cd> 
    </FrctnDspstn> 
    <CcyOptn>EUR</CcyOptn> 
    <DfltPrcgOrStgInstr> 
     <DfltOptnInd>true</DfltOptnInd> 
    </DfltPrcgOrStgInstr> 
    </CorpActnOptnDtls> 

나는 무엇이 실종 되었습니까? 아이디어가 있습니까? 감사합니다.

+0

질문에 샘플 XML 파일을 추가하십시오 – Ishnark

+0

어떤 아이디어? 나는이 출력을 얻는다. [[C @ 7852e922, [C @ 7852e922, [C @ 7852e922] [C @ 7852e922] –

답변

0

아무런 문제없이 코드를 실행할 수 있었지만 XML의 모든 태그가 콘솔에 인쇄되어 있습니다.

 public static void main(String args[]){ 
    XMLInputFactory factory = XMLInputFactory.newInstance(); 
     // I made only below path change to read the xml file from my windows machine 
    File file = new File("C:/LocalWorkSpace/Test/src/Test1.xml"); 
    ArrayList<String> listFruit = new ArrayList<>(); 
     try { 
     XMLStreamReader reader = factory.createXMLStreamReader(new FileReader(file)); 
     while (reader.hasNext()) { 
      int type = reader.next(); 
      switch (type) { 
       case XMLStreamReader.START_ELEMENT: 
        System.out.println("We are on an element " + reader.getLocalName()); 


        if(reader.getLocalName().equals("OptnTp")){ 
         reader.next(); 
         listFruit.add(reader.getCharacterEncodingScheme()); 
         System.out.println("\t -> Fruit recovered! "); 
        } 
        break; 
      } 
      if(listFruit.size() > 2){ 
       System.out.println("\t --> Number of fruit> 1 => end of loop!"); 
       break; 
      } 
     } 

     } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     } catch (XMLStreamException e) { 
     e.printStackTrace(); 
     } 
      System.out.println(listFruit.get(0)); 

    } 

입력 XML

<?xml version="1.0" encoding="UTF-8"?> 
<CorpActnOptnDtls> 
    <OptnNb>004</OptnNb> 
    <OptnTp> 
     <Cd>NOAC</Cd> 
    </OptnTp> 
    <FrctnDspstn> 
     <Cd>DIST</Cd> 
    </FrctnDspstn> 
    <CcyOptn>EUR</CcyOptn> 
    <DfltPrcgOrStgInstr> 
     <DfltOptnInd>true</DfltOptnInd> 
    </DfltPrcgOrStgInstr> 
    </CorpActnOptnDtls> 

콘솔 출력

We are on an element CorpActnOptnDtls 
We are on an element OptnNb 
We are on an element OptnTp 
    -> Fruit recovered! 
We are on an element Cd 
We are on an element FrctnDspstn 
We are on an element Cd 
We are on an element CcyOptn 
We are on an element DfltPrcgOrStgInstr 
We are on an element DfltOptnInd 
UTF-8 
+0

내 문제는 내가 NOAC를 포함해야하는 배열을 표시하려고 할 때 표시하려고 시도한다. –

+0

글쎄 어레이에 NOAC를 추가하는 코드는 어디에도 없습니다. 코드가 다르고 작동하지 않는 것으로 업데이트 된 경우 게시물을 업데이트하십시오. – ssanrao