• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading an XML file using Properties class  RSS feed

 
Ayan Dutta
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am reading an an XML using the following code

import java.util.*;
import java.io.*;

public class ReadFromXML {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("D:/abc.xml");

try{
prop.loadFromXML(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " +
prop.getProperty("foo"));

}
catch(Exception e){
System.out.println("Exception :##" + e);

}
}
}

It is working fine.
But one thing to note is that its dtd is mentioned as

...<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

But when I am trying with another document like struts-config.xml whose dtd is mentioned as...
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">


when I run the program the exception is generated as

java.util.InvalidPropertiesFormatException: org.xml.sax.SAXException: Invalid system identifier: http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd

How can I make my prog generic independent of dts only using the properties class ?
Regards.
Ayan
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I make my prog generic independent of dts only using the properties class ?

You can't - the JavaDocs specifically state that loadFromXML only works with that one DTD. Why would you expect otherwise - how could a Properties object cope with a complex XML format?
You will have to learn some XML processing and work with a Document object.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!