利用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對xml文件進行DOM解析基礎XML
- flash中呼叫XML遇到的中文顯示異常問題XML
- jaxp的sax解析操作
- 使用jaxp解析器dom方式對xml節點進行操作XML
- laravel使用中遇到的問題Laravel
- Hodoop配置中遇到的問題OdooOOP
- 工作中遇到的問題
- javaweb中自己遇到的問題JavaWeb
- loadrunner學習中遇到的問題
- @UpdateProvider註解中遇到的問題IDE
- weex學習中遇到的問題
- kafka 運維中遇到的問題Kafka運維
- 【專案中遇到的zookeeper的問題】
- 實際專案中遇到的問題
- 安裝 Laravel Mix 中遇到的問題Laravel
- Vue使用中遇到的程式碼問題Vue
- 爬蟲過程中遇到的問題爬蟲
- 關於工作中遇到的問題
- SwitchResX自定義解析度時遇到的問題彙總
- linux遇到的問題Linux
- Vagrant 遇到的問題
- Homestead 遇到的問題
- sudo 遇到的問題
- JackJson遇到的問題JSON
- mysql 遇到的問題MySql
- WangEditor遇到的問題
- vue專案中遇到的問題總結Vue
- weex中UISegmentControl實現及遇到的問題UI
- css中經遇到的文字換行問題CSS
- React開發中遇到的問題總結React
- 面試中遇到的一些問題面試
- 學習Java中遇到的繼承問題Java繼承
- 學習vue過程中遇到的問題Vue
- 橫趟!面試中遇到的 ZooKeeper 問題面試
- python中安裝qtdesigner、pyuic遇到的問題PythonQTUI
- 關於 Android studio 在xml中不提示的問題AndroidXML
- 使用git遇到的問題Git
- SpringBoot遇到的某些問題Spring Boot
- 我遇到的小白問題