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?