不说废话直接上代码,下面是要读取的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.Fileimport 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()
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<config>
<db-info id="01">
<!—— jdbc链接Oracle数据库 ——>
<orcldriver-name>oracle.jdbc.driver.OracleDriver</orcldriver-name>
<orclurl>jdbc:oracle:thin:@111.111.1.11:1521/orcl</orclurl>
<orclusername>201</orclusername>
<orclpassword>300</orclpassword>
<orclmandt>006</orclmandt>
<orcldays>10</orcldays><!-- 查几天前到现在的数据 -->
<orclpasesize>10</orclpasesize><!-- 每页显示数 -->
<orclrefreshTime>6</orclrefreshTime><!--翻页间隔 -->
<orclpageRefresh>10</orclpageRefresh><!--页面刷新 -->
</db-info>
<!—— jdbc链接SQL server数据库 ——>
<db-info id="00">
<driver-name>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-name>
<url>jdbc:sqlserver://222.222.0.222:1433DatabaseName=1111</url>
<username>b</username>
<password>b</password>
</db-info>
</config>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)