java + dom4j.jar提取xml文档内容
程序员文章站
2023-10-30 18:46:28
本文实例为大家分享了java + dom4j.jar提取xml文档内容的具体代码,供大家参考,具体内容如下
资源下载页:点击下载
本例程主要借助几个遍历的操作对xml格...
本文实例为大家分享了java + dom4j.jar提取xml文档内容的具体代码,供大家参考,具体内容如下
资源下载页:点击下载
本例程主要借助几个遍历的操作对xml格式下的内容进行提取,操作不是最优的方法,主要是练习使用几个遍历操作。
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的根节点 * @param args */ private element getrootnode(document document) { return document.getrootelement(); } /** * 获取所需节点值 * @param xml */ private void searchvalue(string xml) { document document = load(xml); element root = getrootnode(document); // 根节点 // 文章日期 date = xml.substring(10, 20); // 文章标题 doc_title = root.valueof("//head/title"); // 文章-id list<node> list_doc_id = document.selectnodes("//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节点进行操作 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实现CSV文件导入与导出功能
下一篇: 科学家惊人的发现,地球受神秘力量保护