java jdom 解析xml

本文介绍了一个Java程序,该程序用于读取并解析官方CPE字典XML文件(official-cpe-dictionary_v2.3.xml),提取其中的CPE条目信息,并准备将这些数据存储到数据库中。解析过程涉及使用JDOM库来处理XML结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值