• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read and Parse DTD doctype XML file from jar  RSS feed

 
Madhavi Subramaniam
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to read Document Type Definition XML file from WAR project lib jar.

InputStream formatFileInputStream = null;
formatFileInputStream = getClass().getResourceAsStream("/jarName/fileName.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(formatFileInputStream );
doc.getDocumentElement().normalize();
fieldDetails = doc.getElementsByTagName("tagName");


I've coded like this.. but i am getting NULL value for formatFileInputStream ...

So can you someone Suggest good coding for read and parse XML file form jar


Thanks
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot directly access the jar file's contents and create an InputStream for it.

The second way doesn't seems to be good but still works.... And hopefully you are searching for a similar one using the class loader to load from the resources...

 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:You cannot directly access the jar file's contents and create an InputStream for it.

Uhm, yes you can. That's exactly what getResourceAsStream can do.

The problem here is that the path is incorrect. If the XML file is in the root of the JAR file, the path should be /fileName.xml, not /jarFile/fileName.xml. That's only valid if inside the JAR file there is a directory called jarFile with the fileName.xml file inside it.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob - I tried like below



The People.xml is in the root of the jar file (means next to the META-INF). But still the stream returned is null. Also if I place the People.xml file in the class path directory (bin folder) it gets recognized and the InputStream is formed.
 
Madhavi Subramaniam
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Rob. I've given wrong file path. that is the problem.

It works fine after given correct file path.

Thanks for your reply.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys... I too got it Since my AccessingJar.class file was in a package named ranch, it was placed inside the folder ranch.

I placed the copy of the myjar.jar file in the ranch folder next to the AccessingJar.class file and it worked Strange...
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhavi Subramaniam wrote:
Thanks Rob. I've given wrong file path. that is the problem.

It works fine after given correct file path.

Thanks for your reply.

You're welcome. And welcome to the Ranch!
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!