-
XML DTD problem
I have an xml file I want to validate.
XML file is:
Code:
<?xml version="1.0"?>
<!DOCTYPE gridbag SYSTEM "gridbag.dtd">
<gridbag>
<row>
<cell anchor="EAST">
<bean>
<class>javax.swing.JLabel</class>
<property>
<name>text</name>
<value><string>Face: </string></value>
</property>
</bean>
</cell>
<cell fill="HORIZONTAL" weightx="100">
<bean id="face">
<class>javax.swing.JComboBox</class>
</bean>
</cell>
<cell gridheight="4" fill="BOTH" weightx="100" weighty="100">
<bean id="sample">
<class>javax.swing.JTextArea</class>
<property>
<name>text</name>
<value><string>The quick brown fox jumps over the lazy dog</string></value>
</property>
<property>
<name>editable</name>
<value><boolean>false</boolean></value>
</property>
<property>
<name>lineWrap</name>
<value><boolean>true</boolean></value>
</property>
<property>
<name>border</name>
<value>
<bean>
<class>javax.swing.border.EtchedBorder</class>
</bean>
</value>
</property>
</bean>
</cell>
</row>
<row>
<cell anchor="EAST">
<bean>
<class>javax.swing.JLabel</class>
<property>
<name>text</name>
<value><string>Size: </string></value>
</property>
</bean>
</cell>
<cell fill="HORIZONTAL" weightx="100">
<bean id="size">
<class>javax.swing.JComboBox</class>
</bean>
</cell>
</row>
<row>
<cell gridwidth="2" weighty="100">
<bean id="bold">
<class>javax.swing.JCheckBox</class>
<property>
<name>text</name>
<value><string>Bold</string></value>
</property>
</bean>
</cell>
</row>
<row>
<cell gridwidth="2" weighty="100">
<bean id="italic">
<class>javax.swing.JCheckBox</class>
<property>
<name>text</name>
<value><string>Italic</string></value>
</property>
</bean>
</cell>
</row>
</gridbag>
DTD file I use is
Code:
<!ELEMENT gridbag (row)*>
<!ELEMENT row (cell)*>
<!ELEMENT cell (bean)>
<!ATTLIST cell gridx CDATA #IMPLIED>
<!ATTLIST cell gridy CDATA #IMPLIED>
<!ATTLIST cell gridwidth CDATA "1">
<!ATTLIST cell gridheight CDATA "1">
<!ATTLIST cell weightx CDATA "0">
<!ATTLIST cell weighty CDATA "0">
<!ATTLIST cell fill (NONE|BOTH|HORIZONTAL|VERTICAL) "NONE">
<!ATTLIST cell anchor
(CENTER|NORTH|NORTHEAST|EAST|SOUTHEAST|SOUTH|SOUTHWEST|WEST|NORTHWEST) "CENTER">
<!ATTLIST cell ipadx CDATA "0">
<!ATTLIST cell ipady CDATA "0">
<!ELEMENT bean (class, property*)>
<!ATTLIST bean id ID #IMPLIED>
<!ELEMENT class (#PCDATA)>
<!ELEMENT property (name, value)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT value (int|string|boolean|bean)>
<!ELEMENT int (#PCDATA)>
<!ELEMENT string (#PCDATA)>
<!ELEMENT boolean (#PCDATA)>
I am getting an exception in this method:
Code:
public GridBagPane(String filename)
{
setLayout(new GridBagLayout());
constraints = new GridBagConstraints();
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
File file = new File(filename);
//added for debugging start
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0) System.out.println(dis.readLine());
//added for debbuging end
Document doc = builder.parse(file);
parseGridBag(doc.getDocumentElement());
}
catch (Exception e)
{
e.printStackTrace();
}
}
The line of code Document doc = builder.parse(file) is throwing an exception. I added the part of code between debugging comments to see if application is managing to find the file and create a valid File object. xml file is getting printed ok. I believe problem is either in location DTD file or in the structure or xml or DTD file. Since I copied both files from the book I believe most likely problem is that application can not find the DTD file.
I am using Eclipse. I have put both the xml and DTD file in the project directory in Eclipse workspace directory. I run application from Eclipse. Where should I put the DTD file or how to modify the <!DOCTYPE gridbag SYSTEM "gridbag.dtd"> so that application can find the DTD file. I have googled I lot and I can not find this.
Please help.
Thanks in advance.
-
I managed to solve the problem. I did copy paste of the xml file and had blank characters before <?xml version="1.0"?>. When I removed them everything worked. Sorry to trouble you.