使用VTD-XML在log4j.xml上进行XPath

时间:2014-03-28 15:31:36

标签: java xml xpath vtd-xml

我有像这样的有效Log4jXML字符串

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE log4j:eventSet PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd" >
<log4j:eventSet version="1.2" xmlns:log4j="http://jakarta.apache.org/log4j/" includesLocationInfo="true">
    <log4j:event logger="org.apache.axis2.deployment.ClusterBuilder" 
        timestamp="1395925603261" level="INFO" thread="localhost-startStop-1">
        <log4j:message>
            <![CDATA[Clustering has been disabled]]>
        </log4j:message>
    </log4j:event>
</log4j:eventSet>

我尝试运行此代码:

VTDGen doc = new VTDGen();
doc.setDoc(validLog4jXML.getBytes());
doc.parse(true);
VTDNav vn = doc.getNav();
AutoPilot ap = new AutoPilot(vn);
String xPath = "//log4j:event[(((@level=\"INFO\") or (@level=\"ERROR\")) and "
            + "(@timestamp>\"1395842943674\")) and (@timestamp<\"1396015743674\")]";
ap.selectXPath(xPath);

在最后一行我得到例外:

[ERROR] No URL found for prefix:log4j
com.ximpleware.XPathParseException: No URL found for prefix:log4j
    at com.ximpleware.xpath.CUP$parser$actions.CUP$parser$do_action(parser.java:752)
    at com.ximpleware.xpath.parser.do_action(parser.java:434)
    at java_cup.runtime.lr_parser.parse(lr_parser.java:570)
    at com.ximpleware.AutoPilot.selectXPath(AutoPilot.java:809)
    at ad.ecs.library.LogHelper.collectLogNodesVTD(LogHelper.java:654)
    at ad.ecs.library.LogHelper.collectLogNodesForADayVTD(LogHelper.java:571)
    at ad.ecs.library.LogHelper.collectLogNodesForTheWeekVTD(LogHelper.java:456)
    at ad.ecs.library.LogHelper.access$2(LogHelper.java:453)
    at ad.ecs.library.LogHelper$2.run(LogHelper.java:313)
    at java.lang.Thread.run(Thread.java:724)

我试着查看有关VTD-XML的文档,并说:

要避免此异常:

  

com.ximpleware.XPathParseException:找不到前缀:xyz

的URL

确保创建具有名称空间支持的初始导航器对象(即,将最后一个param设置为true),并将名称空间哈希作为最终参数传递给find-indexes,first-match,all-matches ,get-xml和get-hash。

我试图按照这些步骤进行搜索,但没有成功。我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:2)

在使用declareXPathNameSpace

在查询中使用命名空间之前,必须先注册命名空间
ap.declareXPathNameSpace(log4j, "http://jakarta.apache.org/log4j/");