I am trying to create a program called parseXml for parsing an XML file, which i
ID: 3551210 • Letter: I
Question
I am trying to create a program called parseXml for parsing an XML file, which is a file containing XML tags and text. The tags are used to mark up the structure of the data represented in the file. Here is the algorithm that I have put together but I am having trouble trying to create just the empty tag name stack, which is the first line. What would you suggest I do for starters?
create an empty tag name stack
set indent level to 0
for each token { if (the token is an opening tag) { print indenting according to indent level print token push tag name onto the tag name stack increase indent level
Explanation / Answer
package com.mkyong.seo; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File; public class ReadXMLFile { public static void main(String argv[]) { try { File fXmlFile = new File("/Users/mkyong/staff.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); //optional, but recommended //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("staff"); System.out.println("----------------------------"); for (int temp = 0; tempRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.