-
求解帮忙看下,dom4j解析xml怎么运行错在哪里啊?15
1.我这有两个类,1个是dom4j解析的类,另外一个是测试的类,另外有个xml的字符串。
2.报错的原因就是程序运行的结果是只循环处理第一条记录,后面的记录都没有出来。
3.有可能节点弄错了啊,请帮忙看下!
问题说明:
1.测试类的xml字符串,dc:title不一样,代表的记录不一样,record代表一个记录,我就是要把这xml全部解析,可是结果却出来第一条记录都是一模一样的啊,后面没有出来,麻烦帮忙看下啊!
PS:这是dom4j解析的xml的程序
public class Dom4j1 {
public static void parseString(String xml) {
Document doc = null;
try {
Map<String, String> map = new HashMap<String, String>();
map.put("da","http://purl.org/dc/elements/1.1/");
map.put("oai_dc","http://www.openarchives.org/OAI/2.0/oai_dc/");
SAXReader saxReader = new SAXReader();
// 将字符串转为XML
saxReader.getDocumentFactory().setXPathNamespaceURIs(map);
doc = saxReader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
//获取跟节点
Element rootElem = doc.getRootElement();
System.out.println("根节点是:"+rootElem.getName());
//获取responseDate
String responseDate = rootElem.elementText("responseDate");
System.out.println("responseDate是:"+responseDate);
//将string转换成Date类型
//Date responseDate2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(responseDate);
//获取 request元素属性metadata
String metadataPrefix = rootElem.element("request").attributeValue("metadataPrefix");
System.out.println("metadataPrefixAttr:"+metadataPrefix);
// 获取 request元素属性verb
String verb = rootElem.element("request").attributeValue("verb");
System.out.println("verb是:"+verb);
// 获取 request元素属性rescount
String rescount = rootElem.element("request").attributeValue("rescount");
System.out.println("rescountAttr:"+rescount);
// 获取 request元素属性Filter
String filter= rootElem.element("request").attributeValue("Filter");
// String filter1=new String(filter.getBytes("UTF-8"),"UTF-8");
String filter1=URLDecoder.decode("%D6%D0%B9%FA", "GBK");
System.out.println("filterAttr:"+filter);
System.out.println("filterAttr:"+filter1);
// 获取 requestAttr元素文本
String request = rootElem.elementText("request ");
System.out.println("request是:"+request);
// 获取ListRecords节点
Element ListRecords = rootElem.element("ListRecords");
//获取所有records节点
List<Element> records = null!=ListRecords?ListRecords.elements("record"):null;
// 遍历 ListRecords节点
for (Iterator iterator = records.iterator(); iterator.hasNext();) {
Element record = (Element) iterator.next();
//解析header
Element headerElem = record.element("header");
String identifierHead = headerElem.elementText("identifier");
String datestamp = headerElem.elementText("datestamp");
//将string转换成Date类型
//Date datestamp2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(datestamp);
String setSpecHead = headerElem.elementText("setSpec");
System.out.println("identifierHead:"+identifierHead);
System.out.println("datestampHead:"+datestamp);
//System.out.println("测试转换datestamp:"+datestamp2);
System.out.println("setSpecHead:"+setSpecHead);
//解析metadata节点
Element metadataElem = record.element("metadata");
//得到相应的元素的文本内容
String title = ((Element)(metadataElem.selectNodes("//da:title").get(0))).getTextTrim();
String creator = ((Element)(metadataElem.selectNodes("//da:creator").get(0))).getTextTrim();
String contributor = ((Element)(metadataElem.selectNodes("//da:contributor").get(0))).getTextTrim();
String publisher = ((Element)(metadataElem.selectNodes("//da:publisher").get(0))).getTextTrim();
String date = ((Element)(metadataElem.selectNodes("//da:date").get(0))).getTextTrim();
//将string转换成Date类型
//Date date2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
String type = ((Element)(metadataElem.selectNodes("//da:type").get(0))).getTextTrim();
String language = ((Element)(metadataElem.selectNodes("//da:language").get(0))).getTextTrim();
String relation = ((Element)(metadataElem.selectNodes("//da:relation").get(0))).getTextTrim();
String identifier = ((Element)(metadataElem.selectNodes("//da:identifier").get(0))).getTextTrim();
String source = ((Element)(metadataElem.selectNodes("//da:source").get(0))).getTextTrim();
String edition = ((Element)(metadataElem.selectNodes("//da:edition").get(0))).getTextTrim();
System.out.println("dc:title:"+title);
System.out.println("dc:creator:"+creator);
System.out.println("dc:contributor:"+contributor);
System.out.println("dc:publisher:"+publisher);
System.out.println("dc:date:"+date);
System.out.println("dc:type:"+type);
System.out.println("dc:language:"+language);
System.out.println("dc:relation:"+relation);
System.out.println("dc:identifier:"+identifier);
System.out.println("dc:source:"+source);
System.out.println("dc:edition:"+edition);
System.out.println("===============================================================");
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
2.下面是一个测试类的程序。
public class TestDom4j {
public static void main(String[] args) {
String xmlString =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<OAI-PMH xmlns=\"http://www.openarchives.org/OAI/2.0/\""+
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/"+
" http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd\">"+
" <responseDate>2012-11-23 10:46:47</responseDate>"+
" <request verb=\"ListRecords\" metadataPrefix=\"oai_dc\" rescount=\"3133\" Filter=\"中国\">http://www.goldlibsoft.com.cn/oai</request>"+
" <ListRecords>"+
" <record>"+
"<header>"+
"<identifier>6b010295d7b5602504dc869b0a1d942b</identifier>"+
"<datestamp>2012-11-23 10:46:47</datestamp>"+
"<setSpec>馆藏图书</setSpec>"+
"</header>"+
"<metadata>"+
"<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">"+
"<dc:title>中国法律年鉴.1993</dc:title><dc:creator>中国法律年鉴编辑部</dc:creator><dc:contributor>中国法律年鉴编辑部</dc:contributor><dc:publisher>中国法律年鉴社</dc:publisher><dc:date>2011-06-12 09:31:02</dc:date><dc:type>馆藏图书</dc:type><dc:language>chi</dc:language><dc:relation>null</dc:relation><dc:identifier>null</dc:identifier><dc:source><![CDATA[/libweb/book/do.jsp?method=view&id=1773983&aliasname=馆藏图书]]></dc:source><dc:edition></dc:edition></oai_dc:dc>"+
"</metadata>"+
"</record><record>"+
"<header>"+
"<identifier>4f30b1bc24ec310884d248b27e3d5ca1</identifier>"+
"<datestamp>2012-11-23 10:46:47</datestamp>"+
"<setSpec>馆藏图书</setSpec>"+
"</header>"+
"<metadata>"+
"<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">"+
"<dc:title>中国法律年鉴.1999</dc:title><dc:creator>中国法律年鉴编辑部编</dc:creator><dc:contributor>中国法律年鉴编辑部编</dc:contributor><dc:publisher>中国法律年鉴出版社</dc:publisher><dc:date>2011-06-12 09:25:05</dc:date><dc:type>馆藏图书</dc:type><dc:language>chi</dc:language><dc:relation>null</dc:relation><dc:identifier>null</dc:identifier><dc:source><![CDATA[/libweb/book/do.jsp?method=view&id=1773971&aliasname=馆藏图书]]></dc:source><dc:edition></dc:edition></oai_dc:dc>"+
"</metadata>"+
"</record><record>"+
"<header>"+
"<identifier>d62a5d18a1f8d3b4cd66f59015e3c81b</identifier>"+
"<datestamp>2012-11-23 10:46:47</datestamp>"+
"<setSpec>馆藏图书</setSpec>"+
"</header>"+
"<metadata>"+
"<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">"+
"<dc:title>中国法律年鉴.1997</dc:title><dc:creator>中国法律年鉴编辑部编</dc:creator><dc:contributor>中国法律年鉴编辑部编</dc:contributor><dc:publisher>中国法律年鉴社</dc:publisher><dc:date>2011-06-12 09:30:14</dc:date><dc:type>馆藏图书</dc:type><dc:language>chi</dc:language><dc:relation>null</dc:relation><dc:identifier>null</dc:identifier><dc:source><![CDATA[/libweb/book/do.jsp?method=view&id=1773991&aliasname=馆藏图书]]></dc:source><dc:edition></dc:edition></oai_dc:dc>"+
"</metadata>"+
"</record><record>"+
"<header>"+
"<identifier>91682c84e95fc475a687770b9b6d5c03</identifier>"+
"<datestamp>2012-11-23 10:46:47</datestamp>"+
"<setSpec>馆藏图书</setSpec>"+
"</header>"+
"<metadata>"+
"<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">"+
"<dc:title>信用中国</dc:title><dc:creator>龚勇著</dc:creator><dc:contributor>龚勇著</dc:contributor><dc:publisher>中国方正出版社</dc:publisher><dc:date>2011-06-12 09:27:43</dc:date><dc:type>馆藏图书</dc:type><dc:subject>信用-中国</dc:subject><dc:language>chi</dc:language><dc:relation>7-80107-599-4</dc:relation><dc:identifier>7-80107-599-4</dc:identifier><dc:source><![CDATA[/libweb/book/do.jsp?method=view&id=1524484&aliasname=馆藏图书]]></dc:source><dc:edition></dc:edition></oai_dc:dc>"+
"</metadata>"+
"</record><record>"+
"<header>"+
"<identifier>cc3cdf0248a58a09a0928c7d3fbfcab4</identifier>"+
"<datestamp>2012-11-23 10:46:47</datestamp>"+
"<setSpec>馆藏图书</setSpec>"+
"</header>"+
"<metadata>"+
"<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">"+
"<dc:title>中国见闻.1</dc:title><dc:creator>丁海峰编</dc:creator><dc:contributor>丁海峰编</dc:contributor><dc:publisher>新华出版社</dc:publisher><dc:date>2011-06-12 09:14:26</dc:date><dc:type>馆藏图书</dc:type><dc:subject>政治-评论-中国</dc:subject><dc:language>chi</dc:language><dc:relation>null</dc:relation><dc:identifier>null</dc:identifier><dc:source><![CDATA[/libweb/book/do.jsp?method=view&id=1785682&aliasname=馆藏图书]]></dc:source><dc:edition></dc:edition></oai_dc:dc>"+
"</metadata>"+
"</record><record>"+
"<header>"+
"<identifier>b1333ac7e9b27d6204f1ded179c06d4b</identifier>"+
"<datestamp>2012-11-23 10:46:47</datestamp>"+
"<setSpec>馆藏图书</setSpec>"+
"</header>"+
"<metadata>"+
"<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">"+
"<dc:title>中国刑法史</dc:title><dc:creator>周密著</dc:creator><dc:contributor>周密著</dc:contributor><dc:publisher>群众出版社</dc:publisher><dc:date>2011-06-12 09:14:48</dc:date><dc:type>馆藏图书</dc:type><dc:subject>中国-刑法</dc:subject><dc:language>chi</dc:language><dc:relation>null</dc:relation><dc:identifier>null</dc:identifier><dc:source><![CDATA[/libweb/book/do.jsp?method=view&id=1782735&aliasname=馆藏图书]]></dc:source><dc:edition></dc:edition></oai_dc:dc>"+
"</metadata>"+
"</record><record>"+
"<header>"+
"<identifier>20bcc0e36db109a550ad3b8d7411318d</identifier>"+
"<datestamp>2012-11-23 10:46:47</datestamp>"+
"<setSpec>馆藏图书</setSpec>"+
"</header>"+
"<metadata>"+
"<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">"+
"<dc:title>中国新闻年鉴.1983</dc:title><dc:creator>null</dc:creator><dc:publisher>中国社会科学出版社</dc:publisher><dc:date>2011-06-12 09:13:41</dc:date><dc:type>馆藏图书</dc:type><dc:language>chi</dc:language><dc:relation>null</dc:relation><dc:identifier>null</dc:identifier><dc:source><![CDATA[/libweb/book/do.jsp?method=view&id=1567334&aliasname=馆藏图书]]></dc:source><dc:edition></dc:edition></oai_dc:dc>"+
"</metadata>"+
"</record><record>"+
"<header>"+
"<identifier>bcd4ae46b1312dec368f4e548974f7fb</identifier>"+
"<datestamp>2012-11-23 10:46:47</datestamp>"+
"<setSpec>馆藏图书</setSpec>"+
"</header>"+
"<metadata>"+
"<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">"+
"<dc:title>中国统计年鉴.1983</dc:title><dc:creator>国家统计局编</dc:creator><dc:contributor>国家统计局编</dc:contributor><dc:publisher>中国统计出版社</dc:publisher><dc:date>2011-06-12 09:20:39</dc:date><dc:type>馆藏图书</dc:type><dc:language>chi</dc:language><dc:relation>null</dc:relation><dc:identifier>null</dc:identifier><dc:source><![CDATA[/libweb/book/do.jsp?method=view&id=1692204&aliasname=馆藏图书]]></dc:source><dc:edition></dc:edition></oai_dc:dc>"+
"</metadata>"+
"</record><record>"+
"<header>"+
"<identifier>56f715d53940ba22d910245da1b87561</identifier>"+
"<datestamp>2012-11-23 10:46:47</datestamp>"+
"<setSpec>馆藏图书</setSpec>"+
"</header>"+
"<metadata>"+
"<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">"+
"<dc:title>翠笛引</dc:title><dc:creator>马萧萧著</dc:creator><dc:contributor>马萧萧著</dc:contributor><dc:publisher>中国文联出版公司</dc:publisher><dc:date>2011-06-12 09:18:31</dc:date><dc:type>馆藏图书</dc:type><dc:subject>诗歌-中国-当代</dc:subject><dc:language>chi</dc:language><dc:relation>null</dc:relation><dc:identifier>null</dc:identifier><dc:source><![CDATA[/libweb/book/do.jsp?method=view&id=1668136&aliasname=馆藏图书]]></dc:source><dc:edition></dc:edition></oai_dc:dc>"+
"</metadata>"+
"</record><record>"+
"<header>"+
"<identifier>93b9a188371749750f78ce1fb78154cb</identifier>"+
"<datestamp>2012-11-23 10:46:47</datestamp>"+
"<setSpec>馆藏图书</setSpec>"+
"</header>"+
"<metadata>"+
"<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">"+
"<dc:title>中国法律年鉴.2003</dc:title><dc:creator>孙琬锺主编</dc:creator><dc:contributor>孙琬锺主编</dc:contributor><dc:publisher>中国法律年鉴社</dc:publisher><dc:date>2011-06-12 09:21:24</dc:date><dc:type>馆藏图书</dc:type><dc:language>chi</dc:language><dc:relation>null</dc:relation><dc:identifier>null</dc:identifier><dc:source><![CDATA[/libweb/book/do.jsp?method=view&id=1943685&aliasname=馆藏图书]]></dc:source><dc:edition></dc:edition></oai_dc:dc>"+
"</metadata>"+
"</record>"+
" <resumptionToken expirationDate=\"2012-11-23 10:46:47\""+
" cursor=\"0\">1352969813449</resumptionToken>"+
" </ListRecords>"+
"</OAI-PMH>";
Dom4j1.parseString(xmlString);
}
}
PS:图片中的内容就是各个程序的截图。
如果不清楚的话,另外麻烦请加企鹅:1018677016.进行详聊2012年12月06日 23:30
2个答案 按时间排序 按投票排序
-
缺jar包 jaxen.jar
到这下
http://dongisland.iteye.com/blog/11533952012年12月07日 07:39
-
问题在于((Element)(metadataElem.selectNodes("//da:title").get(0))).
因为你用namespace da:获取节点所以得到的是全文的title节点,而每次get(0),所以就是第一条数据,可以设置一个int index=0 变量,改为metadataElem.selectNodes("//da:title").get(index))),每次循环完一个record,index++
其他节点也类似处理2012年12月07日 09:28
相关推荐
1. 解析XML:介绍如何使用DOM、SAX和StAX解析XML文件。 2. 创建XML:展示如何在Java程序中生成XML文档。 3. XPath和XSLT:讲解如何使用XPath查找XML文档中的特定元素,以及如何用XSLT转换XML文档结构。 4. JAXB...
解析XML有DOM(Document Object Model)和SAX(Simple API for XML)两种主要方式。DOM一次性加载整个XML文档到内存,形成一棵树形结构,方便随机访问,但对大文件内存消耗大;SAX是事件驱动的解析器,只在遇到元素...
解析XML文档的方式主要有DOM解析、SAX解析和STAX解析。 二、HTML5部分 HTML5是最新版的超文本标记语言,为现代网页提供新的元素和API。 1. HTML5的新特性包括新语义元素(如article、section、nav等)、新的表单...
在"JAVA操作XML"和"Java解析XML文件"中,我们可以学习到Java如何通过DOM(文档对象模型)、SAX(简单API for XML)和StAX(流式API for XML)等不同方式来读取和写入XML文档。DOM将整个XML文件加载到内存中形成一棵...
5. **XML处理**:DOM、SAX、StaX解析XML文档,以及XPath查询。 6. **Java EE相关**:Servlet、JSP、EJB等企业级应用开发的基础知识。 书本上的例题程序则为每章的关键概念提供了示例代码,这些例子通常包括: 1. **...
`DocumentBuilderFactory`和`DocumentBuilder`用于解析XML,`NodeList`和`Node`接口处理XML元素,`Element`接口处理元素属性。 10. **获取IP地址.java**: Java的`java.net`包提供了获取本机或远程主机IP地址的方法...
在Java中解析XML文档主要有三种方式:SAX(Simple API for XML)、DOM(Document Object Model)以及StAX(Streaming API for XML)。 1. **SAX**: - **特点**:事件驱动模型,适合大型文档的解析。 - **原理**...
19. **基于XML的网站生成器**:涉及XML解析、DOM树构建和HTML生成,需要理解XML的语法规则以及如何将XML数据转化为网站结构。 20. **简单矢量图形**:可能涉及到图形学中的向量表示和操作,如线段、曲线的绘制,...
开发者可能需要深入理解DOM(文档对象模型)、AJAX(异步JavaScript和XML)以及可能涉及的浏览器兼容性问题。 在Moodle-Solver-master压缩包中,可能包含了以下内容: 1. 源代码文件:JavaScript文件、HTML文件和...
SVG(Scalable Vector Graphics)是一种基于XML的矢量图像格式,广泛用于网页设计和图形创作,因为它可以无损地缩放而不会失真。Brother PCS文件则是Brother绣花机使用的专有格式,存储刺绣设计的数据,通常包含线迹...
作为一款基于JavaScript技术的应用,TactuumCalc利用了Web技术的灵活性和可访问性,可以在多种平台上运行,包括桌面浏览器和移动设备。 **二、JavaScript技术详解** 1. **JavaScript基础**:JavaScript是一种解释...
- **解析灵活**:能够根据DOM树模型灵活查找元素。 - **编码转换**:自动处理文档编码问题,简化开发者的工作。 - **文档转换**:可以将文档转换为统一格式,便于后续处理。 - **易于扩展**:支持多种解析器,可...
- XML处理:如DOM、SAX解析,用于结构化数据存储和传输。 在这个" Coding-Interview-Questions-master "压缩包中,你可以找到各种Java实现的面试题解答,涵盖了上述提到的数据结构和算法。通过深入学习和实践这些...
在JavaScript这个标签下,我们可以推测"我的OSRM"是将OSRM的核心功能包装成Web可使用的版本,可能是一个Web应用或者库,使得开发者能够在浏览器环境中进行路线规划和导航。JavaScript作为客户端脚本语言,使得这个...
**标题与描述解析** 标题"MathALEA:JavaScript入门和数学入门,HTML和LaTeX或PDF"表明这是一个关于学习JavaScript编程以及数学基础知识的资源,同时提供了HTML、LaTeX和PDF等格式的学习材料。MathALEA可能是一个...