利用JAXP解析xml中遇到的问题

03-06-16 genner

  目的:想利用一个xml文件对数据库进行描述,同时利用JAXP这个包来解析这样一个文件,最后生成一个相应的建表sql语句

 1。xml文件结构如下:

<xml version="1.0" encoding="utf-8"?>

<db>

<table name="db_user">

<field name="host" type="char" length="60"/>

<field name="user" type="char" length="16"/>

<field name="password" type="char" length="16"/>

</table>

<table name="db_host">

<field name="host" type="char" length="60"/>

<field name="db" type="char" length="64"/>

</table>

</db>

2。所要求生成的对应结果如下:

   1. create table db_user(

name char(14) null,

user char(16) null,

password char(16) null);

2 create table db_host(

name char(60) null,

db char(64) null);

3。写的java解析代码如下:

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import java.io.IOException;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

import org.w3c.dom.Node;

import org.w3c.dom.NamedNodeMap;

import java.io.File;

public class BuildDBTables{

private Document document = null;

private DocumentBuilderFactory dbf = null;

private DocumentBuilder db = null;

public void BuildDBTables(){

}

public String[] parse() throws Exception{

dbf = DocumentBuilderFactory.newInstance();

db = dbf.newDocumentBuilder();

document = db.parse(new File("db.xml"));

String[] k = walkNode();

return k;

}

private String[] walkNode() throws Exception{

NodeList tables = document.getElementsByTagName("table");

// get string array of building tables

String[] buildTableStringArray = new String[1024];

for(int i = 0; i < tables.getLength(); i++){

Node aNode = tables.item(i);

//get table name

String tableName = null;

NamedNodeMap attrs = aNode.getAttributes();

for(int a = 0; a <attrs.getLength(); a++){

Node theAttr = attrs.item(a);

tableName = theAttr.getNodeValue();

System.out.println(tableName);

}

// get fileds attribute

StringBuffer allFieldStrBuff = new StringBuffer();

 //好像是这儿出了问题,不知是什么原因!

if(aNode.hasChildNodes()){

NodeList fields = aNode.getChildNodes();

for(int a = 0; a < fields.getLength(); a++){

Node bNode = fields.item(i);

//这是我打印的<table>下各子节点的名及值

System.out.println(bNode.getNodeName()+" = "+bNode.getNodeValue());

                  

}

//get a field attribute

/*

String andFlag = " ";

for(int a = 0; a < fields.getLength(); a++){

Node bNode = fields.item(i);

NamedNodeMap attrs2 = bNode.getAttributes();

StringBuffer aFieldStrBuff = new StringBuffer();

for(int b = 0; b < attrs2.getLength(); b++){

Node theAttr = attrs2.item(b);

String nodeValue = theAttr.getNodeValue();

System.out.println(theAttr.getNodeName()+" = " + theAttr.getNodeValue());

// aFieldStrBuff.append(nodeValue+" ");

}

// String tmpString = aFieldStrBuff.toString();

// andFlag = ",";

// allFieldStrBuff.append(andFlag+"\n"+tmpString);

}

*/

}

String fieldString = allFieldStrBuff.toString();

String buildTableString = " create table "+tableName+ "("+fieldString+")";

System.out.println(buildTableString);

buildTableStringArray = buildTableString;

}

return buildTableStringArray;

}

public static void main(String[] args) throws Exception{

BuildDBTables dtable = new BuildDBTables();

String[] tables = dtable.parse();

/*

for(int i = 0; i < tables.length; i++)

{

if(tables!=null)

System.out.println(tables);

}

*/

}

}

4。代码生成的结果如下:

db_user

#text =

#text =

#text =

#text =

#text =

#text =

#text =

create table db_user()

db_host

field = null

field = null

field = null

field = null

field = null

create table db_host()

5.疑问有:

 1)为什么没有返回 table子节点下field的各个属性名及值

 2)相同的节点描述,为什么<table name="db_user">会返回"#text", 而<table naem="db_host">会返回field =null

 希望各们大哥给以指点指点!

genner
2003-06-16 16:01

真对不起,在这儿占用了大量的版面!误导了大家,造成错误的原因是我在循环时,将变量a取成了变量i!