您的位置:首页 > 编程语言 > Java开发

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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: