java怎么把解析的xml数据添加到数据库中

java怎么把解析的xml数据添加到数据库中,第1张

不说废话直接上代码,下面是要读取的xml文件

<?xml version="1.0" encoding="UTF-8"?>

<PERSON>

  <NAME>a1</NAME>

  <ADDRESS>b1</ADDRESS>

  <TEL>c1</TEL>

  <FAX>d1</FAX>

  <EMAIL>e1</EMAIL>

</PERSON>

下面是读取xml文件的java代码,在数据库中建一张和xml文件字段对应的表,将读取的数据插入到表中。如果数据插入不会,欢迎继续追问:

import java.io.File

import javax.xml.parsers.DocumentBuilder

import javax.xml.parsers.DocumentBuilderFactory

import org.w3c.dom.Document

import org.w3c.dom.Element

import org.w3c.dom.NodeList

public class Test {

public static void main(String[] args) throws Exception{

System.out.println("22")

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance()

DocumentBuilder db = dbf.newDocumentBuilder()

Document document = db.parse(new File("candidate.xml"))

NodeList list = document.getElementsByTagName("PERSON")

for(int i = 0 i < list.getLength() i++)

{

Element element = (Element)list.item(i)

String content = element.getElementsByTagName("NAME").item(0).getFirstChild().getNodeValue()

            content = element.getElementsByTagName("ADDRESS").item(0).getFirstChild().getNodeValue()

            content = element.getElementsByTagName("TEL").item(0).getFirstChild().getNodeValue()

            content = element.getElementsByTagName("FAX").item(0).getFirstChild().getNodeValue()

            content = element.getElementsByTagName("EMAIL").item(0).getFirstChild().getNodeValue()

        }

    }

}

