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

📄 parserxml.java~17~

📁 一个解析xml到数据表的程序
💻 JAVA~17~
字号:
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;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.Element;
import org.jdom.Attribute;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class ParserXml {

  public ParserXml() {
  }

  public Document parserFile(File file)
  {
    Document document;
    List list;
      //DocumentBuilder builder = factory.newDocumentBuilder();
      //document = builder.parse( file );
      SAXBuilder builder = new SAXBuilder();
      try
      {
        Document doc = builder.build(file);

        return doc;
      }catch(JDOMException ex)
      {
        ex.printStackTrace();
      }
      return null;
  }

  public List getDbFrDoc(Document doc)
  {
    LinkedList rBeacon=new LinkedList();

    Element root=doc.getRootElement();
    List nl=root.getChildren();
    System.out.println("root:"+root.getName()+" chlid length:"+nl.size());

    for(int i=0;i<nl.size();i++)
    {
      Element databaseEle=(Element)nl.get(i);
      System.out.println("dbName:"+databaseEle.getName());
      if(databaseEle.getName().equals("database"))
      {
        LinkedList llist=getTablesList(databaseEle);
        rBeacon.add(llist);
      }
    }
    return rBeacon;
  }


  public LinkedList getTablesList(Element dbEle)
  {
    LinkedList tablesList=new LinkedList();
    List nl=dbEle.getChildren();
    System.out.println("table count:"+nl.size());
    for(int i=0;i<nl.size();i++)
    {
      Element tableEle=(Element)nl.get(i);
      LinkedList tableProList=getTableProList(tableEle);
      tablesList.add(tableProList);
    }
    return tablesList;
  }

  public LinkedList getTableProList(Element tableEle)
  {
    LinkedList tableProList=new LinkedList();
    List nl=tableEle.getAttributes();
    for(int i=0;i<nl.size();i++)
    {
      Attribute attrNode=(Attribute)nl.get(i);
      String colName=attrNode.getValue();
      System.out.println("nodeValue:"+colName);
      tableProList.add(colName);
    }
    return tableProList;
  }
  //public parseFile()
  public static void main(String[] args) {
  }

}

⌨️ 快捷键说明

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