0

I am working on a project where I need to read in an XML file and use the information in the file to analyze user input. More specifically the file looks like this.

<course>
<category  name="Course Name">
    <category name="Exams">
        <item name="Midterm" outof="110.0" weight="17.0"></item>
        <item name="Final Exam" outof="110.0" weight="18.0"></item>
    </category>

    <category name="Individual Assignments">
            <item name="Swing GUI Conference Program" outof="50.0" weight="7.5">     </item>
            <item name="Assignment 2" outof="50.0" weight="7.5">     </item>
            <item name="Assignment 3" outof="50.0" weight="7.5">     </item>
            <item name="Assignment 4" outof="50.0" weight="7.5">     </item>

    </category>

    <category name="Group Projects">
        <item name="Lab Selector Project" outof="75.0" weight="15"></item>
        <item name="Android Project" outof="75.0" weight="15"></item>
    </category>

    <category name="Homework, Quizzes, Participation">
        <item name="01 - GUI Using Dialog Boxes" outof="20.0" weight="2.5"></item>
        <item name="02 - GUID Using Menu Files" outof="20.0" weight="2.5"></item>
    </category> 

</category>

What I need to be able to do is pull this information so that I can use it in a grade calcualtor application.

Edit: Several of you have mentions SAX/DOM editor and I've searched and tried both of those methods but when I try to implement them it comes back will blank strings. I think it has to do with all the examples I find looking like this:

    <?xml version="1.0" encoding="UTF-8"?>
<school>
 <student id="1">
  <firstname>ankush</firstname>
  <lastname>thakur</lastname>
  <email>beingjavaguy.gmail.com</email>
  <phone>7678767656</phone>
 </student>
 <student id="2">
  <firstname>aarti</firstname>
  <lastname>gupta</lastname>
  <email>aartigupta.gmail.com</email>
  <phone>9876546545</phone>
 </student>
</school>

Whereas my xml file has all the information stored in the opening tag and not stored in between the open and close tags.

4 Answers4

1

If you have XSD for this XML (or can write), you can use JAXB for avoid writing parsing code , and get XML data as Java POJO with "unmarshal".

pasha701
  • 6,831
  • 1
  • 15
  • 22
0

You can use a XML parser like sax or DOM to parse the xml to your java program and then can work on the implementation the calc.

how to use sax and DOM

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50
0

I would recommend checking out the SAX parser. http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/. If you have a more specific question I'm sure we could help you better

mattdee123
  • 213
  • 1
  • 5
  • 11
0

Parsing the XML with parsers like SAX or DOM or StAX are helpful to fetch the information you want. See this link for basic example : Parsing xml using DOM, SAX and StAX

OneSix
  • 71
  • 3