VTD XML查找ghost / non存在路径时出错

时间:2016-08-31 22:32:57

标签: java xml vtd-xml

我有一个XML块,如下所示,其中包含3个attachmentBinary元素(虽然为了便于阅读而被截断):

  <MyRequest>
      <MojNumber>219</MojNumber>
      <AttachmentBinary fileExtension="image/jpeg">/9j/4UK8RXhpZgAASUkqAAgAAAAMAAABBAABAAAAAAoAAAEBBAABAAAAAAABABAgAJAAAApgAAABIBAwABAAAAAQAAABoBBQABAAAAsAAAABsBBQABAAAgAAADEBAgAOAAAAwAAAADIBAgAUAAAAzgAAABMCAwABAAAAAQAAAGmHBA</AttachmentBinary>                                   
      <AttachmentBinary fileExtension="image/jpeg">/9j/4UE1RXhpZgAASUkqAAgAAAAMAAABBAABAAAAAAoAAAEBBAABAAAAAA5QCMg7udvGabszG8hX5M5JZhk/SrFvuV0Q7dmx87s4c8LUlwrRqrKWyBl</AttachmentBinary>
      <AttachmentBinary fileExtension="image/jpeg">/9j/4SfRRXhpZgAASUkqAAgAAAAMAAABBAABAAAAAAoAAAEBBAABAAAAAASTwcU+GZSThd3y4J6GpAm8ysxDmQALk71A9hVKTvcznHS5gSh2LSgkEgyG</AttachmentBinary>
      </MyRequest>

我的VTD-XML Java代码首先提取fileExtension和主Base64编码的jpg如下:

try {
                        ap.selectXPath("/MyRequest/AttachmentBinary/text()");
                    } catch (XPathParseException e2) {
                        // TODO Auto-generated catch block
                        e2.printStackTrace();
                    }

                int i = 0;

                while ((i = ap.evalXPath()) != -1) {
                    // i points to text node of
                        String ext  = vn.toString(vn.getAttrVal("fileExtension"));
                     System.out.println("ext is:"+ ext);

                     String s = vn.toRawString(i);
}
然而,我获得了6次而不是3次点击。其中只有3个产量可见的图像。

1 个答案:

答案 0 :(得分:0)

你的xpath请求一个没有属性节点的文本节点...所以你应该使用/MyRequest/AttachmentBinary,即没有text()......那么ext就可以了......

但要获取text(),您需要调用getText()并确保它不是-1。