有谁在用 jdom.jar beta 9 开发 java+xml 的人吗?一起来讨论一些问题。(50分)

  • 主题发起人 主题发起人 WorldCreater
  • 开始时间 开始时间
W

WorldCreater

Unregistered / Unconfirmed
GUEST, unregistred user!
第一个问题:
private staticdo
cumentdo
c = null;
public static void prepareDoc(String fileName) {
try{
//create the JDOM SAXBuilder
SAXBuilder saxBuilder = new SAXBuilder (true);
//prepare the JDOM tree
do
c = saxBuilder.build (new File(fileName));
} catch (JDOMException e) {
System.out.println ("JDOMException: " + e.getMessage());
} catch (IOException e) {
System.out.println ("IOException: " + e.getMessage());
} catch (Exception e) {
System.out.println ("Exception: " + e.getMessage());
}
}
SAXBuilder 强制要求要有dtd吗?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE CONFIGURATIONS SYSTEM "./CONFIG.dtd"><!-----要是没有这句,就会有错误---->
<CONFIGURATIONS>
<CONFIG>
<DRIVERNAME>com.microsoft.jdbc.sqlserver.SQLServerDriver</DRIVERNAME>
</CONFIG>
</CONFIGURATIONS>
=============================================================================
JDOMException: Error on line 3 ofdo
cument file:/e:/xxx/config.xml:do
cument is
invalid: no grammar found.
Exception in thread "main" java.lang.NullPointerException
at com.bcstnet.enterprise.beans.XmlSetting.forTest(XmlSetting.java:44)
at com.bcstnet.enterprise.beans.XmlSetting.main(XmlSetting.java:24)
 
试试:
SAXBuilder saxBuilder = new SAXBuilder (false);
 
e://connectionconfig.xml
Exception in thread "main" java.lang.NullPointerException
at com.bcstnet.enterprise.beans.XmlSetting.forTest(XmlSetting.java:44)
at com.bcstnet.enterprise.beans.XmlSetting.main(XmlSetting.java:24)
Press any key to continue...
====================
xml内容
<?xml version="1.0" ?>
<config>
<drivername>com.microsoft.jdbc.sqlserver.SQLServerDriver</drivername>
<connection>jdbc:microsoft:sqlserver://192.168.0.110:1433</connection>
<user>bc</user>
<password>123</password>
<managerid>28</managerid>
</config>
 
XmlSetting.java:44 ?????
把源文件贴出来吧。
 

/**
*uses jdom.jar beta 9
*/
import org.jdom.*;
import org.jdom.adapters.*;
import org.jdom.filter.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.transform.*;
import org.jdom.xpath.*;
import java.io.*;
import java.io.IOException;
public class XmlSetting {
private staticdo
cumentdo
c = null;
public static void main (String[] args) {
if (args.length > 0 ) {
System.out.println (args[0]);
xXmlSetting( args[0] );
forTest();
}
}
public static void xXmlSetting (String fileName) {
try{
//create the JDOM SAXBuilder
SAXBuilder saxBuilder = new SAXBuilder (false);
//prepare the JDOM tree
do
c = saxBuilder.build (new File(fileName));
} catch (JDOMException e) {
System.out.println ("JDOMException: " + e.getMessage());
} catch (IOException e) {
System.out.println ("IOException: " + e.getMessage());
} catch (Exception e) {
System.out.println ("Exception: " + e.getMessage());
}
}
public static void forTest () {
System.out.println(do
c.getRootElement().getChild("CONFIG").getChild("DRIVERNAME").getText().trim() );
do
c.getRootElement().getChild("CONFIG").getChild("DRIVERNAME").setText(new String("com.microsoft.jdbc.sqlserver.SQLServerDriver"));
System.out.println(do
c.getRootElement().getChild("CONFIG").getChild("DRIVERNAME").getText().trim() );
XMLOutputter outputter = new XMLOutputter();
try{
outputter.output(doc,System.out);
java.io.FileOutputStream x = new FileOutputStream("e://out.xml");

outputter.output(doc, x);
}catch (Exception e){System.out.println(e.getMessage());}
}

}
 
ft, config已经就是root了。你对getRootElement()再调用getChild("CONFIG")当然会抛出NullException了!
这样:
public static void forTest () {
System.out.println(do
c.getRootElement().getChild("drivername").getText().trim());
do
c.getRootElement().getChild("drivername").setText(new String("com.microsoft.jdbc.sqlserver.SQLServerDriver"));
System.out.println(do
c.getRootElement().getChild("drivername").getText().trim());
XMLOutputter outputter = new XMLOutputter();
try{
outputter.output(doc,System.out);
java.io.FileOutputStream x = new FileOutputStream("e://out.xml");

outputter.output(doc, x);
}catch (Exception e){System.out.println(e.getMessage());}
}
 
