dom4j遍历xml出错
本帖最后由 jiangeb 于 2011-04-15 14:46 编辑
我用dom4j 遍历xml文件。这个xml文件里包含了5条记录,每条记录的结构一样。遍历的时候程序循环了5遍,得到了5条记录。但是所有记录的结果是一样的,都是第一条的内容。下面是xml文件代码
- <ListRecords>
- <record>
- <metadata>
- <oai_dc:dc xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" 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>
- </metadata>
- </record>
复制代码每条记录在一个<record></record>中。在<record>上还有一层是<listrecords>
- public int ReadListIdentifyXML(String filename)
- {
- int returnValue =0;
- String nodename = "";
- String nodetext = "";
- String nodepath = "";
- int count = 0;
- try
- {
- SAXReader saxReader = new SAXReader();
- Document document = saxReader.read((new File(filename)));
- Element root = document.getRootElement();
- Element iden = root.element("ListRecords");
- for(Iterator i = iden.elementIterator(); i.hasNext();)
- {
- Element element1 =(Element) i.next();
- nodename = element1.getName();
- nodetext = element1.getText();
- nodepath = element1.getPath();
- if(nodename.equals("record"))
- {
- Element record = iden.element("record");
- for(Iterator p = record.elementIterator(); p.hasNext();)
- {
- Element element2 =(Element) p.next();
- nodename = element2.getName();
- nodetext = element2.getText();
- nodepath = element2.getPath();
- if(nodename.equals("metadata"))
- {
- Element metadata = record.element("metadata");
- for(Iterator q = metadata.elementIterator(); q.hasNext();)
- {
- Element element3 =(Element) q.next();
- nodename = element3.getName();
- nodetext = element3.getText();
- nodepath = element3.getPath();
- if(nodename.equals("dc"))
- {
- Element dc = metadata.element("dc");
- for(Iterator m = dc.elementIterator(); m.hasNext();)
- {
- Element element4 =(Element) m.next();
- nodename = element4.getName();
- nodetext = element4.getText();
- nodepath = element4.getPath();
- System.out.println("4 layer-name=" + nodename);
- System.out.println("4 layer-value=" + nodetext);
- System.out.println("4 layer-path=" + nodepath);
- System.out.println("#################################");
- }
- }
- }
- }
- }
- }
- nodename = "";
- nodetext = "";
- nodepath = "";
- }
复制代码不知道是哪里出错了?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论