java jdom 解析xml
2014-10-14 16:02
357 查看
package com.xxxx.service;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.List;
import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
/**
* 读CPE文件:official-cpe-dictionary_v2.3.xml
* 解析xml,把cpe数据存入数据库
* *
*/
public class ImportCpeDictionaryFromXml {
/*
*
* <?xml version='1.0' encoding='UTF-8'?>
<cpe-list xmlns:meta="http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2" xmlns:config="http://scap.nist.gov/schema/configuration/0.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cpe-23="http://scap.nist.gov/schema/cpe-extension/2.3"
xmlns:scap-core="http://scap.nist.gov/schema/scap-core/0.3" xmlns:ns6="http://scap.nist.gov/schema/scap-core/0.1"
xmlns="http://cpe.mitre.org/dictionary/2.0" xsi:schemaLocation="http://scap.nist.gov/schema/configuration/0.1
http://nvd.nist.gov/schema/configuration_0.1.xsd
http://cpe.mitre.org/dictionary/2.0
http://scap.nist.gov/schema/cpe/2.3/cpe-dictionary_2.3.xsd http://scap.nist.gov/schema/scap-core/0.3 http://nvd.nist.gov/schema/scap-core_0.3.xsd http://scap.nist.gov/schema/scap-core/0.1 http://nvd.nist.gov/schema/scap-core_0.1.xsd http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2 http://nvd.nist.gov/schema/cpe-dictionary-metadata_0.2.xsd http://scap.nist.gov/schema/cpe-extension/2.3 http://scap.nist.gov/schema/cpe/2.3/cpe-dictionary-extension_2.3.xsd">
<cpe-item name="cpe:/a:1024cms:1024_cms:0.7">
<title xml:lang="en-US">1024cms.org 1024 CMS 0.7</title>
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:0.7:*:*:*:*:*:*:*"/>
</cpe-item>
<cpe-item name="cpe:/a:1024cms:1024_cms:1.2.5">
<title xml:lang="en-US">1024cms.org 1024 CMS 1.2.5</title>
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.2.5:*:*:*:*:*:*:*"/>
</cpe-item>
<cpe-item name="cpe:/a:1024cms:1024_cms:1.3.1">
<title xml:lang="en-US">1024cms.org 1024 CMS 1.3.1</title>
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.3.1:*:*:*:*:*:*:*"/>
</cpe-item>
<cpe-item name="cpe:/a:1024cms:1024_cms:1.4.1">
<title xml:lang="en-US">1024cms.org 1024 CMS 1.4.1</title>
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.4.1:*:*:*:*:*:*:*"/>
</cpe-item>
</cpe-list>
*
*/
public boolean readXml() {
//读xml文件,返回文件内容字符串
String fileStr = readFile("D:/xxx项目/xxx/official-cpe-dictionary_v2.3_bak.xml");
StringReader read = new StringReader(fileStr);
SAXBuilder sb = new SAXBuilder(false);
Document doc;
try {
doc = (Document) sb.build(read);
Element rootElement = doc.getRootElement();
// Element cpeItem = (element).getChild("cpe-item");
List<Element> cpeItemList = (rootElement).getChildren();
//循环<cpe-item>节点
for (Element element : cpeItemList) {
//<cpe-item>节点的name属性
String cpe_item_name = element.getAttributeValue("name");
// System.out.println(cpe_item_name);
// <cpe-item>节点包含的内容是List<Content>,可以通过调试进行跟踪一下,看看List<Content>的内容,然后就知道如何进行遍历操作了
List<Content> contentList = element.getContent();
for (Content content : contentList) {
if (content instanceof Element) {
Element e = (Element) content;
if("title".equals(e.getName())){
String title = e.getText();
// System.out.println(title);
}
}
}
}
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
/**
* 读文件,根据文件名,返回文件内容字符串
*/
public String readFile(String filePath){
StringBuffer sb = new StringBuffer();
try {
String encoding = "UTF-8";
File file = new File(filePath);
//判断文件是否存在
if(file.isFile() && file.exists()){
InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
// System.out.println(lineTxt);
sb.append(lineTxt).append("\r\n");
}
read.close();
}else{
System.out.println(sb.append("找不到指定的文件:" + filePath));
}
} catch (Exception e) {
System.out.println(sb.append("读取文件内容出错"));
e.printStackTrace();
}
return sb.toString();
}
public static void main(String[] args) {
boolean b = new ImportCpeDictionaryFromXml().readXml();
// System.out.println(b);
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.List;
import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
/**
* 读CPE文件:official-cpe-dictionary_v2.3.xml
* 解析xml,把cpe数据存入数据库
* *
*/
public class ImportCpeDictionaryFromXml {
/*
*
* <?xml version='1.0' encoding='UTF-8'?>
<cpe-list xmlns:meta="http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2" xmlns:config="http://scap.nist.gov/schema/configuration/0.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cpe-23="http://scap.nist.gov/schema/cpe-extension/2.3"
xmlns:scap-core="http://scap.nist.gov/schema/scap-core/0.3" xmlns:ns6="http://scap.nist.gov/schema/scap-core/0.1"
xmlns="http://cpe.mitre.org/dictionary/2.0" xsi:schemaLocation="http://scap.nist.gov/schema/configuration/0.1
http://nvd.nist.gov/schema/configuration_0.1.xsd
http://cpe.mitre.org/dictionary/2.0
http://scap.nist.gov/schema/cpe/2.3/cpe-dictionary_2.3.xsd http://scap.nist.gov/schema/scap-core/0.3 http://nvd.nist.gov/schema/scap-core_0.3.xsd http://scap.nist.gov/schema/scap-core/0.1 http://nvd.nist.gov/schema/scap-core_0.1.xsd http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2 http://nvd.nist.gov/schema/cpe-dictionary-metadata_0.2.xsd http://scap.nist.gov/schema/cpe-extension/2.3 http://scap.nist.gov/schema/cpe/2.3/cpe-dictionary-extension_2.3.xsd">
<cpe-item name="cpe:/a:1024cms:1024_cms:0.7">
<title xml:lang="en-US">1024cms.org 1024 CMS 0.7</title>
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:0.7:*:*:*:*:*:*:*"/>
</cpe-item>
<cpe-item name="cpe:/a:1024cms:1024_cms:1.2.5">
<title xml:lang="en-US">1024cms.org 1024 CMS 1.2.5</title>
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.2.5:*:*:*:*:*:*:*"/>
</cpe-item>
<cpe-item name="cpe:/a:1024cms:1024_cms:1.3.1">
<title xml:lang="en-US">1024cms.org 1024 CMS 1.3.1</title>
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.3.1:*:*:*:*:*:*:*"/>
</cpe-item>
<cpe-item name="cpe:/a:1024cms:1024_cms:1.4.1">
<title xml:lang="en-US">1024cms.org 1024 CMS 1.4.1</title>
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.4.1:*:*:*:*:*:*:*"/>
</cpe-item>
</cpe-list>
*
*/
public boolean readXml() {
//读xml文件,返回文件内容字符串
String fileStr = readFile("D:/xxx项目/xxx/official-cpe-dictionary_v2.3_bak.xml");
StringReader read = new StringReader(fileStr);
SAXBuilder sb = new SAXBuilder(false);
Document doc;
try {
doc = (Document) sb.build(read);
Element rootElement = doc.getRootElement();
// Element cpeItem = (element).getChild("cpe-item");
List<Element> cpeItemList = (rootElement).getChildren();
//循环<cpe-item>节点
for (Element element : cpeItemList) {
//<cpe-item>节点的name属性
String cpe_item_name = element.getAttributeValue("name");
// System.out.println(cpe_item_name);
// <cpe-item>节点包含的内容是List<Content>,可以通过调试进行跟踪一下,看看List<Content>的内容,然后就知道如何进行遍历操作了
List<Content> contentList = element.getContent();
for (Content content : contentList) {
if (content instanceof Element) {
Element e = (Element) content;
if("title".equals(e.getName())){
String title = e.getText();
// System.out.println(title);
}
}
}
}
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
/**
* 读文件,根据文件名,返回文件内容字符串
*/
public String readFile(String filePath){
StringBuffer sb = new StringBuffer();
try {
String encoding = "UTF-8";
File file = new File(filePath);
//判断文件是否存在
if(file.isFile() && file.exists()){
InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
// System.out.println(lineTxt);
sb.append(lineTxt).append("\r\n");
}
read.close();
}else{
System.out.println(sb.append("找不到指定的文件:" + filePath));
}
} catch (Exception e) {
System.out.println(sb.append("读取文件内容出错"));
e.printStackTrace();
}
return sb.toString();
}
public static void main(String[] args) {
boolean b = new ImportCpeDictionaryFromXml().readXml();
// System.out.println(b);
}
}
相关文章推荐
- JDOM是JAVA中最常用的一种解析XML的工具
- xml解析(jdom java)
- Java使用JDOM解析XML(转载,简单又详细)
- Java用jdom.jar解析读取XML文件信息
- Java解析XML之四(JDOM篇)
- JAVA对XML的几种解析方法讲解(JDOM)
- Java程序员从笨鸟到菜鸟之(二十七)XML之Jdom和DOM4J解析 .
- java jdom解析和创建xml
- JAVA JDom解析XML
- Java XML解析工具 JDOM介绍及使用实例
- Java中四种XML解析技术对比(Dom、Sax、Dom4j JDom)
- Java程序员从笨鸟到菜鸟之(二十七)XML之Jdom和DOM4J解析 .
- jdom解析xml时候报java.lang.ClassNotFoundException: org.jdom.JDOMException
- Java:XML篇,使用JDOM读取并解析XML
- java JDOM解析XML字符串(非XML文档)
- java解析xml方法:DOM解析、SAX解析、JDOM解析、DOM4J解析
- java使用jdom架包解析xml字符串dom原理并保存map对象
- java解析xml(JDOM)配置环境
- Java程序员从笨鸟到菜鸟之(二十七)XML之Jdom和DOM4J解析 .
- Java 用 JDom 解析 XML 配置文件