Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Read and Parse DTD doctype XML file from jar

 
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Sheriff
Posts: 22573
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22573
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
reply
    Bookmark Topic Watch Topic
  • New Topic