最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java + dom4j.jar提取xml文檔內容

 更新時間:2019年08月30日 10:27:12   作者:靜遠小和尚  
這篇文章主要為大家詳細介紹了java + dom4j.jar提取xml文檔內容,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java + dom4j.jar提取xml文檔內容的具體代碼,供大家參考,具體內容如下

資源下載頁:點擊下載

本例程主要借助幾個遍歷的操作對xml格式下的內容進行提取,操作不是最優(yōu)的方法,主要是練習使用幾個遍歷操作。

xml格式文檔內容:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE nitf SYSTEM "http://www.nitf.org/IPTC/NITF/3.3/specification/dtd/nitf-3-3.dtd"> 
-<nitf version="-//IPTC//DTD NITF 3.3//EN" change.time="19:30" change.date="June 10, 2005">
 
 
-<head>
 
<title>An End to Nuclear Testing</title>
 
<meta name="publication_day_of_month" content="7"/> 
<meta name="publication_month" content="7"/> 
<meta name="publication_year" content="1993"/> 
<meta name="publication_day_of_week" content="Wednesday"/> 
<meta name="dsk" content="Editorial Desk"/> 
<meta name="print_page_number" content="14"/> 
<meta name="print_section" content="A"/> 
<meta name="print_column" content="1"/>
<meta name="online_sections" content="Opinion"/>
 
 
-<docdata>
 
<doc-id id-string="619929"/>
 
<doc.copyright year="1993" holder="The New York Times"/>
 
 
-<identified-content>
 
<classifier type="descriptor" class="indexing_service">ATOMIC WEAPONS</classifier> 
<classifier type="descriptor" class="indexing_service">NUCLEAR TESTS</classifier> 
<classifier type="descriptor" class="indexing_service">TESTS AND TESTING</classifier> 
<classifier type="descriptor" class="indexing_service">EDITORIALS</classifier> 
<person class="indexing_service">CLINTON, BILL (PRES)</person> 
<classifier type="types_of_material" class="online_producer">Editorial</classifier> 
<classifier type="taxonomic_classifier" class="online_producer">Top/Opinion</classifier> 
<classifier type="taxonomic_classifier" class="online_producer">Top/Opinion/Opinion</classifier> 
<classifier type="taxonomic_classifier" class="online_producer">Top/Opinion/Opinion/Editorials</classifier> 
<classifier type="general_descriptor" class="online_producer">Nuclear Tests</classifier> 
<classifier type="general_descriptor" class="online_producer">Atomic Weapons</classifier> 
<classifier type="general_descriptor" class="online_producer">Tests and Testing</classifier> 
<classifier type="general_descriptor" class="online_producer">Armament, Defense and Military Forces</classifier>
 
</identified-content> 
</docdata> 
<pubdata name="The New York Times" unit-of-measure="word" item-length="390" ex-ref="http://query.nytimes.com/gst/fullpage.html?res=9F0CEFDF1439F934A35754C0A965958260" date.publication="19930707T000000"/>
 
</head>
 
 
-<body>
 
 
-<body.head>
 
 
-<hedline>
 
<hl1>An End to Nuclear Testing</hl1>
 
</hedline> 
</body.head>
 
 
-<body.content>
 
 
-<block class="lead_paragraph">
 
<p>For nearly half a century, test explosions in the Nevada desert were a reverberating reminder of cold war insecurity. Now the biggest worry is nuclear proliferation, not the Soviet threat. That's why President Clinton has quietly decided to extend the moratorium on tests of nuclear arms for at least 15 months.</p> 
<p>To persuade nuclear have-nots to stay out of the bomb-making business, it makes more sense to halt testing and try to get others to do likewise than to conduct more demonstrations of America's deterrent power.</p>
 
</block>
 
 
-<block class="full_text">
 
