Hubert95
Hubert95

Reputation: 31

How iterate in XML file by iterator Java?

I have this content of XML file. I need to iterate in XML file by Iterator in Java. I don't want to iterate by loop or something other. Only by Iterator. Can you show me how iterate XML file by iterator ? Can I read XML file such as text file or I have to parse file by DOM ?

XML file

 <?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>

And here is code writed by me..

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

Upvotes: 1

Views: 1005

Answers (2)

Michael Kay
Michael Kay

Reputation: 163448

You should never attempt to process XML by reading it as text, you should always use an XML parser. Otherwise you are relying on accidental features of the XML that could easily change in future, for example if the sender decides to improve performance by switching off indentation.

You should process this file by parsing it into an in-memory tree structure: either the old and clumsy DOM, or one of the more modern alternatives such as JDOM2 or XOM.

Upvotes: 1

npinti
npinti

Reputation: 52185

There are some issues with your code, mainly this:

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

the br.readLine(), will give you one line of text. Whatever the result, splitting it by the new line denominator: \n will not make any difference, thus, the splittedTextXMLfile will contain only 1 element, that being line.

As a result, this: if(singleLine.equals("Question")){ will fail, since at the very least you should be looking for <Question> -> singleLine.ToLower().contains("<question>").

That being said, you might want to look at this tutorial to get you started in the right direction.

As for your last point, an iterator is usually used with a while loop, and loops are eventually turned into an iterator implementation.

Upvotes: 0

Related Questions