利用JAXP解析xml中遇到的問題
目的:想利用一個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);
}
*/
}
}
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
希望各們大哥給以指點指點!
相關文章
- jaxp程式問題sos
- 使用JAXP對xml文件進行DOM解析基礎XML
- 解析xml 成bool的問題XML
- jaxp的sax解析操作
- 面試中遇到的問題面試
- 使用jaxp解析器dom方式對xml節點進行操作XML
- javaweb中自己遇到的問題JavaWeb
- laravel使用中遇到的問題Laravel
- Hodoop配置中遇到的問題OdooOOP
- 工作中遇到的問題
- 關於SAX解析xml檔案的問題XML
- 利用Perl解析XML檔案XML
- 使用JAXP進行DOM解析_SAX解析
- kafka 運維中遇到的問題Kafka運維
- weex學習中遇到的問題
- hive學習中遇到的問題Hive
- 【專案中遇到的zookeeper的問題】
- 學習中遇到的javabean中的scope問題JavaBean
- 關於工作中遇到的問題
- loadrunner學習中遇到的問題
- 實際專案中遇到的問題
- 橫趟!面試中遇到的 ZooKeeper 問題面試
- @UpdateProvider註解中遇到的問題IDE
- 安裝 Laravel Mix 中遇到的問題Laravel
- Vue使用中遇到的程式碼問題Vue
- Linux ~ CentOS使用中遇到的問題LinuxCentOS
- MERGE開發中遇到的問題
- 面試中遇到的一些問題面試
- CKEditor使用中遇到的問題解決
- Fragstas軟體使用中遇到的問題
- Hadoop與HBase中遇到的問題Hadoop
- 安裝mysql中遇到的問題1MySql
- 爬蟲過程中遇到的問題爬蟲
- JAXP 專述??Sun 的 Java API for XML 語法分析(轉)JavaAPIXML語法分析
- 有待整理的工作中遇到的問題
- 請教高手關於解析xml檔案的問題 急~~XML
- 學習vue過程中遇到的問題Vue
- 學習Java中遇到的繼承問題Java繼承