使用 apache tika 从 gzip 文件中提取 xml 数据?

发布于 2024-09-14 13:34:09 字数 1303 浏览 8 评论 0原文

我正在工作一个项目,其中我需要使用 apache tika[AM NEW TO TIKA] 从 gz 文件中提取 xml(站点地图)数据。 fie 名称类似于 sitemap01.xml.gz 我可以从普通文本文件或 html 中提取数据,但我不知道如何从 gz 中提取 xml 并从 xml 中提取元和数据... 这两天我用谷歌搜索了。

我需要在 tika 中使用 delegateParser 从 xml 中提取数据吗? 请指导我一些示例或文章......

这是我的尝试

public void parseXml() throws IOException{
    Metadata metadata = new Metadata();
    ContentHandler handler = new BodyContentHandler();
    Parser parser = new AutoDetectParser();
    ParseContext context = new ParseContext();
     InputStream stream =this.getClass().getResourceAsStream("sitemap.xml.gz");
    try {
        parser.parse(stream,handler,metadata,context);
        for(int i = 0; i <metadata.names().length; i++) {
            String name = metadata.names()[i];
            System.out.println(name + " : " + metadata.get(name));
          }
        System.out.println(handler.toString());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TikaException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
         if(stream!=null) {
                stream.close();
            }
    }


}

I am working a project in which i need to extract xml(sitemap)data from gz file using apache tika[AM NEW TO TIKA].
the fie name is something like sitemap01.xml.gz
I could extract data from normal text file or html,but i don't know how to extract xml from gz and extract the meta and data from xml...
I searched Google for past two days.

Do i need to use delegateParser in tika to extract data from xml?
Please guide me to some sample or articles....

Here is my try

public void parseXml() throws IOException{
    Metadata metadata = new Metadata();
    ContentHandler handler = new BodyContentHandler();
    Parser parser = new AutoDetectParser();
    ParseContext context = new ParseContext();
     InputStream stream =this.getClass().getResourceAsStream("sitemap.xml.gz");
    try {
        parser.parse(stream,handler,metadata,context);
        for(int i = 0; i <metadata.names().length; i++) {
            String name = metadata.names()[i];
            System.out.println(name + " : " + metadata.get(name));
          }
        System.out.println(handler.toString());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TikaException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
         if(stream!=null) {
                stream.close();
            }
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泡沫很甜 2024-09-21 13:34:09

您缺少的是在 ParseContext 上设置递归解析器。您可能想要这样的东西:

Parser parser = new AutoDetectParser();
ParseContext context = new ParseContext();
context.set(Parser.class, parser);
parser.parse(....)

通过在 ParseContext 上设置解析器,您告诉 Tika 在遇到嵌入文档(例如 GZip 中的 XML)时调用该解析器

The thing you're missing is setting a recursing parser on your ParseContext. You probably want something like:

Parser parser = new AutoDetectParser();
ParseContext context = new ParseContext();
context.set(Parser.class, parser);
parser.parse(....)

By setting a Parser on the ParseContext, you tell Tika to call that when it encounters embedded documents (such as the XML inside your GZip)

要走干脆点 2024-09-21 13:34:09

以下是如何针对您的案例使用 Apache Tika 的 XML 解析器:

 //detecting the file type
  BodyContentHandler handler = new BodyContentHandler(-1);
  Metadata metadata = new Metadata();
  File inFile = new File("sitemap.xml.gz");
  System.out.println(inFile.isFile());
  FileInputStream inputstream = new FileInputStream(inFile);
  ParseContext pcontext = new ParseContext();

  //Xml parser
  XMLParser xmlparser = new XMLParser(); 
  xmlparser.parse(inputstream, handler, metadata, pcontext);
  System.out.println(pcontext.toString());

  System.out.println("Contents of the document:" + handler.toString());//this one contains all contents from xml files and tags are also removed
  System.out.println("Metadata of the document:");
  String[] metadataNames = metadata.names();

  for(String name : metadataNames) {
    System.out.println(name + ": " + metadata.get(name));

Here is how you can use XML parser from Apache Tika for your case:

 //detecting the file type
  BodyContentHandler handler = new BodyContentHandler(-1);
  Metadata metadata = new Metadata();
  File inFile = new File("sitemap.xml.gz");
  System.out.println(inFile.isFile());
  FileInputStream inputstream = new FileInputStream(inFile);
  ParseContext pcontext = new ParseContext();

  //Xml parser
  XMLParser xmlparser = new XMLParser(); 
  xmlparser.parse(inputstream, handler, metadata, pcontext);
  System.out.println(pcontext.toString());

  System.out.println("Contents of the document:" + handler.toString());//this one contains all contents from xml files and tags are also removed
  System.out.println("Metadata of the document:");
  String[] metadataNames = metadata.names();

  for(String name : metadataNames) {
    System.out.println(name + ": " + metadata.get(name));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文