【发布时间】: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