gpt4 book ai didi

java - Java如何通过迭代器迭代XML文件?

转载 作者:行者123 更新时间:2023-11-30 05:59:21 26 4
gpt4 key购买 nike

我有 XML 文件的内容。我需要通过 Java 中的 Iterator 迭代 XML 文件。我不想通过循环或其他方式进行迭代。仅通过迭代器。你能告诉我如何通过迭代器迭代 XML 文件吗?我可以读取 XML 文件(例如文本文件)还是必须通过 DOM 解析文件?

XML 文件

 <?xml version="1.0" encoding="utf-8"?>
<Rules>

<Rule id="damaged">
<Question>Do you want to accidents car?</Question>
<Answer>
<Selection value="true">
<SingleValue value="yes"/>
</Selection>
<Selection value="false">
<SingleValue value="no"/>
</Selection>
</Answer>
</Rule>

<Rule id="family">
<Question>Do you want to family car?</Question>
<Answer>
<Selection value="true">
<MultipleValue value="yes"/>
</Selection>
<Selection value="false">
<MultipleValue value="no"/>
</Selection>
</Answer>
</Rule>

<Rule id="money">
<Question>Do you have 200.000 PLN for a car?</Question>
<Answer>
<Selection value="true">
<SingleValue value="yes"/>
</Selection>
<Selection value="false">
<SingleValue value="no"/>
</Selection>
</Answer>
</Rule>

<Rule id="drivingGear">
<Question>Do you have front-wheel drive?</Question>
<Answer>
<Selection value="true">
<MultipleValue value="on the front wheels"/>
</Selection>
<Selection value="false">
<MultipleValue value="on the rear wheels"/>
</Selection>
</Answer>
</Rule>

<Rule id="gearBox">
<Question>Do you want to automatic gearbox?</Question>
<Answer>
<Selection value="true">
<SingleValue value="automatic"/>
</Selection>
<Selection value="false">
<SingleValue value="manual"/>
</Selection>
</Answer>
</Rule>


<Rule id="comfort">
<Question>What is more important for you?</Question>
<Answer>
<Selection value="true">
<SingleValue value="comfort"/>
</Selection>
<Selection value="false">
<SingleValue value="speed"/>
</Selection>
</Answer>
</Rule>
...
<Rule id="luxury">
<Question>What feature is must-have for you?</Question>
<Answer>
<Selection value="true">
<MultipleValue value="gps,bluetooth,dvd,automatic transmission,self-driving"/>
</Selection>
<Selection value="false">
<MultipleValue value="nothing,abs,fog lights,central lock"/>
</Selection>
</Answer>
</Rule>

这是我写的代码..

public class Question{

private List<String> splittedXML;

public Question(){
this.splittedXML = new ArrayList<>();
}

public List<String> getSplittedXML(){
return this.splittedXML;
}

public Iterator<String> QuestionIterator(){
try (BufferedReader br = new BufferedReader(new FileReader("/home/hubert/Pulpit/expert-system-hubert/Rules.xml"))) {

String line;

while ((line = br.readLine()) != null) {
String[] splittedTextXMLFile = line.split("\n");


for(String singleLine: splittedTextXMLFile){
if(singleLine.equals("Question")){
splittedXML.add(singleLine.substring(singleLine.indexOf(">") + 1, singleLine.indexOf("<")));
}
}

}
Iterator<String> itr = splittedXML.iterator();

while(itr.hasNext()){
return itr;
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

最佳答案

您永远不应该尝试通过将 XML 读取为文本来处理 XML,您应该始终使用 XML 解析器。否则,您将依赖于 XML 的意外功能,这些功能将来很容易更改,例如,如果发送者决定通过关闭缩进来提高性能。

您应该通过将其解析为内存中的树结构来处理该文件:要么是旧的、笨拙的 DOM,要么是更现代的替代方案之一,例如 JDOM2 或 XOM。

关于java - Java如何通过迭代器迭代XML文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52540203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com