⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parserxml.java~11~

📁 一个解析xml到数据表的程序
💻 JAVA~11~
字号:
package xmltotable;import java.io.File;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import javax.xml.parsers.DocumentBuilder;import org.xml.sax.SAXParseException;import org.xml.sax.SAXException;import java.io.IOException;import java.util.LinkedList;import java.util.List;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.xml.sax.Attributes;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class ParserXml {  File file;  DocumentBuilderFactory factory ;  public ParserXml() {   factory = DocumentBuilderFactory.newInstance();  }  public Document parserFile(File file)  {    Document document;    List list;    try {      DocumentBuilder builder = factory.newDocumentBuilder();      document = builder.parse( file );      return document;    } catch (SAXException spe) {      spe.printStackTrace();    }    catch(IOException ex)    {      ex.printStackTrace();    }    catch(Exception exe)    {      exe.printStackTrace();    }    return null;  }  public List getDbFrDoc(Document doc)  {    LinkedList rBeacon=new LinkedList();    Element root=doc.getDocumentElement();    NodeList nl=root.getChildNodes();    System.out.println("root:"+root.getNodeName()+" chlid length:"+nl.getLength());    for(int i=0;i<nl.getLength();i++)    {      Element databaseEle=(Element)nl.item(i+1);      System.out.println("dbName:"+databaseEle.getNodeName());      if(databaseEle.getTagName().equals("database"))      {        LinkedList llist=getTablesList(databaseEle);        rBeacon.add(llist);      }    }    return rBeacon;  }  public LinkedList getTablesList(Element dbEle)  {    LinkedList tablesList=new LinkedList();    NodeList nl=dbEle.getChildNodes();    for(int i=0;i<nl.getLength();i++)    {      Element tableEle=(Element)nl.item(i+1);      LinkedList tableProList=getTableProList(tableEle);      tablesList.add(tableProList);    }    return tablesList;  }  public LinkedList getTableProList(Element tableEle)  {    LinkedList tableProList=new LinkedList();    NamedNodeMap nl=tableEle.getAttributes();    for(int i=0;i<nl.getLength();i++)    {      Node attrNode=nl.item(i);      String colName=attrNode.getNodeValue();      tableProList.add(colName);    }    return tableProList;  }  //public parseFile()  public static void main(String[] args) {  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -