如何用java DOM 在一xml檔案里加像 《!DOCTYPE和《!ELEMENTT @Nc呢

nanhoo發表於2004-02-24
如何用java DOM 在一xml檔案里加像 《!DOCTYPE和《!ELEMENTT @Nc呢
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!DOCTYPE article [
<!ELEMENT article (headline)>
<!ELEMENT headline (PCDATA)>
<!ENTITY sharp "≯">
]>
<article>
<headline>
Telecom or &sharp; on an upswing
</headline>
</article>
我用doc生成.xml遇到麻┭僵ol我呢,
@些c不知道如何加M去的
<!DOCTYPE article [
<!ELEMENT article (headline)>
<!ELEMENT headline (PCDATA)>
<!ENTITY sharp "≯">
]>

@些我已可以了
<article>
<headline>
Telecom or &sharp; on an upswing
</headline>
</article>
Need help with javax.xml.transform.Transformer! Urgent!
Hi!

I am currently using javax.xml.transform.Transformer to get XML from a DOM Document. I have to declare a DOCTYPE either externally or internally in an XML file. I have had some problems with the external DOCTYPE because I couldn't specify the base URI (because I don't know where the application will be installed). Therefore, I thought it would be a good idea to specify the DOCTYPE internally, i.e. by giving the DTD specification with the XML file itself.

When using Transformer, I get a problem. The problem is that the transform() method doesn't write the internal DOCTYPE declaration to my XML file. An example of the kind of XML file I want to use:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE entries [
<!ELEMENT entries (entry)*>
<!ELEMENT entry (active | line | station | reason)>
<!ELEMENT active (CDATA)>
<!ELEMENT line (CDATA)>
<!ELEMENT station (CDATA)>
<!ELEMENT reason (CDATA)>
]>

<entries>
<entry>
<line>134</line>
<station>332</station>
<reason>The stupid thing is not working</reason>
</entry>
</entries>



Is there a way of configuring the Transformer so that the internal DOCTYPE declaration is written with the XML data? Alternatively, is there an other way of solving the problem?

My Transformer code is like this:




try { transformer = TransformerFactory.newInstance().newTransformer(); /* * Tried a lot of things with this method without luck... * transformer.setOutputProperty(javax.xml.transform.OutputKeys.?????, dtdDeclaration); */} catch (TransformerConfigurationException e) { e.printStackTrace();} catch (TransformerFactoryConfigurationError transformerFactoryConfigurationError) { transformerFactoryConfigurationError.printStackTrace();}// Try to transform to XML and write to specified URItry { transformer.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(new File(absolutePath))));} catch (TransformerException e) { e.printStackTrace();} catch (FileNotFoundException e) { e.printStackTrace();}



Thanks!!

相關文章