This is an XML document (the sentence and whitespace prior to the XML declaration and XSLT processing instruction are part of the input):
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3.0/style/exchange.xsl"?>
<mts:meta name="elapsed-time" value="18" />
<exchange-documents>
<exchange-document country="US" number="8049504">
....
....
....
</exchange-document>
</exchange-documents>
I am parsing the XML and using XPath. In most of the XML files, the first line contains some text or spaces (refer the above xml)
Without that leading text, it parses successfully, but if any text appears it produces the below error:
--- exec-maven-plugin:1.2.1:exec (default-cli) @ XMLHandling ---
[Fatal Error] :1:1: Content is not allowed in prolog.
How can I get around this?
The code that I am using:
public static void main(String[] args) throws ParseException {
String filePath = "D:/newxml.xml";
try {
FileInputStream file = new FileInputStream(new File(filePath));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
String pubOrPatentNumber = xPath.compile("//preference").evaluate(xmlDocument);
...
...
}
}
I can manually remove the text and execute, but I need to solve this within my code to clean up the input automatically.