Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to parse an XML in Java?

Can you please help me in writing the code to parse the following XML file and assign it to hash in Java:

<Report>
  <Date>Wednesday, July 17, 2013  11:41:27</Date>
  <File>
    <name>someFilemname1.cpp</name>
    <FileReport>
      <Check>
        <name>check1</name>
        <CheckStatus>PASSED</CheckStatus>
        <Location>
          <ExistingLineNumbers>5</ExistingLineNumbers>
          <NewLineNumbers>10</NewLineNumbers>
        </Location>
        <Message>Something happened</Message>
      </Check>
      <Check>
        <name>check2</name>
        <CheckStatus>PASSED</CheckStatus>
        <Location>
          <ExistingLineNumbers>30,34,38</ExistingLineNumbers>
          <NewLineNumbers>13,45,60</NewLineNumbers>
        </Location>
        <Message>Something not very bad happened</Message>
      </Check>
      <Check>
        <name>check3</name>
        <CheckStatus>PASSED</CheckStatus>
        <Location>
          <ExistingLineNumbers></ExistingLineNumbers>
          <NewLineNumbers>45,67,78</NewLineNumbers>
        </Location>
        <Message>Something very bad happened</Message>
      </Check>
    </FileReport>
  </File>
    <File>
    <name>someFilename2.cpp</name>
    <FileReport>
      <Check>
        <name>check4</name>
        <CheckStatus>PASSED</CheckStatus>
        <Location>
          <ExistingLineNumbers>25</ExistingLineNumbers>
          <NewLineNumbers>210</NewLineNumbers>
        </Location>
        <Message>Something just happened</Message>
      </Check>
      <Check>
        <name>check5</name>
        <CheckStatus>PASSED</CheckStatus>
        <Location>
          <ExistingLineNumbers>230,234,238</ExistingLineNumbers>
          <NewLineNumbers>213,245,260</NewLineNumbers>
        </Location>
        <Message>Something not very bad just happened</Message>
      </Check>
      <Check>
        <name>check6</name>
        <CheckStatus>PASSED</CheckStatus>
        <Location>
          <ExistingLineNumbers></ExistingLineNumbers>
          <NewLineNumbers>245,267,278</NewLineNumbers>
        </Location>
        <Message>Something very bad just happened</Message>
      </Check>
    </FileReport>
  </File>
</Report>

Open in new window

Avatar of mccarl
mccarl
Flag of Australia image

and assign it to hash
You will need to elaborate on this part quite a bit more. The data presented in the XML has quite a complex structure and so it can't just be assigned to a simple Java type, I guess you actually mean HashMap.

What part of the XML are you interested in?

What is the ultimate goal, what do you need to do with this data? This will affect how it should be represented in your program.
Avatar of Tolgar
Tolgar

ASKER

You are right. I meant to say hashmap.

I am interested in the bold printed data below. It is actually everything.

What I want to do is to know the status of each check for each  file and the line numbers that causes the issues:

For example for the first file and the first check:

File-> someFilemname1.cpp
  Check -> check1
    CheckStatus -> FAILED
    Location
       ExistingLineNumbers -> 5
       NewLineNumbers -> 10
    Message -> Something happened

Open in new window



And I need to know all of this data for all files and checks.

<Report>
  <Date>Wednesday, July 17, 2013  11:41:27</Date>
  <File>
    <name>someFilemname1.cpp</name>
    <FileReport>
      <Check>
        <name>check1</name>
        <CheckStatus>FAILED</CheckStatus>
        <Location>
          <ExistingLineNumbers>5</ExistingLineNumbers>
          <NewLineNumbers>10</NewLineNumbers>
        </Location>
        <Message>Something happened</Message>
      </Check>
      <Check>
        <name>check2</name>
        <CheckStatus>PASSED</CheckStatus>
        <Location>
          <ExistingLineNumbers>30,34,38</ExistingLineNumbers>
          <NewLineNumbers>13,45,60</NewLineNumbers>
        </Location>
        <Message>Something not very bad happened</Message>
      </Check>
      <Check>
        <name>check3</name>
        <CheckStatus>PASSED</CheckStatus>
        <Location>
          <ExistingLineNumbers></ExistingLineNumbers>
          <NewLineNumbers>45,67,78</NewLineNumbers>
        </Location>
        <Message>Something very bad happened</Message>
      </Check>
    </FileReport>
  </File>
    <File>
    <name>someFilename2.cpp</name>
    <FileReport>
      <Check>
        <name>check4</name>
        <CheckStatus>PASSED</CheckStatus>
        <Location>
          <ExistingLineNumbers>25</ExistingLineNumbers>
          <NewLineNumbers>210</NewLineNumbers>
        </Location>
        <Message>Something just happened</Message>
      </Check>
      <Check>
        <name>check5</name>
        <CheckStatus>PASSED</CheckStatus>
        <Location>
          <ExistingLineNumbers>230,234,238</ExistingLineNumbers>
          <NewLineNumbers>213,245,260</NewLineNumbers>
        </Location>
        <Message>Something not very bad just happened</Message>
      </Check>
      <Check>
        <name>check6</name>
        <CheckStatus>PASSED</CheckStatus>
        <Location>
          <ExistingLineNumbers></ExistingLineNumbers>
          <NewLineNumbers>245,267,278</NewLineNumbers>
        </Location>
        <Message>Something very bad just happened</Message>
      </Check>
    </FileReport>
  </File>
</Report>

After I get this data, I will put it in a table in a GUI in Java. The table is actually ready for some fake data. Now I need to put some actual data from the this XML file.

It is not related but this is now I created my table:

        //TAB3 CONTENT
        //This is the progress bar
        GridLayout progressBarGridLayout = new GridLayout();
        progressBarGridLayout.numColumns = 1;
        GridData progressBarGridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
        RunCheckProgressBar progressBar = new RunCheckProgressBar();
        progressBar.createProgressBar(compTab3, progressBarGridData);
        
        //This is the results table
        Table table = new Table(compTab3, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        //GridData resultsGridData = new GridData(1, 100, false, false);
        table.setLayout(progressBarGridLayout);
        table.setHeaderVisible(true);
        String[] titles = { "Override", "Rerun", "Check/Error", "Filename", "Line" };
        
        for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
            TableColumn column = new TableColumn(table, SWT.NULL);
            column.setText(titles[loopIndex]);
            column.setWidth(80);
        }
        
        for (int loopIndex = 0; loopIndex < 20; loopIndex++) {
            TableItem item = new TableItem(table, SWT.NULL);
            item.setText(0, "yes");
            item.setText(1, "0");
            item.setText(2, "Spellcheck");
            item.setText(3, "/main/lib/filename" + loopIndex + ".cpp");
            item.setText(4, "50" + loopIndex*4);
            if (loopIndex % 2 == 0) item.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
         }
          
         for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
              table.getColumn(loopIndex).pack();
         }
          
         GridData data3 = new GridData(GridData.FILL_HORIZONTAL);
         data3.verticalAlignment = SWT.BOTTOM;
         data3.horizontalSpan = 2;
         table.setLayoutData(data3);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar

ASKER

Perfect answer!
Your welcome! :)