Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Read XML file java

843806Feb 27 2009 — edited Feb 27 2009
Hi all i wonder if someone could give me a hand. I've got some code (below) which reads in an xml file and spits out the contents of the file. Thats fine and dandy, however what i want to be able to do is specify which element to spit out, which i'm not sure how to do. So say i had the following xml file:
<?xml version="1.0"?>
<OpenSourceQuestions>
	<question>
		<setup>A raster graphics editor</setup>
		<answerChoice1>The Gimp</answerChoice1>
		<answerChoice2>The Chimp</answerChoice2>
		<answerChoice3>The Pimp</answerChoice3>
		<answerChoice4>The Blimp</answerChoice4>
		<correctAnswer>The Gimp</correctAnswer> 
	</question>
	<question>
		<setup>test question 2</setup>
		<answerChoice1>question2 answer1</answerChoice1>
		<answerChoice2>question2 answer2</answerChoice2>
		<answerChoice3>question2 answer3</answerChoice3>
		<answerChoice4>question2 answer4</answerChoice4>
		<correctAnswer>question2 answer1</correctAnswer> 
	</question>
</OpenSourceQuestions>
What i want to do is only spit out the xml for the first question, i.e. "A raster graphics editor" and its answers

Here is my java code for reading in and arsing the file:
	void readXML(){
		  try {
			  File fXmlFile = new File("MyXMLFile.xml");
			  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			  Document doc = dBuilder.parse(fXmlFile);
			  doc.getDocumentElement().normalize();

			  System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
			  NodeList nList = doc.getElementsByTagName("question");
			  System.out.println("-----------------------");

			  for (int temp = 0; temp < nList.getLength(); temp++) {

			    Node nNode = nList.item(temp);

			    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

			      Element eElement = (Element) nNode;

			      System.out.println("Question : "  + getTagValue("setup",eElement));
			      System.out.println("Answer1 : "  + getTagValue("answerChoice1",eElement));
			      System.out.println("Answer2 : "  + getTagValue("answerChoice2",eElement));
			      System.out.println("Answer3 : "  + getTagValue("answerChoice3",eElement));
			      System.out.println("Answer4 : "  + getTagValue("answerChoice4",eElement));


			    }
		    }
		  } catch (Exception e) {
		    e.printStackTrace();
		  }

		 }

		 private static String getTagValue(String sTag, Element eElement)
		 {
			  NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
			  Node nValue = (Node) nlList.item(0);

			  return nValue.getNodeValue();
		 }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 27 2009
Added on Feb 27 2009
4 comments
113 views