File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes reading properties with xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "reading properties with xml " Watch "reading properties with xml " New topic
Author

reading properties with xml

Veronica Damian
Ranch Hand

Joined: Feb 04, 2006
Posts: 61
Hi, how do you read properties from an xml file in Tocmat? I'm talking about props that I set (variables), not config info for server or webapps..

I just want to read props from an xml file like you would from a .properties or .config file.. I have looked and looked, found a xml-apis.jar in Tomcat (common/endorsed), but did not come w/docs... there is also a xercesImpl.jar, do I need this.. can I put properties I set in web.xml or do I need a different xml file..

I have searched google ad nauseum.. all results are about Tomcat configurations for servers, etc...

thank you very much..

-v
[ March 08, 2006: Message edited by: Bear Bibeault ]
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
Well, placing the XML file with the "properties" definition in the class
path (preferabbly in the WEB-INF/classes directory if this is only for a
specific webapp, and then using getResourceAsStream(propsFileName) should
do it for you. Read the API doc for the Properties class and the routine
loadFromXML(InputStream)
Veronica Damian
Ranch Hand

Joined: Feb 04, 2006
Posts: 61
how is this used pls..

have an xml file called props.xml in web-inf/classes; in servlet:

Class.getResourceAsStream(props); // won't compile..

also tried

Class.getResourceAsStream(props.xml);// also doesn't compile..

what do I put at top of xml? just for testing purposes put this in xml:


<?xml version="1.0" encoding="ISO-8859-1"?>
<props>
<myName>Veronica</myName>
</props>

not sure header is right.. I just can't find info on this; all info I find related to xml/Tomcat deals with Tomcat/webapp configuration, not reading properties..

thank you very much..

-v
Veronica Damian
Ranch Hand

Joined: Feb 04, 2006
Posts: 61
got it.. this compiled:

InputStream is = getClass().getResourceAsStream("props.xml");

however, loadFromXML() method is 1.5-only.. how do you do this if you're on 1.4 or 1.3.. many thanks...

-v
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

There is nothing Tomcat-specific, or even web-specific, about this question. Moved to the XML forumm with an appropriate title change.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Originally posted by Veronica Damian:
however, loadFromXML() method is 1.5-only.. how do you do this if you're on 1.4 or 1.3.. many thanks...
You don't. The Properties class doesn't support the XML format in those versions.

That's not to say you can't use XML-formatted information in those versions. You just can't use the Properties class to read it in. You would have to write your own code to extract the information, or perhaps use somebody else's code (such as Apache Commons Digester).
Veronica Damian
Ranch Hand

Joined: Feb 04, 2006
Posts: 61
thank you.. I'm on Tomcat 3, it comes with a xml-apis.jar, but no docs.. do you know where I can find docs for it.. thank you..

-v
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading properties with xml