Hi I have a problem when trying to load an XML file into Properties Object in Java. This is what I am trying to do:
public class loadXML() {
private dbConn
public loadXML(Connection currConn) {
this.dbConn = currConn;
}
public Properties populateProperties() {
ResultSet rst;
Statement stmt;
Connection con;
Properties prop;
con = dbConn.getConnection();
stmt = conn.createStatement();
rst.executeQuery("SELECT xml_row FROM xml_table");
if (rst.next()) {
prop = new Properties();
/* Old Code */
System.out.println(((XMLType)rst.getObject("xml_row")).getStringVal()); // * Note 1
prop.loadFromXML(((XMLType)rst.getObject("xml_row")).getInputStream());
/* Old Code */
/* New Code */
// transform xml loaded from database into byte stream.
byte[] bytes = ((XMLType)rst.getObject("xml_row")).getDOM().toString().getBytes("UTF-8");
// Load stream into properties object
prop.loadFromXML(new ByteArrayInputStream(bytes));
/* New Code */
}
return prop;
}
}
Below is the XML extract as it is saved in the database:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="key-name">01234</entry>
<entry key="key-name">123456</entry>
</properties>
Note 1 - This is what the System.out.println() is showing on screen:
XML Extract
<?xml version="1.0" encoding="UTF-8" standalone='no'?>
<!DOCTYPE properties>
<!--Copyright 2006 Sun Microsystems, Inc. All rights reserved.-->
<!-- DTD for properties -->
<properties version="1.0">
<entry key="key-name">01234</entry>
<entry key="key-name">123456</entry>
</properties>
Where I noticed that the version attribute is added to the element Properties (which however does not really bother me), however the problem is that the link to the DTD is removed (i.e SYSTEM "http://java.sun.com/dtd/properties.dtd") which is triggering an error when executing the following line of code:
prop.loadFromXML(((XMLType)rst.getObject("xml_row")).getInputStream());
Below is the error:
java.util.InvalidPropertiesFormatException: org.xml.sax.SAXParseException: : XML-20149: (Error) Element 'properties' used but not declared.