利用JAXP解析xml中遇到的問題

genner發表於2003-06-16
  目的:想利用一個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
 希望各們大哥給以指點指點!

相關文章