<p>For nearly half a century, test explosions in the Nevada desert were a reverberating reminder of cold war insecurity. Now the biggest worry is nuclear proliferation, not the Soviet threat. That's why President Clinton has quietly decided to extend the moratorium on tests of nuclear arms for at least 15 months.</p>
<p>To persuade nuclear have-nots to stay out of the bomb-making business, it makes more sense to halt testing and try to get others to do likewise than to conduct more demonstrations of America's deterrent power.</p> 
<p>Not that nuclear wannabes will necessarily follow America's lead. Nor will an end to all testing assure an end to bomb-making; states like Pakistan have developed nuclear devices without testing them first.</p>
<p>But calling a halt to U.S. nuclear testing makes it easier for leaders in Russia and France to extend the moratoriums they are now observing and improve the atmosphere for prompt negotiation of a treaty to ban all tests.</p>
<p>That test ban in turn should shore up international support for the 1968 Nonproliferation Treaty, linchpin of efforts to stop the spread of nuclear arms, when it comes up for review in 1995. It will also bolster the backing for tighter controls on exports used in bomb-making.</p>
<p>Mr. Clinton has taken three helpful steps. He has extended the Congressionally mandated moratorium on U.S. tests that was due to expire last week. He has declared that the U.S. will not test unless another nation does so first. And he wants to negotiate a total ban on testing.</p>
<p>But the President also wants the nuclear labs to be prepared for a prompt resumption of warhead safety and reliability tests. This could cost millions of dollars and doesn't make much sense, since in Mr. Clinton's own words, "After a thorough review, my Administration has determined that the nuclear weapons in the United States' arsenal are safe and reliable."</p>
<p>Moreover, preparations for testing can take on a life of their own: 30 years after the Limited Test Ban Treaty put an end to above-ground tests, the U.S. still spends $20 million a year on Safeguard C, a program to keep test sites ready.</p>
<p>American security no longer rests on that sort of eternal nuclear vigilance. Mr. Clinton's moratorium may make America safer than all the tests and preparations for tests that the nuclear labs can dream up.</p>
 
</block>
 
</body.content>
 
</body>
 
</nitf>

提取代碼:

對多文件進行操作,首先遍歷所有文件路徑,存到遍歷器中,然后對遍歷器中的文件路徑進行逐一操作。

package com.njupt.ymh;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
import edu.princeton.cs.algs4.In;
 
/**
 * 返回文件名列表
 * @author 11860
 *
 */
public class SearchFile {
 
 public static List<String> getAllFile(String directoryPath,boolean isAddDirectory) {
  List<String> list = new ArrayList<String>(); // 存放文件路徑
  File baseFile = new File(directoryPath); // 當前路徑
  
  if (baseFile.isFile() || !baseFile.exists()) 
   return list;
  
  File[] files = baseFile.listFiles(); // 子文件
  for (File file : files) {
   if (file.isDirectory()) 
   { 
    if(isAddDirectory) // isAddDirectory 是否將子文件夾的路徑也添加到list集合中
     list.add(file.getAbsolutePath()); // 全路徑
    
    list.addAll(getAllFile(file.getAbsolutePath(),isAddDirectory));
   } 
   else 
   {
    list.add(file.getAbsolutePath());
   }
  }
  return list;
 }
 public static void main(String[] args) {
 
 //SearchFile sFile = new SearchFile();
 List<String> listFile = SearchFile.getAllFile("E:\\huadai", false);
 System.out.println(listFile.size());
 File file = new File(listFile.get(3));
 In in = new In(listFile.get(4));
 while (in.hasNextLine()) {
 String readLine = in.readLine().trim(); // 讀取當前行
 System.out.println(readLine);
 
 }
 System.out.println(file.length());
 
 }
 
}
package com.njupt.ymh;
 
import java.io.File;
import java.util.Iterator;
import java.util.List;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
 
public class NewsPaper {
 int doc_id; // 文章id
 String doc_title; // 文章標題
 String lead_paragraph ; // 文章首段
 String full_text; // 文章內容
 String date; // 文章日期
 public NewsPaper(String xml) {
 doc_id = -1; // 文章id
 doc_title = null; // 文章標題
 lead_paragraph = null; // 文章首段
 full_text = null; // 文章內容
 date = null; // 文章日期
 searchValue(xml);
 }
 
