0

I'm trying to transform an XML file into a document like this:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse("C:/xml/41111208890622000144550010000000011000003066-nfe.xml"); 
Document document = db.parse(new InputSource(new StringReader("C:/xml/41111208890622000144550010000000011000003066-nfe.xml")));

but it is giving the error message:

Exception in thread "main" org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1;

someone knows what to do?

1
  • What's the content of the file? Commented Jul 30, 2013 at 15:11

1 Answer 1

4

You're currently creating a reader containing the string

"C:/xml/41111208890622000144550010000000011000003066-nfe.xml"

and asking the DocumentBuilder to parse that as if it were XML, when it's clearly not. (I'm referring to the second parse call, which I suspect is the one in your actual code. The code you've provided wouldn't compile as you've declared document twice.)

You can create a FileInputStream or perhaps an InputStreamReader wrapped around it:

String filename = "C:/xml/41111208890622000144550010000000011000003066-nfe.xml";
try (FileInputStream input = new FileInputStream(filename))
{
    Document document = db.parse(new InputSource(input));
}

(I prefer to use a stream directly, and let the parser detect the encoding.)

Now this call:

Document document = db.parse("C:/xml/...");

would nearly work and may actually work, using DocumentBuilder.parse(String) - it depends on whether parse is happy to handle a filename as a URI. (I've seen some XML APIs that are fine with that, and some that aren't.) If it doesn't work, try using the file:// scheme:

Document document = db.parse("file://C:/xml/...");
Sign up to request clarification or add additional context in comments.

6 Comments

Not quite. The DocumentBuilder#parse(String) method takes a URL (in string form) and parses the content of that location. This is in fact a safer method than the InputStream version, since the parser is given the base URI (system ID) and can use it for resolving other resources, if needed.
@forty-two: When you say "not quite" - unless there are other (relative) resources in the XML, this works fine, right? (My claim of "needing" to do this is wrong, admittedly - will edit that.)
"Not quite" referred to the implication that #parse(String) takes an XML string, which it doesn't.
@forty-two: I was referring to the second call to parse, which takes an InputSource built with a StringReader. That is trying to parse the filename as if it's XML, which explains the exception. I've clarified slightly - it really doesn't help that the code wouldn't even have compiled.
@forty-two: Does my answer make more sense now? I've added a bit about the parse(String) method as well though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.