【高分200】如何分析这个xml,得到所有节点,用于创建数据表,生成类 你可以使用jdom进行读取xml文件,然后使用jdbc建立表,不过你要生成类就不知道你要生成什么样子了呢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是不是就生成需要的set/get方法呢,如果是应该很简单的。 用jsp或servlet(java语言),2个100分,一旦可以马上结贴,决不失言! 我是新手最好具体一点。 谢谢! 大致的过程应该是:指定xml文件-〉使用jdom打开该xml文件-〉读取表名及各个字段的信息-〉组装成对应的sql语句-〉使用jdbc建立数据库连接-〉执行该sql语句-〉使用fileiostream建立.java文件-〉将需要的java代码写入该文件-〉end 你可以看看:books.xml --待处理的xml文件<?xml version="1.0" encoding="UTF-8"?><catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <publish_date> <year>2000</year> <month>10</month> <day>1</day> </publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <publish_date> <year>2000</year> <month>12</month> <day>16</day> </publish_date> <description>A former architect battles corporate zombies.</description> </book> <book id="bk103"> <author>Corets, Eva</author> <title>Maeve Ascendant</title> <publish_date> <year>2000</year> <month>11</month> <day>17</day> </publish_date> <description>After the collapse of a nanotechnology society in England.</description> </book></catalog>使用DOM解析 --BookParser.javaimport javax.xml.parsers.*;import org.w3c.dom.*;import java.io.*;/** *使用DOM提取XML内容的例子 */class BookParser{ public static void main(String[] args){ try{ //获取一个XML解析器 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); //解析XML文件 Document document=builder.parse(new File("books.xml")); //去掉XML文档中空白部分 document.normalize(); //获取根节点并打印根节点的名称 Element root=document.getDocumentElement(); System.out.println("根原始的名称:"+root.getTagName()); //获取所有的"book"标记,它是一个NodeList对象 NodeList books=root.getElementsByTagName("book"); //遍历NodeList System.out.println("书本列表"); for(int i=0;i<books.getLength();i++){ //获取books中的每一个元素 Element book=(Element)books.item(i); //获取每个"book"标记的"id"属性 String book_id=book.getAttribute("id");//或book.getAttributeNode("id").getValue(); System.out.println("序号:"+book_id); //获取"book"标记下的每个元素 System.out.print("作者:"); System.out.println(book.getElementsByTagName("author").item(0).getFirstChild().getNodeValue()); System.out.print("标题:"); System.out.println(book.getElementsByTagName("title").item(0).getFirstChild().getNodeValue()); //获取"publish_date"元素 Element publishDate=(Element)book.getElementsByTagName("publish_date").item(0); //获取"publish_date"元素下的每个子元素 String year=publishDate.getElementsByTagName("year").item(0).getFirstChild().getNodeValue(); String month=publishDate.getElementsByTagName("month").item(0).getFirstChild().getNodeValue(); String day=publishDate.getElementsByTagName("day").item(0).getFirstChild().getNodeValue(); System.out.println("出版日期:"+year+"年"+month+"月"+day+"日"); System.out.print("描述:"); System.out.println(book.getElementsByTagName("description").item(0).getFirstChild().getNodeValue()); System.out.println("--------------------"); } } catch(Exception e2){} }} 谢谢 zxhong(红透半边天)、jcq(疯子弟)、 kreven(天地无用恨离别) 等! 我刚接触到这部分知识,在大家的热心帮助下,我会学好的! 再次,谢谢大家的帮助,特别是“zxhong(红透半边天)”! 请问如何将bufferedImage里的图像转成二进制数组啊 JavaScript两个数字比较出问题了!!! MySql+jsp+Tomcat无法创建下列URL的连接: 关于MYSQL中显示乱码的问题? 在线急等! 关于淘宝网图片浏览器 问一根据范围不同取不同值的查询sql! jbuilder9运行项目的问题 谁有用jsp做的电子商务的论文? 请大侠们帮帮忙,关于jsp中调用javabean的问题。 Questions about JavaBean 高手请进,咋么设置才能通过邮件服务器验证! 类型转换问题
我是新手最好具体一点。
谢谢!
指定xml文件-〉使用jdom打开该xml文件-〉读取表名及各个字段的信息-〉组装成对应的sql语句-〉使用jdbc建立数据库连接-〉执行该sql语句-〉使用fileiostream建立.java文件-〉将需要的java代码写入该文件-〉end
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<publish_date>
<year>2000</year>
<month>10</month>
<day>1</day>
</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<publish_date>
<year>2000</year>
<month>12</month>
<day>16</day>
</publish_date>
<description>A former architect battles corporate zombies.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<publish_date>
<year>2000</year>
<month>11</month>
<day>17</day>
</publish_date>
<description>After the collapse of a nanotechnology society in England.</description>
</book>
</catalog>使用DOM解析 --BookParser.javaimport javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;/**
*使用DOM提取XML内容的例子
*/
class BookParser{
public static void main(String[] args){
try{
//获取一个XML解析器
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
//解析XML文件
Document document=builder.parse(new File("books.xml"));
//去掉XML文档中空白部分
document.normalize();
//获取根节点并打印根节点的名称
Element root=document.getDocumentElement();
System.out.println("根原始的名称:"+root.getTagName());
//获取所有的"book"标记,它是一个NodeList对象
NodeList books=root.getElementsByTagName("book");
//遍历NodeList
System.out.println("书本列表");
for(int i=0;i<books.getLength();i++){
//获取books中的每一个元素
Element book=(Element)books.item(i);
//获取每个"book"标记的"id"属性
String book_id=book.getAttribute("id");//或book.getAttributeNode("id").getValue();
System.out.println("序号:"+book_id);
//获取"book"标记下的每个元素
System.out.print("作者:");
System.out.println(book.getElementsByTagName("author").item(0).getFirstChild().getNodeValue());
System.out.print("标题:");
System.out.println(book.getElementsByTagName("title").item(0).getFirstChild().getNodeValue());
//获取"publish_date"元素
Element publishDate=(Element)book.getElementsByTagName("publish_date").item(0);
//获取"publish_date"元素下的每个子元素
String year=publishDate.getElementsByTagName("year").item(0).getFirstChild().getNodeValue();
String month=publishDate.getElementsByTagName("month").item(0).getFirstChild().getNodeValue();
String day=publishDate.getElementsByTagName("day").item(0).getFirstChild().getNodeValue();
System.out.println("出版日期:"+year+"年"+month+"月"+day+"日");
System.out.print("描述:");
System.out.println(book.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
System.out.println("--------------------");
}
}
catch(Exception e2){}
}
}
zxhong(红透半边天)、jcq(疯子弟)、 kreven(天地无用恨离别) 等!
我刚接触到这部分知识,在大家的热心帮助下,我会学好的!
再次,谢谢大家的帮助,特别是“zxhong(红透半边天)”!