楼主你可以 用 SAXParser,Document 解析XML 然后把得到的数据拼成sql语句插入 数据库就可以了例如:public class ReaderXML extends DefaultHandler { @Override,

我给你个解析的代码吧~具体存数据库得根据实际情况来

#include "StdAfx.h"

#include "parse.h"

#include <string>

#include "stdafx.h"

#include <libxml/parser.h>

#include <libxml/tree.h>

#include <iostream>

#include <libxml/xpath.h>

#include <libxml/xpathInternals.h>

#include <stdio.h>

#include <stdlib.h>

#include<windows.h>

using namespace std

parse::parse()

//int parse::parsexml()

{

xmlDocPtr doc //定义解析文档指针

xmlNodePtr curNode //定义结点指针(你需要它为了在各个结点间移动)

xmlChar *szKey //临时字符串变量

char *szDocName

doc = xmlReadFile("SimACQ_Config.xml","GB2312",XML_PARSE_RECOVER)//解析文件

//检查解析文档是否成功,如果不成功,libxml将指一个注册的错误并停止。

if (NULL==doc)

{

fprintf(stderr,"Document not parsed successfully. /n")

/*return -1*/

}

curNode = xmlDocGetRootElement(doc)//确定文档根元素

/*检查确认当前文档中包含内容*/

if (NULL == curNode)

{

fprintf(stderr,"empty document/n")

xmlFreeDoc(doc)

/*return -1*/

}

/*在这个例子中,我们需要确认文档是正确的类型。“root”是在这个示例中使用文档的根类型。*/

if (xmlStrcmp(curNode->name, BAD_CAST "SIMCONFIG"))

{

fprintf(stderr,"document of the wrong type, root node != mail")

xmlFreeDoc(doc)

/*return -1*/

}

curNode = curNode->xmlChildrenNode

xmlNodePtr propNodePtr = curNode

while(curNode != NULL)

{

//取出节点中的内容

if ((!xmlStrcmp(curNode->name, (const xmlChar *)"ComConfig")))

{

xmlNodePtr comConfigPtr= curNode->children

while(comConfigPtr!=NULL)

{

if((!xmlStrcmp(comConfigPtr->name,(const xmlChar *)"DMS")))

{

xmlChar* szAttr = xmlGetProp(comConfigPtr,BAD_CAST "IP")

IP=(char*)szAttr

szAttr=xmlGetProp(comConfigPtr,BAD_CAST "PORT")

PORT=atoi((const char *)szAttr)

szAttr=xmlGetProp(comConfigPtr,BAD_CAST "TIMEOUT")

TIMEOUT=atoi((const char *)szAttr)

xmlFree(szAttr)

}

comConfigPtr=comConfigPtr->next

}

}

if ((!xmlStrcmp(curNode->name, (const xmlChar *)"Log")))

{

xmlChar* szAttr = xmlGetProp(curNode,BAD_CAST "Flag")

if(szAttr!=NULL)

{

if((!xmlStrcmp(szAttr,(const xmlChar *)"True")))

{

FLAG=true

}

else

{

FLAG=false

}

}

szAttr = xmlGetProp(curNode,BAD_CAST "Path")

if(szAttr!=NULL)

{

PATH=(char*)szAttr

}

xmlFree(szAttr)

}

if ((!xmlStrcmp(curNode->name, (const xmlChar *)"DMS")))

{

xmlChar* szAttr = xmlGetProp(curNode,BAD_CAST "Cache")

if(szAttr!=NULL)

{

Cache=atoi((const char *)szAttr)

}

xmlFree(szAttr)

}

if ((!xmlStrcmp(curNode->name, (const xmlChar *)"SimFile")))

{

xmlChar* szAttr = xmlGetProp(curNode,BAD_CAST "Type")

if(szAttr!=NULL)

{

if((!xmlStrcmp(szAttr,(const xmlChar *)"PlainFilm")))

{

xmlNodePtr FileNodes=curNode->children

int i=0

while(FileNodes!=NULL)

{

if((!xmlStrcmp(FileNodes->name,(const xmlChar *)"FILE")))

{

szAttr = xmlGetProp(FileNodes,BAD_CAST "name")

if(szAttr!=NULL)

{

/*SIM_PLAIN[i].Name=(char*)szAttr*/

strcpy(simulation.SIM_PLAIN[i].Name,(char*)szAttr)

}

szAttr=xmlGetProp(FileNodes,BAD_CAST "sliceNum")

if(szAttr!=NULL)

{

simulation.SIM_PLAIN[i].Num=atoi((char*)szAttr)

}

i++

}

FileNodes=FileNodes->next

}

xmlFree(FileNodes)

simulation.SIM_PLAIN[i].flag=0

}

if((!xmlStrcmp(szAttr,(const xmlChar *)"Spiral"))){

xmlNodePtr FileNodes=curNode->children

int i=0

while(FileNodes!=NULL)

{

if((!xmlStrcmp(FileNodes->name,(const xmlChar *)"FILE")))

{

szAttr = xmlGetProp(FileNodes,BAD_CAST "name")

if(szAttr!=NULL)

{

/*SIM_SPIRAL[i].Name=(char*)szAttr*/

strcpy(simulation.SIM_SPIRAL[i].Name,(char*)szAttr)

}

szAttr=xmlGetProp(FileNodes,BAD_CAST "sliceNum")

if(szAttr!=NULL)

{

simulation.SIM_SPIRAL[i].Num=atoi((char*)szAttr)

}

i++

}

FileNodes=FileNodes->next

}

xmlFree(FileNodes)

simulation.SIM_SPIRAL[i].flag=0

}

if((!xmlStrcmp(szAttr,(const xmlChar *)"axial")))

{

xmlNodePtr FileNodes=curNode->children

int i=0

while(FileNodes!=NULL)

{

if((!xmlStrcmp(FileNodes->name,(const xmlChar *)"FILE")))

{

szAttr = xmlGetProp(FileNodes,BAD_CAST "name")

if(szAttr!=NULL)

{

/* SIM_AXIAL[i].Name=(char*)szAttr*/

strcpy(simulation.SIM_AXIAL[i].Name,(char*)szAttr)

}

szAttr=xmlGetProp(FileNodes,BAD_CAST "sliceNum")

if(szAttr!=NULL)

{

simulation.SIM_AXIAL[i].Num=atoi((char*)szAttr)

}

i++

}

FileNodes=FileNodes->next

}

xmlFree(FileNodes)

simulation.SIM_AXIAL[i].flag=0

}

}

xmlFree(szAttr)

}

curNode = curNode->next

}

xmlFreeDoc(doc)

/*return 0*/

}

有什么不明白的再问吧!


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6707358.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-27
下一篇2023-03-27

发表评论

登录后才能评论

评论列表(0条)

    保存