由于没有xml的DTD需要分析xml的结构
不知道使用jdom怎么样得到xml的结构树了
不知道各位GGJJ有没有示例程序能借鉴一下,小弟感激不尽
------解决方案--------------------
package com.icbridge.mx.util;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.apache.log4j.Logger;
import com.icbridge.mx.dbc.OracleDBC;
public class XmlWriter {
private static Logger logger = Logger.getLogger(XmlWriter.class);
public XmlWriter(){
}
/**
*
* @Author: Gary.M
* @Date: 2007-04-11 上午09:44:24
* @param sql
* @param parentElement 父级Element名
* @param childElement 子Element名
* @param fileName 最终生成的(xml)文件名(包括路径)如: "d:\\data.xml "
* @return
*/
public boolean isCreate(String sql,String parentElement,String childElement,String fileName){
boolean isDone = true;
OracleDBC dbc = null;
Connection con = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
ResultSetMetaData rsmd = null;
int numberOfColumns = 0;
Document document=new Document(new Element(parentElement));//创建文档
try
{
dbc = new OracleDBC();
con = dbc.getConnection();
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
rsmd = rs.getMetaData(); //获取字段名
numberOfColumns = rsmd.getColumnCount(); //获取字段数
if(rs == null)
{
logger.info( "无法取得记录集! ");
isDone = false;
}
else
{
int i=0;
while(rs.next())//将查询结果取出
{
Element element0=new Element(childElement); //创建元素 生成JDOM树
document.getRootElement().addContent(element0);
for(i=1; i <=numberOfColumns;i++)
{
String date = rs.getString(i);
//new String(rs.getString(i).getBytes( "ISO-8859-1 "), "gb2312 "); //代码转换
Element element = new Element(rsmd.getColumnName(i)).setText(date);
element0.addContent(element);
}
}
}
rs.close();
pstmt.close();
}catch(Exception e)
{
logger.error( "数据库操作失败! "+e);
}
finally
{
dbc.close();
}
XMLOutputter outp = new XMLOutputter();
try
{
outp.output(document, new FileOutputStream(fileName)); //输出XML文档
}
catch(IOException e)
{
logger.error( "XML 文档生成失败! "+e);
}
logger.info( "XML 文档生成完毕! ");
return isDone;
}
}
------解决方案--------------------
dtd只是规范xml文档,dom在没有dtd的时候照样可以按照他自己的方式解析文档
------解决方案--------------------
- Java code
package run;import org.jdom.*;import org.jdom.input.*;import java.io.*;import java.util.*;;public class jdomTest { public void jdomtest() { String textXml = null; textXml = "<note width="+"\"100%\""+">"; textXml = textXml + "<to><to1>a111</to1><to2>a222</to2><to3>a333</to3></to><from>bbb</from><heading>ccc</heading><body>ddd</body>"; textXml = textXml + "</note>"; System.out.println(textXml); SAXBuilder builder = new SAXBuilder(); Document doc = null; Reader in= new StringReader(textXml); try { doc = builder.build(in); Element root = doc.getRootElement(); System.out.println(root.getAttributeValue("width")); List ls = root.getChildren();//注意此处取出的是root节点下面的一层的Element集合 for (Iterator iter = ls.iterator(); iter.hasNext(); ) { Element el = (Element) iter.next(); if(el.getName().equalsIgnoreCase("to")){ List ls2 = el.getChildren(); for(Iterator iter2 = ls2.iterator(); iter2.hasNext();){ Element e2 = (Element)iter2.next(); if(e2.getTextTrim().equals("to1")){ e2.setText("I have bean changed in this element."); System.out.println(e2.getName()); continue; } System.out.println(e2.getName()); } continue; } System.out.println(el.getText()); } } catch (IOException ex) { ex.printStackTrace(); } catch (JDOMException ex) { ex.printStackTrace(); } }}