While parsing to XML files with SAX Parser in JAVA, the common exception occurs as Document Root Element is missing.
If the XML is well formed then the exception can occur because it is created in UNIX environment and you are trying to parse in windows environment.
There can be three solutions for this exception if it is well-formed
1) Manual
2) Using setEncoding Method
3) Using Script in your java code.
Manual Solution:
First solution is to open xml in Notepad and hit the enter at the end of file.so it will add one blank line after end of root element.
But if you have more than one file to parse above manual option is not a good option.
setEncoding Method:
This method is used in before calling parser.parse method.
we need to call setEncoding("UTF-8") method beofre parsing XML file.
Using VBScript
The better solution is to call below vbscript from your java code so it will overcome all manual task for adding new line.
--------------------------------------------------------------------------
dim file_name
Set objFS = createObject("Scripting.FileSystemObject")
file_Folder = WScript.Arguments.Item(0)
Set xmlDom = CreateObject("Microsoft.XMLDOM")
xmlDom.async = False
xmlDom.Load(Your XML file with full path)
xmlDom.Load(Yout XML file with full path)
---------------------------------------------------------------------------
The above script can be call from your java code using Runtime.getRuntime.exec() method.
If you have more files in the sub folders you can use recursive function to parser all the XMLs from the parent folder.
This way you can solve the Root Element missing exception. But i believe 2nd option is the best option for solving this exception.
If the XML is well formed then the exception can occur because it is created in UNIX environment and you are trying to parse in windows environment.
There can be three solutions for this exception if it is well-formed
1) Manual
2) Using setEncoding Method
3) Using Script in your java code.
Manual Solution:
First solution is to open xml in Notepad and hit the enter at the end of file.so it will add one blank line after end of root element.
But if you have more than one file to parse above manual option is not a good option.
setEncoding Method:
This method is used in before calling parser.parse method.
we need to call setEncoding("UTF-8") method beofre parsing XML file.
Using VBScript
The better solution is to call below vbscript from your java code so it will overcome all manual task for adding new line.
--------------------------------------------------------------------------
dim file_name
Set objFS = createObject("Scripting.FileSystemObject")
file_Folder = WScript.Arguments.Item(0)
Set xmlDom = CreateObject("Microsoft.XMLDOM")
xmlDom.async = False
xmlDom.Load(Your XML file with full path)
xmlDom.Load(Yout XML file with full path)
---------------------------------------------------------------------------
The above script can be call from your java code using Runtime.getRuntime.exec() method.
If you have more files in the sub folders you can use recursive function to parser all the XMLs from the parent folder.
This way you can solve the Root Element missing exception. But i believe 2nd option is the best option for solving this exception.
Good one
ReplyDelete