Reputation: 119
I am working on how to read xml file in java.Below is my xml code
<path>
<Excelpath>
C:/Documents and Settings/saalam/Desktop/Excel sheets/New Microsoft Excel Worksheet (3).xls
</Excelpath>
</path>
Using this, I wrote java code to read this xml, below is my java code
try {
//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
//parse using builder to get DOM representation of the XML file
Document doc = db.parse(fXmlFile);
//read the xml node element
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
System.out.println("expath is:"+doc.getElementsByTagName("Excelpath"));
} catch(ParserConfigurationException pce) {
pce.printStackTrace();
} catch(SAXException se) {
se.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
here what i want to achieve is,i need to read the xml from xml i need to get the excelpath which i provided.later on i need to use this excelpath and use this to get the excelsheet values for my further code. when i tried to run the following code,it didnt give me option to run it as java application rather it displayed "run configuration". is this correct that its appearing as "run configuration" and not as "run as java application".
Upvotes: 0
Views: 723
Reputation: 1874
From your question, I assume that you are using eclipse IDE for your development. Eclipse IDE basically will show two different options "Run Configurations" and Run as "Java Application".
The option "Java Application" will be shown when you are attempting to execute a class which has "main" method and it will run with default JVM arguments where as the other option "Run Configurations" will always be shown and in this case, you have specify your main class, the JVM arguments and other arguments (if any) on which your program depends on.
If your code doesn't have main method, kindly add one in your main class.
The API getElementsByTagName(<<Tag Name>>)
will return the list of all nodes with the matching tag name. You have to iterate the node list and get the text content as below.
NodeList nodeList = doc.getElementsByTagName("Excelpath");
for (int index = 0; index < nodeList.getLength(); index++) {
System.out.println(nodeList.item(index).getTextContent());
}
Read the documentation here - https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Document.html#getElementsByTagName%28java.lang.String%29
Upvotes: 1