• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

XML file Location

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
i am new to XML programming. i am successfull in reading the data from XML file by using SAX parser. My question is while reading the XML file i did not mentioned any path ,it is able to read XML file only from my project root directory. for example C:\testing is the location i placed my project , application is able to read only from the above mentioned location.

i'm pasting my code for your reference. Please provide any idea on this.

Code:
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc=docBuilder.parse(new File("AggregationModes.xml"));
doc.getDocumentElement().normalize();
NodeList aggregationModesList=doc.getElementsByTagName("Aggregation-Modes");
for (int i = 0; i < aggregationModesList.getLength(); i++)
{
Node nodes=aggregationModesList.item(i);
if(nodes.getNodeType()==Node.ELEMENT_NODE)
{
Element aggregationModes =(Element)nodes;
//Reading Aggregation Mode ID from XML
NodeList listAggregationIds=aggregationModes.getElementsByTagName("ID_AGGREGATION_TFV");
Element aggregationsIDElement = (Element)listAggregationIds.item(0);
NodeList textAggregationIdList = aggregationsIDElement.getChildNodes();
//Reading Aggregation Modes from XML
NodeList listAggregationModes=aggregationModes.getElementsByTagName("AGGREGATION_MODE_DESC");
Element aggregationModeElement = (Element)listAggregationModes.item(0);
NodeList textAggregationModeList = aggregationModeElement.getChildNodes();
int j=Integer.parseInt(textAggregationIdList.item(0).getNodeValue().toString());


Thanks in Advance,
Hareendra.
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can specify the path in the File object, e.g.
 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Freddy Wong,

Thanks for your valuable answer. if we mention like that as you said Document doc = docBuilder.parse(new File"C:\Projects\AggregationModes.xml")); our appliations looks only at this location, consider the scenario what happens if we change the location of the file or location of the project itself.

Thanks in advance.
Hareendra.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course you shouldn't hard code the location of the file in the source code. In fact, it's a very bad idea. You can put the information on where your XML file is in the configuration file, e.g. in the Java properties file.

You can also put it in the System property. So you when you run your application, it's gonna be something like this:
java -jar -Dxml.file.path=C:/Projects/YourXML.xml YouJar.jar

There are actually many ways to achieve this.

Hope this helps.
[ October 19, 2007: Message edited by: Freddy Wong ]
 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help. I'll try this using property file, and let you know if i face any issues further.
reply
    Bookmark Topic Watch Topic
  • New Topic