如何用java do M 在一個xml文件里加像 《!DOCTYPE和《!ELEMENTT 這種節點呢 (100分)

  • 主题发起人 主题发起人 goddy
  • 开始时间 开始时间
G

goddy

Unregistered / Unconfirmed
GUEST, unregistred user!
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!DOCTYPE article [
<!ELEMENT article (headline)>
<!ELEMENT headline (#PCDATA)>
<!ENTITY sharp "≯">
]>
<article>
<headline>
Telecom or &amp;sharp;
on an upswing
</headline>
</article>
我用doc生成.xml遇到麻煩呀﹐誰幫幫我呢,
這些節點不知道如何加進去的
<!DOCTYPE article [
<!ELEMENT article (headline)>
<!ELEMENT headline (#PCDATA)>
<!ENTITY sharp "≯">
]>
這些我已經可以了
<article>
<headline>
Telecom or &amp;sharp;
on an upswing
</headline>
</article>
 
哥们要是真的没办法解决,那就直接进行文件操作把这部分插入。我以前懒得用DOM的时候就是自己写的XML生成器,这样就想怎么写就怎么写。
 
Need help with javax.xml.transform.Transformer! Urgent!
Hi!
I am currently using javax.xml.transform.Transformer to get XML from ado
Mdo
cument. I have to declare ado
CTYPE either externally or internally in an XML file. I have had some problems with the externaldo
CTYPE because I couldn't specify the base URI (because Ido
n't know where the application will be installed). Therefore, I thought it would be a good idea to specify thedo
CTYPE 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() methoddo
esn't write the internaldo
CTYPE 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 internaldo
CTYPE 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(newdo
MSource(doc), new StreamResult(new FileOutputStream(new File(absolutePath))));} catch (TransformerException e) { e.printStackTrace();} catch (FileNotFoundException e) { e.printStackTrace();}

Thanks!!
 
use JDOM and here is its way to createdo
CType:

____
import org.jdom.*;
import org.jdom.output.XMLOutputter;

public class TryDocType {
public static void main(String[] args) {
DocTypedo
ctype = newdo
cType("article");
String dtd = "<!ELEMENT article (headline)>/n";
dtd += "<!ELEMENT headline (#PCDATA)>/n";
dtd += "<!ENTRY sharp /">/">";
doctype.setInternalSubset(dtd);
Element root = new Element("Your_root");
Documentdo
c = newdo
cument(root,do
ctype);
XMLOutputter op = new XMLOutputter();
op.setIndent(" ");
op.setNewlines(true);
try{
op.output(doc, System.out);
} catch (Exception e){}
}
}
____

The output is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article [
<!ELEMENT article (headline)>
<!ELEMENT headline (#PCDATA)>
<!ENTRY sharp ">">]>
<Your_root />

It is pretty close to what you need. Ido
n't think there is an easy in JDOM that allows one to set the standalone tag. If that is a must, you may have to go to Sun XML API--I assume it have more finer control over that. If you know how todo
that please let me know. Thank you in advance.

------------------------------
--
xzma
 
后退
顶部