我正在解析一个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>
我错过了什么?
有任何想法吗?
感谢
答案 0 :(得分: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