Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to check the xml file to be not empty before parsing?

800443Sep 3 2008 — edited Sep 5 2008
Hello I have an xml file , and I read all Elements of this file using xml parser as follow:
public void readXmlFile(){
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	 DocumentBuilder builder = factory.newDocumentBuilder();
	 Document doc = builder.parse(filePath);
	System.out.println(builder.parse(filePath).getAttributes());
	NodeList list = doc.getElementsByTagName("*");
        ....
}
if I have the following xml file I can read it with out any problem:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<employee>
    <person id=1>
            <name>john</name>
            <lastname>logan</logan>
    </person>
</employee>
but if i have my xml file empty means only with the xml header :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
Then when it comes over this method it fails and gives me this error: *[Fatal Error] file.xml:2:1: Premature end of file.*
because there is no element in the file.
The question is how can i check the content of my xml file to be not empty before parsing?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 3 2008
Added on Sep 3 2008
11 comments
1,515 views