New issue 40 by mikenere...@gmail.com: set doctype
http://code.google.com/p/xmltool/issues/detail?id=40
Is it possible to set the doctype?
I have an english properties file that I translate to several other
languages. It seems in order for Properties#loadFromXML to work I need to
include a doctype:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
Properties#loadFromXML
Not your problem, not information nontheless, here is the stacktrace from
trying to load the file created with XML tool
Exception in thread "main" java.util.InvalidPropertiesFormatException:
org.xml.sax.SAXParseException: Document root element "properties", must
match DOCTYPE root "null".
at java.util.XMLUtils.load(XMLUtils.java:59)
at java.util.Properties.loadFromXML(Properties.java:852)
at com.retrieve.XmlApp.main(XmlApp.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.xml.sax.SAXParseException: Document root
element "properties", must match DOCTYPE root "null".
And here is a sample of the file being loaded.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<properties>
<entry key="i18n.language.abbreviation">pl</entry>
<entry key="i18n.language.name">Polish</entry>
<entry key="application.header.home">Dom</entry>
My solution, for the record, while waiting to hear if this functionality is
available in XML tool, was to write the XML file out, then read it back in
add the doctype and write it out again.
http://www.exampledepot.com/egs/javax.xml.transform/SetDocType.html
private static void addPropertiesDoctype(final File xmlFile)
{
final Document document =
BasicDom.parseXmlFile(xmlFile.getAbsolutePath(), false);
try
{
final Transformer transformer =
TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://java.sun.com/dtd/properties.dtd");
final Source source = new DOMSource(document);
final Result result = new StreamResult(xmlFile);
transformer.transform(source, result);
}
catch (final TransformerConfigurationException e)
{
}
catch (final TransformerException e)
{
}
}
resulting in
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
Comment #3 on issue 40 by mathieu....@gmail.com: set doctype
http://code.google.com/p/xmltool/issues/detail?id=40
Hi,
Thanks for the report, this is definitely a feature that should be added.
I'll try to do my best to spend time on it, plus other requests, by the end
of the month and release a new version. I am very busy at the moment.
Mathieu, don't rush for me. I have a solution that works. I really just
wasn't sure if I was not looking in the right place, or if it was a missing
feature.
I thought I could get at the underlying dom builder, or something, to set
the value, but if its a missing feature, then you've confirmed that its not
at this time possible to do this.
Thanks, for the tool. Been using it on several projects since 2008.