I am parsing xml file in java. I am getting SEVERE: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog. exception.
-
1Well it sounds like your XML file is bogus or you've got a bug in how you're using the XML parser. Please show us the file (or at least a minimal file that demonstrates the problem) and your code.Jon Skeet– Jon Skeet2014-01-20 09:04:28 +00:00Commented Jan 20, 2014 at 9:04
-
Try this: stackoverflow.com/questions/4569123/…Bart Kiers– Bart Kiers2014-01-20 09:05:23 +00:00Commented Jan 20, 2014 at 9:05
-
Please add the stack trace of your error and provide the code where the error is occurring. There is not nearly enough information here to work with.Kent Hawkings– Kent Hawkings2014-01-20 09:05:29 +00:00Commented Jan 20, 2014 at 9:05
-
Sounds like there is something wrong with the prolog in the xml file. If you need help, maybe you should post the prolog of the xml file? And maybe also the java code where you invoke the parse() method.Alderath– Alderath2014-01-20 09:07:45 +00:00Commented Jan 20, 2014 at 9:07
Add a comment
|
1 Answer
Here is xml file Stocks.xml which contains some stocks and there price, quantity we will use this in our xml parsing example in Java.
<?xml version="1.0" encoding="UTF-8"?>
<stocks>
<stock>
<symbol>Citibank</symbol>
<price>100</price>
<quantity>1000</quantity>
</stock>
<stock>
<symbol>Axis bank</symbol>
<price>90</price>
<quantity>2000</quantity>
</stock>
</stocks>
Here is a code example of parsing above xml file in Java using DOM parser:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DOMExampleJava {
public static void main(String args[]) {
try {
File stocks = new File("Stocks.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(stocks);
doc.getDocumentElement().normalize();
System.out.println("root of xml file" + doc.getDocumentElement().getNodeName());
NodeList nodes = doc.getElementsByTagName("stock");
System.out.println("==========================");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("Stock Symbol: " + getValue("symbol", element));
System.out.println("Stock Price: " + getValue("price", element));
System.out.println("Stock Quantity: " + getValue("quantity", element));
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
}
Output:
root of xml file stocks
==========================
Stock Symbol: Citibank
Stock Price: 100
Stock Quantity: 1000
Stock Symbol: Axis bank
Stock Price: 90
Stock Quantity: 2000
This is the Procedure of parsing an Xml using Java