List inside Java XML Properties

时间:2016-10-20 13:03:39

标签: java xml properties dtd

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>

1 个答案:

答案 0 :(得分:0)

您的新xml文件:

<button onclick="resettimeinMainHtml()">RESET TIME</button>

<script>function resettimeinMainHtml(){window.opener.resettime();// will reset in mail html}</script>

properties.dtd文件放在同一目录中

 <?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>