 /**
 * 加載Document文件
 * @param fileName
 * @return Document
 */
 private Document load(String fileName) {
 Document document = null; // 文檔
 SAXReader saxReader = new SAXReader(); // 讀取文件流
 
 try {
 document = saxReader.read(new File(fileName));
 } catch (DocumentException e) {
 e.printStackTrace();
 }
 
 return document;
 }
 
 /**
 * 獲取Document的根節(jié)點
 * @param args
 */
 private Element getRootNode(Document document) {
 return document.getRootElement();
 }
 
 /**
 * 獲取所需節(jié)點值
 * @param xml
 */
 private void searchValue(String xml) {
 Document document = load(xml);
  Element root = getRootNode(document); // 根節(jié)點 
  
  // 文章日期
  date = xml.substring(10, 20);
  // 文章標題
  doc_title = root.valueOf("http://head/title");
  
  // 文章-id
  List<Node> list_doc_id = document.selectNodes("http://doc-id/@id-string"); 
  for(Node ele:list_doc_id){
   doc_id = Integer.parseInt(ele.getText());
  }
  
  // 文章內容
  for (Iterator<Element> i = root.elementIterator(); i.hasNext();) { 
   Element el = (Element) i.next(); // head、body
   
   // 對body節(jié)點進行操作
   if (el.getName() == "body") { // body
    for (Iterator<Element> body = el.elementIterator(); body.hasNext();) {
  Element elbody = body.next();
  
  if (elbody.getName() == "body.content") { //body.content
  for (Iterator<Element> block = elbody.elementIterator(); block.hasNext();) {
  Element block_class = (Element) block.next();
  
  if (block_class.attributeValue("class").equals("full_text") ) { // full_text
  List<Node> list_text = block_class.selectNodes("p");
  for (Node text : list_text) 
   if (full_text == null) 
   full_text = text.getStringValue();
   else 
   full_text = full_text +" " + text.getStringValue();
  }
  
  else { // lead_paragraph
  List<Node> list_lead = block_class.selectNodes("p");
  for (Node lead : list_lead) 
   if (lead_paragraph == null)
   lead_paragraph = lead.getStringValue();
   else 
   lead_paragraph = lead_paragraph +" "+ lead.getStringValue();
  }
  }
  }
 }
   }
  } 
 }
 
 /**
 * 獲取文章標題
 * @param args
 */
 public String getTitle() {
 return doc_title;
 }
 
 /**
 * 獲取文章id
 * @param args
 */
 public int getID() {
 return doc_id;
 }
 
 /**
 * 獲取文章簡介
 * @param args
 */
 public String getLead() {
 if (getID() < 394070 && lead_paragraph != null && lead_paragraph.length() > 6)  //1990-10-22之前
 return lead_paragraph.substring(6);
 else       //1990-10-22之后
 return lead_paragraph;
 }
 
 /**
 * 獲取文章正文
 * @param args
 */
 public String getfull() {
 if (getID() < 394070 && full_text != null && full_text.length() > 6)   //1990-10-22之前
 return full_text.substring(6);
 else
 return full_text;
 }
 
 /**
 * 獲取文章日期
 * @param args
 */
 public String getDate() {
 return date;
 }
 
 /**
 * 判斷獲取的信息是否有用
 * @return
 */
 public boolean isUseful() {
 if (getID() == -1)
 return false;
 if (getDate() == null ) 
 return false;
 if (getTitle() == null || getTitle().length() >= 255) 
 return false;
 if (getLead() == null || getLead().length() >= 65535 ) 
 return false;
 if (getfull() == null || getfull().length() >= 65535) 
 return false;
 
 return !isnum();
 }
 