你好象误解了,我的xml文档要是有dtd的声明的话是不出错的。
把dtd声明删除后才报错的。
问题在哪?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE CONFIGURATIONS SYSTEM "./CONFIG.dtd"><!-----要是没有这句,就会有错误---->
<CONFIGURATIONS>
<CONFIG>
<DRIVERNAME>com.microsoft.jdbc.sqlserver.SQLServerDriver</DRIVERNAME>
</CONFIG>
</CONFIGURATIONS>
 
上边写错了,我现在的xml文档根元素是<configurations>

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 rel. 3 U (http://www.xmlspy.com) by aa (bb) -->
<!DOCTYPE CONFIGURATIONS SYSTEM "./CONFIG.dtd">/////////////////问题是没有这句就出错
<CONFIGURATIONS>
<CONFIG>
<DRIVERNAME>com.microsoft.jdbc.sqlserver.SQLServerDriver</DRIVERNAME>
</CONFIG>
</CONFIGURATIONS>
 
问题还没解决。
再问一个问题:jdom中有将xml根据xsl转换的类及函数没?叫什么?
 
我在xmlspy
中转换正常的xsl,在这段程序中转化后不正常的输出。。。。。。。。
Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("E://CONFIG.xslt"));
JDOMSource jdomSource = new JDOMSource(doc);
JDOMResult jdomResult = new JDOMResult();
transformer.transform(jdomSource,jdomResult);
do
cumentdo
c2 = jdomResult.getDocument();
outputter.output(doc2,System.out);

========================================
=============== xsl =====================
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:template match="/">
<html>
<head/>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="Config">
<xsl:if test="position()=1">
<xsl:text disable-output-escaping="yes">&amp;lt;table border="1"&amp;gt;</xsl:text>
</xsl:if>
<xsl:if test="position()=1">
<thead>
<tr>
<td>Id</td>
<td>DriverName</td>
<td>ConnectionURL</td>
<td>UserName</td>
<td>UserPassword</td>
</tr>
</thead>
</xsl:if>
<xsl:if test="position()=1">
<xsl:text disable-output-escaping="yes">&amp;lt;tbody&amp;gt;</xsl:text>
</xsl:if>
<tr>
<td>
<xsl:for-each select="@Id">
<input>
<xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
</input>
</xsl:for-each>
</td>
<td>
<xsl:for-each select="DriverName">
<input>
<xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
</input>
</xsl:for-each>
</td>
<td>
<xsl:for-each select="ConnectionURL">
<input>
<xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
</input>
</xsl:for-each>
</td>
<td>
<xsl:for-each select="UserName">
<input>
<xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
</input>
</xsl:for-each>
</td>
<td>
<xsl:for-each select="UserPassword">
<input>
<xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
</input>
</xsl:for-each>
</td>
</tr>
<xsl:if test="position()=last()">
<xsl:text disable-output-escaping="yes">&amp;lt;/tbody&amp;gt;</xsl:text>
</xsl:if>
<xsl:if test="position()=last()">
<xsl:text disable-output-escaping="yes">&amp;lt;/table&amp;gt;</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

==============================================================================
============ 正常输出=======================================
<html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><head /><body>
<META http-equiv="Content-Type" content="text/html;
charset=UTF-16">
<table border="1">
<thead><tr><td>Id</td><td>DriverName</td><td>ConnectionURL</td><td>UserName</td><td>UserPassword</td>
</tr>
</thead><tbody><tr>
<td><input value="SqlServer"></td>
<td><input value="com.microsoft.jdbc.sqlserver.SQLServerDriver"></td>
<td><input value="jdbc:microsoft:sqlserver://127.0.0.1:1433"></td>
<td><input value="UserName"></td>
<td><input value="UserPassword"></td>
</tr>
<tr>
<td><input value="Access"></td>
<td><input value="jdbc.odbc.JdbcOdbcDriver"></td>
<td><input value="c:/demo.mdb"></td>
<td><input value="UserName"></td>
<td><input value="UserPassword"></td>
</tr></tbody></table></body></html>
===========================================================================
==============================不正常输出
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><head /><body>
<tr><td><input value="SqlServer" /></td><td><input value="com.microsoft.jdbc.sqlserver.SQLServerDriver" /></td><td><input value="jdbc:microsoft:sqlserver://127.0.0.1:1433" /></td><td><input value="UserName" /></td><td><input value="UserPassword" /></td></tr>
<tr><td><input value="Access" /></td><td><input value="jdbc.odbc.JdbcOdbcDriver" /></td><td><input value="c:/demo.mdb" /></td><td><input value="UserName" /></td><td><input value="UserPassword" /></td></tr>
<tr><td><input value="debug" /></td><td><input value="" /></td><td /><td><input value="" /></td><td><input value="" /></td></tr><?javax.xml.transform.disable-output-escaping?>&amp;lt;/tbody&amp;gt;<?javax.xml.transform.enable-output-escaping?><?javax.xml.transform.disable-output-escaping?>&amp;lt;/table&amp;gt;<?javax.xml.transform.enable-output-escaping?></body></html>
=======================================================================
=============================== 原因在于有些实体没有转换
如: &amp;lt;
我应该怎么做?
 
接受答案了.
 
后退
顶部