Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Reading XML File using DOMParser and have problem with accessing xml

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    My Mood
    Goofy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Reading XML File using DOMParser and have problem with accessing xml

    I am trying to create a Java application that would generate Multiple Choice Question. The multiple choice questions are stored in the XML file and validated with DTD or schema. The proposed idea is to create Java app with model view controller design pattern and accessing the XML file. But I am still in an initial phase and basically encountered problem here. I have managed to access almost all element of XML file but could access particular element. I hope you guys could help me out.

    Well my XML File is as following,

    <?xml version="1.0"?>
    <!DOCTYPE question-group SYSTEM "../dtds/something.dtd">
    <question-group>
    <question-label>something</question-label>
    <title>something</title>

    <mcqs>
    <question>something</question>
    <answers>
    <option>
    <label>something</label>
    <statement>something</statement>
    </option>
    <option>
    <label>something</label>
    <statement>something</statement>
    </option>
    <option>
    <label>something</label>
    <statement>something</statement>
    </option>
    <option>
    <label>something</label>
    <statement>something</statement>
    </option>
    </answers>
    <actual-answers>
    <answer-label>something</answer-label>
    <answer-label>something</answer-label>
    </actual-answers>
    </mcqs>

    </question-group>

    My question is "How to access the element is <answer-label tag>?" My Java file is as following:



    public class ReadAndDisplay {

    public static void main(String args[]) {

    try {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new File("xml/something.xml"));

    //To normalise text representation
    doc.getDocumentElement().normalize();


    //---------- To read mcqs
    NodeList multipleQuestion;
    multipleQuestion = doc.getElementsByTagName("mcqs");
    int totalQuestions = multipleQuestion.getLength();
    System.out.println("Total number of questions : " + totalQuestions);

    for(int n=0; n<totalQuestions; n++) {

    Node Question = multipleQuestion.item(n);
    if((Question.getNodeType()==Node.ELEMENT_NODE)) {
    Element questionElement = (Element)Question;

    //-------
    NodeList questionList = questionElement.getElementsByTagName("question");
    Element qElement = (Element)questionList.item(0);

    NodeList textQuestionList = qElement.getChildNodes();
    System.out.println("Question : " + ((Node)textQuestionList.item(0)).getNodeValue().tr im());

    //-------
    NodeList multipleAnswerList = doc.getElementsByTagName("option");
    //int totalAnswerList = multipleAnswerList.getLength();
    //System.out.println("Total probable answers: " + totalAnswerList);

    for(int t=0; t<totalAnswerList.getLength();t++) {


    Node answerNode = multipleAnswerList.item(t);
    if(answerNode.getNodeType()==Node.ELEMENT_NODE) {

    Element answerElement = (Element)answerNode;
    //-------
    NodeList labelList = answerElement.getElementsByTagName("label");
    Element labelElement = (Element)labelList.item(0);

    NodeList textLabelList = labelElement.getChildNodes();
    System.out.println("Label : " + ((Node)textLabelList.item(0)).getNodeValue().trim( ));

    //-------
    NodeList statementList = answerElement.getElementsByTagName("statement");
    Element statementElement = (Element)statementList.item(0);

    NodeList textStatementList = statementElement.getChildNodes();
    System.out.println("Statement : " + ((Node)textStatementList.item(0)).getNodeValue().t rim());

    } //end of if clause
    } //end of for loop with t var

    //--

    NodeList actualAnswerList = doc.getElementsByTagName("actual-answers");
    int totalActualAnswerList = actualAnswerList.getLength();
    System.out.println("Total answers: " + totalActualAnswerList);

    for(int a=0; a<actualAnswerList.getLength();a++) {

    Node actualAnswerNode = actualAnswerList.item(a);
    if(actualAnswerNode.getNodeType()==Node.ELEMENT_NO DE) {

    Element actualAnswerElement = (Element)actualAnswerNode;
    //------
    NodeList answerLabelList = actualAnswerElement.getElementsByTagName("answer-label");
    Element answerLabelElement = (Element)answerLabelList.item(0);

    NodeList textAnswerLabelList = answerLabelElement.getChildNodes();
    System.out.println("Actual Answer Label : " + ((Node)textAnswerLabelList.item(0)).getNodeValue() .trim());
    } //end of if clause
    } //end of for loop with a var

    } //end of if clause
    } //end of for loop with n var

    }

    Currently, there are lots of flaws in the coding as it prints many number of times than it intends to. Could you please suggest? Am I in the right path to create Java application with relation to XML? Your suggestions and tips will be mostly welcomed. Cheers


  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    My Mood
    Goofy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading XML File using DOMParser and have problem with accessing xml

    Can anybody please help me out? I am just stuck with this.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading XML File using DOMParser and have problem with accessing xml

    if you want the value of answer-label you might want to try this:

    File xmlFile = new File("C:\\xmlFile.xml");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(xmlFile);
    Element documentElement = document.getDocumentElement();
    NodeList answers = documentElement.getElementsByTagName("answer-label");
    int length = answers.getLength();
    for (int i = 0; i < length; i++) {
    Node item = answers.item(i);
    System.out.println("ITEM:"+item.getTextContent());
    }
    it works

Similar Threads

  1. [SOLVED] Problem in File Reading...
    By Mr.777 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 20th, 2011, 07:24 AM
  2. Problem reading correctly from a file
    By stavrakas in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 6th, 2011, 08:09 AM
  3. Problem in reading image file
    By ramhanuman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2011, 02:46 PM
  4. Accessing Properties File
    By java_mein in forum Java Servlet
    Replies: 5
    Last Post: May 14th, 2010, 02:44 AM
  5. Problem on reading file in Java program
    By nathanernest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 13th, 2009, 08:14 AM