【问题标题】:java xml parsing dblpjava xml解析dblp
【发布时间】:2014-05-20 14:47:05
【问题描述】:

这是xml文件
请问如何解析标签作者的例子,我们不知道每个inproceeding有多少作者?

<?xml version="1.0" encoding="ISO-8859-1"?>    
<dblp>

<inproceedings mdate="2014-01-18" key="series/sci/AzzagL13">
<author>Hanane Azzag</author>
<author>Mustapha Lebbah</author>
<title>A New Way for Hierarchical and Topological Clustering.</title>
<pages>85-97</pages>
<year>2011</year>
<booktitle>EGC (best of volume)</booktitle>
<ee>http://dx.doi.org/10.1007/978-3-642-35855-5_5</ee>
<crossref>series/sci/2013-471</crossref>
<url>db/series/sci/sci471.html#AzzagL13</url>
</inproceedings>

<inproceedings mdate="2014-01-18" key="series/sci/RabatelBP13">
<author>Julien Rabatel</author>
<author>Sandra Bringay</author>
<author>Pascal Poncelet</author>
<title>Mining Sequential Patterns: A Context-Aware Approach.</title>
<pages>23-41</pages>
<year>2011</year>
<booktitle>EGC (best of volume)</booktitle>
<ee>http://dx.doi.org/10.1007/978-3-642-35855-5_2</ee>
<crossref>series/sci/2013-471</crossref>
<url>db/series/sci/sci471.html#RabatelBP13</url>
</inproceedings>
</dblp>

【问题讨论】:

    标签: java xml parsing xml-parsing


    【解决方案1】:

    使用 Xpath,它快速而强大,您的示例中的这些行返回 5 行

    代码:

    final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream("input.xml"));
    
            final XPath xPath = XPathFactory.newInstance().newXPath();
            final NodeList nodeList = (NodeList) xPath.compile("//author").evaluate(document, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
            }
    

    显示:

    Hanane Azzag
    Mustapha Lebbah
    Julien Rabatel
    Sandra Bringay
    Pascal Poncelet
    

    【讨论】:

      【解决方案2】:

      下面的代码解析使用了实际项目中常用的 apache digester。来自 apache 社区的不错的一个

      // 根据需要更新代码。

         import java.io.ByteArrayInputStream;
      import java.io.FileInputStream;
      import java.io.InputStream;
      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.Iterator;
      import java.util.List;
      import java.util.Map.Entry;
      
      import org.apache.commons.digester.Digester;
      import org.apache.commons.digester.Rule;
      import org.apache.commons.digester.Rules;
      import org.xml.sax.InputSource;
      
      
      public class Parsing {
      public static void main(String[] args) throws Exception{
          InputStream data = new FileInputStream("E:\\workspace\\trunk\\Parsing\\src\\data.xml");
          byte[] b = new byte[data.available()];
      //  data.read(b);
          Digester digester = new Digester();
          //Genearting Array list while encountering dblp xpath
          digester.addObjectCreate("dblp", HashMap.class);
          digester.addObjectCreate("dblp/inproceedings", ArrayList.class);
          //Calling add method while encountering author xpath
          AuthorRule rule = new AuthorRule();
          digester.addRule("dblp/inproceedings/author", rule);
          digester.addRule("dblp/inproceedings/title", rule);
          digester.addRule("dblp/inproceedings", rule);
      
          HashMap parsedData = (HashMap) digester.parse(data);
          Iterator<Entry<String, ArrayList>> dataItr = parsedData.entrySet().iterator();
          while(dataItr.hasNext()){
              Entry<String, ArrayList> entry = dataItr.next();
              System.out.println("Title : " + entry.getKey() + ", Authors" + entry.getValue().toString());
          }
      
      
      }
      private static class AuthorRule extends Rule{
          String currentTitle = "";
      
          @Override
          public void body(String namespace, String name, String text)
          throws Exception {
              HashMap object = (HashMap) digester.peek(1);
              ArrayList authors = (ArrayList) digester.peek(0);
              if(name.equals("title")){
                  currentTitle = text;
              }
              else if(name.equals("author")){
                  authors.add(text);
              }
          }
      
          @Override
          public void end(String namespace, String name) throws Exception {
              HashMap object = (HashMap) digester.peek(1);
              ArrayList authors = (ArrayList) digester.peek(0);
              if(name.equals("inproceedings")){
                  object.put(currentTitle, authors);
              }
          }
      }
      }
      

      输出::
      标题 : 层次和拓扑聚类的新方法。, Authros[Hanane Azzag, Mustapha Lebbah] 标题:挖掘顺序模式:一种上下文感知方法。,Authros[Julien Rabatel,Sandra Bringay,Pascal Poncelet]

      【讨论】:

      • 它需要 commons digester、collection、beanutils 和 logging jar 文件
      • 我希望它们采用这种格式:title: A New Way for Hierarchical and Topological Clustering。作者:Ammar Mechouche、Nathalie Abadie、Emeric Prouteau、Sbastien Mustire –
      • 我会在 1 天内寄给你
      • 答案会根据您的需要进行更新。将其标记为最终答案并关闭此问题。
      • 在基于对象的模型中解析有点容易理解,在处理具有大量路径和元素结构的大型 xml 时也易于管理。 Digester 在解析如此庞大的 xml 树方面有很大帮助。您可以谷歌搜索更多的消化器示例以更好地学习它。高拉夫
      【解决方案3】:

      有很多方法,例如通过 DOM:

      import org.w3c.dom.Document;
      import org.w3c.dom.Node;
      import org.w3c.dom.NodeList;
      
      import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
      import java.io.File;
      
      public class XmlAuthorReader {
          public static void main(String argv[]) {
              try {
                  File fXmlFile = new File(<filePath>);
                  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                  Document doc = dBuilder.parse(fXmlFile);
      
                  NodeList nList = doc.getElementsByTagName("author");
      
                  System.out.println(nList.getLength()+ " author(s) found");
                  for (int temp = 0; temp < nList.getLength(); temp++) {
                      Node nNode = nList.item(temp);
                      System.out.println("Author: " + nNode.getTextContent());
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
      

      您可以在此处找到更多变体:http://www.mkyong.com/tutorials/java-xml-tutorials/

      【讨论】:

      • 我希望它们采用这种格式:title: A New Way for Hierarchical and Topological Clustering。作者:Ammar Mechouche,Nathalie Abadie,Emeric Prouteau,Sbastien Mustire
      猜你喜欢
      • 2018-09-23
      • 1970-01-01
      • 2012-11-17
      • 2012-03-07
      • 1970-01-01
      • 2014-05-10
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多