【问题标题】:Parsing Apache Tika XML Output returns Unknown Tag解析 Apache Tika XML 输出返回未知标记
【发布时间】:2015-11-25 08:02:03
【问题描述】:

基本上,我使用<div class="embedded" id="content"> 解析来自Apache Tika 的几个xml 输出以获取元数据(通过元标记)和嵌入文件列表。但是,我发现我的地图有几个键 Unknown tag (0x...)。我想知道它是否是由 Tika 的不完整标签输出引起的,因为我得到的错误只与未关闭的标签有关——我怀疑它在 XML 的正文中,而不是我想要的输出(元、div)。但是,写入地图的唯一代码是元标记和 div(带有嵌入式类)是相当不合逻辑的——这只是文档的一小部分。

public class Parse {
    private class internalXMLReader extends DefaultHandler{
        public final Map<String, Object> entityList = new HashMap<>();

        @Override
        public void startElement(String uri, String localname, String qName, Attributes attributes) throws SAXException{
            String key, content;
            if(qName.equalsIgnoreCase("meta")){
                key = attributes.getValue("name");
                content = attributes.getValue("content");
                if(key.contains("Content-Type")){
                    String tmp[] = attributes.getValue("content").replace(' ', '\0').split(";");
                    if(tmp.length > 1){
                        content = tmp[0];
                    }
                }
                entityList.put(key, content);
            }
            else if(qName.equalsIgnoreCase("div")){
                if((attributes.getValue("class") != null) && (attributes.getValue("class").equalsIgnoreCase("embedded"))){
                    key = "embedded";
                    List<String> inlist;
                    if(entityList.containsKey("embedded") && (entityList.get("embedded") instanceof List)){
                        inlist = (List) entityList.get(key);
                    }
                    else{
                        inlist = new LinkedList<>();
                        entityList.put(key, inlist);
                    }
                    inlist.add(attributes.getValue("id"));
                }
            }
        }

        @Override
        public void endElement(String uri, String localname, String qName) throws SAXException{
            //no, i just did not want to validate or such..
        }

        @Override
        public void characters(char ch[], int start, int length) throws SAXException{
            //no, we don't actually read <something>this</something> yet
        }
    }
    public Entity parse(String xml, Entity in){
        try{
            InputSource xmlinput = new InputSource(new StringReader(xml));
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            internalXMLReader handler = new internalXMLReader();
            parser.parse(xmlinput, handler);
            in.addMeta(handler.entityList);
        }
        catch(IOException | ParserConfigurationException | SAXException ex){
            Logger.getLogger(TikaParseNCluste.class.getName()).log(Level.SEVERE, null, ex);
        }
        return in;
    }
}

也许我应该看看我的 800 多个 xml 文件。

【问题讨论】:

    标签: java xml sax saxparser apache-tika


    【解决方案1】:

    谷歌和javadocs 没有关于这件事的信息,我很不耐烦。但是运行grep -i -l -r "name=\"unknown" . 我有几个jpg 文件有&lt;meta name="Unknown..." contents="..."/&gt; 也许这就是原因。我不希望 ApacheTika 会给出这样的输出。因此,我将代码更改为:

    ...
    if(qName.equalsIgnoreCase("meta") && (attributes.getValue("name") != null)){
                    key = attributes.getValue("name");
                    if((key != null) && (!key.contains("Unknown"))){
                        content = attributes.getValue("content");
                        if(key.contains("Content-Type")){
                            String tmp[] = attributes.getValue("content").replace(' ', '\0').split(";");
                            if(tmp.length > 1){
                                content = tmp[0];
                            }
                        }
                        entityList.put(key, content);
                    }
                }
    ...
    

    我想知道这是一个错误还是其他什么。到目前为止,使用关键字 apache tika unknown tag 快速查询 Google 搜索只会将我带到这里。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-05
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      相关资源
      最近更新 更多