 /**
 * 挑出具有特殊開頭的數字內容文章
 * @return
 */
 private boolean isnum() {
 if (getfull() != null && getfull().length() > 24) {
 if (getfull().substring(0, 20).contains("*3*** COMPANY REPORT") ) { // 剔除數字文章 
 return true;
 }
 }
 return false;
 }
 
 
 public static void main(String[] args) {
 List<String> listFile = SearchFile.getAllFile("E:\\huadai\\1989\\10", false); // 文件列表
 //String date; // 日期
 int count = 0;
 int i = 0;
 for (String string : listFile) {
 NewsPaper newsPaper = new NewsPaper(string);
 count++;
 if (!newsPaper.isUseful()) {
 i++;
 System.out.println(newsPaper.getLead());
 } 
 }
 
 System.out.println(i + " "+ count);
 
 }
}

 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • JAVA大作業(yè)之圖書管理系統(tǒng)實現全解

    JAVA大作業(yè)之圖書管理系統(tǒng)實現全解

    隨著網絡技術的高速發(fā)展,計算機應用的普及,利用計算機對圖書館的日常工作進行管理勢在必行,本篇文章手把手帶你用Java實現一個圖書管理系統(tǒng),大家可以在過程中查缺補漏,提升水平
    2022-01-01
  • 一篇文章弄懂Java8中的時間處理

    一篇文章弄懂Java8中的時間處理

    Java8以前Java處理日期、日歷和時間的方式一直為社區(qū)所詬病,將 java.util.Date設定為可變類型,以及SimpleDateFormat的非線程安全使其應用非常受限,下面這篇文章主要給大家介紹了關于Java8中時間處理的相關資料,需要的朋友可以參考下
    2022-01-01
  • SpringBoot2自動裝配原理解析

    SpringBoot2自動裝配原理解析

    這篇文章主要介紹了SpringBoot2自動裝配原理解析,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • 探討Java 將Markdown文件轉換為Word和PDF文檔

    探討Java 將Markdown文件轉換為Word和PDF文檔

    這篇文章主要介紹了Java 將Markdown文件轉換為Word和PDF文檔,本文通過分步指南及代碼示例展示了如何將 Markdown 文件轉換為 Word 文檔和 PDF 文件,需要的朋友可以參考下
    2024-07-07
  • 深入理解Java虛擬機_動力節(jié)點Java學院整理

    深入理解Java虛擬機_動力節(jié)點Java學院整理

    虛擬機是一種抽象化的計算機,通過在實際的計算機上模擬各種計算機功能來實現的,下面通過本文給大家分享Java虛擬機相關知識,感興趣的朋友一起看看吧
    2017-06-06
  • springboot集成mybatisplus的方法

    springboot集成mybatisplus的方法

    這篇文章主要為大家詳細介紹了springboot集成mybatisplus的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Java二叉樹路徑和代碼示例

    Java二叉樹路徑和代碼示例

    這篇文章主要介紹了Java二叉樹路徑和代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Java初級必看的數據類型與常量變量知識點

    Java初級必看的數據類型與常量變量知識點

    這篇文章主要給大家介紹了關于Java初級必看的數據類型與常量變量知識點的相關資料,需要的朋友可以參考下
    2023-11-11
  • SpringBoot bean查詢加載順序流程詳解

    SpringBoot bean查詢加載順序流程詳解

    當你在項目啟動時需要提前做一個業(yè)務的初始化工作時,或者你正在開發(fā)某個中間件需要完成自動裝配時。你會聲明自己的Configuration類,但是可能你面對的是好幾個有互相依賴的Bean
    2023-03-03
  • 玩轉SpringBoot中的那些連接池(小結)

    玩轉SpringBoot中的那些連接池(小結)

    這篇文章主要介紹了玩轉SpringBoot中的那些連接池(小結),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12

最新評論

随州市| 瑞丽市| 达孜县| 章丘市| 华容县| 台北市| 岑巩县| 封开县| 栾川县| 石屏县| 建昌县| 宾阳县| 澄迈县| 抚州市| 香港| 南投县| 玛曲县| 翁源县| 习水县| 仁化县| 清远市| 和林格尔县| 司法| 鄂州市| 丹寨县| 广平县| 乌兰浩特市| 凤山市| 格尔木市| 永昌县| 灌南县| 贵州省| 佳木斯市| 揭阳市| 新邵县| 宝坻区| 迁安市| 嫩江县| 浦县| 类乌齐县| 隆回县|