Java 从xml获取空白值

Java 从xml获取空白值,java,xml,stax,Java,Xml,Stax,我正在解析一个xml文件以获得一些值,所有的东西看起来都很好。当我显示从xml获得的值时,它们是空白的 public static void main(String[] args) throws Throwable { SpringApplication.run(SwiftApplication.class, args); XMLInputFactory factory = XMLInputFactory.newInstance(); //On utilis

我正在解析一个xml文件以获得一些值,所有的东西看起来都很好。当我显示从xml获得的值时,它们是空白的

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));



   }

   }
publicstaticvoidmain(String[]args)抛出Throwable{
run(switapplication.class,args);
XMLInputFactory=XMLInputFactory.newInstance();
//关于使用notre-fichier测试
File File=新文件(“/home/dhafer/Downloads/Connector's Files/File.xml”);
//我们是阿隆·斯托克先生,我们是费耶尔先生和阿雷特·拉先生
ArrayList listFruit=新的ArrayList();
试一试{
//诺特尔阅读器
XMLStreamReader=factory.createXMLStreamReader(新文件读取器(文件));
while(reader.hasNext()){
//莱维尼门特杯
int type=reader.next();
开关(类型){
案例XMLStreamReader.START_元素:
System.out.println(“Nous sommes sur unéelement”+reader.getLocalName());
//这不是应答器,而是应答器的一部分
if(reader.getLocalName().equals(“OptnTp”)){
//在suivant的récupère lévenement上,您可以在应答器的内容中找到答案
//斯托克·丹·拉收藏酒店
reader.next();
add(reader.getCharacterEncodingScheme());
System.out.println(“\t->Fruit récupérée!”);
}
打破
}
//我是埃文斯·德克斯·费伊尔斯,在《停止洛杉矶讲座》上
如果(listFruit.size()>2){
System.out.println(“\t-->Nombre de fruit>1=>fin de boucle!”);
打破
}
}
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(XMLStreamException e){
e、 printStackTrace();
}
System.out.println(listFruit.get(0));
}
}
这是我正在使用的xml文件

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

004
NOAC
距离
欧元
真的
我错过了什么? 有什么想法吗?
谢谢

我能够毫无问题地运行您的代码,我看到所有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文件。有什么想法吗?我得到这个输出:[[C@7852e922, [C@7852e922, [C@7852e922, [C@7852e922]我的问题是,当我试图显示必须包含NOAC的数组时,请尝试显示它。我看不到在您的代码中有任何地方将NOAC添加到数组中。如果您的代码更新后试图执行其他操作&不起作用,请更新您的帖子。
<?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