Bastian
Bastian

Reputation: 1593

List inside Java XML Properties

I want to store a list of key-value pairs as values to specific key in a properties xml file.

Consider following example:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="led.color">
       <color key="r">0</color>
       <color key="g">100</color>
       <color key="b">0</color>
    </entry>
</properties>

My initial approach was to retrieve the value of led.color as String and convert it to a Map. But the retrieval failed with following exception:

Caused by: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 20; Element type "color" must be declared.

This happens because color is not declared in the DTD and hence, the validation fails.

Is there any build-in way to handle such task or a way to avoid the validation of this specific value?

This is the references DTD:

<!--
   Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
-->
<!-- DTD for properties -->
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>

Upvotes: 0

Views: 1388

Answers (1)

andolsi zied
andolsi zied

Reputation: 3791

your new xml file :

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <!DOCTYPE properties SYSTEM "properties.dtd">
 <properties>
   <entry key="led.color">
    <color key="r">0</color>
    <color key="g">100</color>
    <color key="b">0</color>
  </entry>
</properties>

properties.dtd file to put in the same directory

 <?xml encoding="UTF-8"?>

 <!ELEMENT properties (entry)>
 <!ATTLIST properties xmlns CDATA #FIXED ''>

 <!ELEMENT entry (color)+>
 <!ATTLIST entry xmlns CDATA #FIXED '' key NMTOKEN #REQUIRED>

 <!ELEMENT color (#PCDATA)>
 <!ATTLIST color xmlns CDATA #FIXED '' key NMTOKEN #REQUIRED>

Upvotes: 0

Related Questions