【问题标题】:Getting blank values from xml从xml获取空白值
【发布时间】:2017-04-17 14:43:00
【问题描述】:

我正在解析一个 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));



   }

   }

这是我正在使用的 xml 文件。

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

我错过了什么? 有任何想法吗? 谢谢

【问题讨论】:

  • 请在您的问题中添加一个示例 xml 文件
  • 有什么想法吗?我得到这个输出:[[C@7852e922,[C@7852e922,[C@7852e922,[C@7852e922]

标签: java xml stax


【解决方案1】:

我能够毫无问题地运行您的代码,我看到 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

【讨论】:

  • 我的问题是当我尝试显示必须包含 NOAC 的数组时,请尝试显示它
  • 好吧,我没有在您的代码中看到将 NOAC 添加到数组的任何地方。如果您的代码已更新,试图做一些不同的事情但无法正常工作,请更新您的帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-27
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多