VTD-XML 元素片段不正确
VTD-XML element fragment incorrect
当使用 VTD-XML 解析包含 © 等特殊字符的 XML 文档(UTF-8 格式)时,我现在遇到一个问题,即返回的元素片段 (getElementFragment) 不正确.
示例代码:
VTDGen vg = new VTDGen();
String xmlDocument =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
"<Root>\r\n" +
" <!-- © -->\r\n" +
" <SomeElement/>\r\n" +
"</Root>";
// For some reason with US_ASCII it does work, although the file is UTF-8.
vg.setDoc(xmlDocument.getBytes(StandardCharsets.UTF_8));
// True or false doesn't matter here, some result.
vg.parse(false);
// Find the element and its fragment.
VTDNav nv = vg.getNav();
AutoPilot ap = new AutoPilot(nv);
ap.selectXPath("//SomeElement");
while ((ap.evalXPath()) != -1) {
long elementOffset = nv.getElementFragment();
int contentStartIndex = (int)elementOffset;
int contentEndIndex = contentStartIndex + (int)(elementOffset>>32);
System.out.println("Returned fragment: " + contentStartIndex + ":" + contentEndIndex + ":\n'" + xmlDocument.substring(contentStartIndex, contentEndIndex) + "'");
}
这个returns:
Returned fragment: 65:79:
'SomeElement/>
'
虽然将 StandardCharsets.UTF_8 更改为 StandardCharsets.US_ASCII 它确实有效:
Returned fragment: 64:78:
'<SomeElement/>'
当输入文件是 UTF-8 文件时,这会导致不正确的行为。这可能是 VTD-XML 中的错误,还是我在这里做错了什么?
“©”是一个双字 unicode 字符,它导致 starting/ending unicode 偏移量从 starting/ending 字节偏移量漂移 1。这不是错误...下面是修复
while ((ap.evalXPath()) != -1) {
long elementOffset = nv.getElementFragment();
int contentStartIndex = (int)elementOffset;
int contentEndIndex = contentStartIndex + (int)(elementOffset>>32);
System.out.println("Returned fragment: " + contentStartIndex + ":" + contentEndIndex + ":\n'"
+ nv.toString(contentStartIndex,(int)(elementOffset>>32)));
//+ xmlDocument.substring(contentStartIndex, contentEndIndex) + "'");
}
当使用 VTD-XML 解析包含 © 等特殊字符的 XML 文档(UTF-8 格式)时,我现在遇到一个问题,即返回的元素片段 (getElementFragment) 不正确.
示例代码:
VTDGen vg = new VTDGen();
String xmlDocument =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
"<Root>\r\n" +
" <!-- © -->\r\n" +
" <SomeElement/>\r\n" +
"</Root>";
// For some reason with US_ASCII it does work, although the file is UTF-8.
vg.setDoc(xmlDocument.getBytes(StandardCharsets.UTF_8));
// True or false doesn't matter here, some result.
vg.parse(false);
// Find the element and its fragment.
VTDNav nv = vg.getNav();
AutoPilot ap = new AutoPilot(nv);
ap.selectXPath("//SomeElement");
while ((ap.evalXPath()) != -1) {
long elementOffset = nv.getElementFragment();
int contentStartIndex = (int)elementOffset;
int contentEndIndex = contentStartIndex + (int)(elementOffset>>32);
System.out.println("Returned fragment: " + contentStartIndex + ":" + contentEndIndex + ":\n'" + xmlDocument.substring(contentStartIndex, contentEndIndex) + "'");
}
这个returns:
Returned fragment: 65:79:
'SomeElement/>
'
虽然将 StandardCharsets.UTF_8 更改为 StandardCharsets.US_ASCII 它确实有效:
Returned fragment: 64:78:
'<SomeElement/>'
当输入文件是 UTF-8 文件时,这会导致不正确的行为。这可能是 VTD-XML 中的错误,还是我在这里做错了什么?
“©”是一个双字 unicode 字符,它导致 starting/ending unicode 偏移量从 starting/ending 字节偏移量漂移 1。这不是错误...下面是修复
while ((ap.evalXPath()) != -1) {
long elementOffset = nv.getElementFragment();
int contentStartIndex = (int)elementOffset;
int contentEndIndex = contentStartIndex + (int)(elementOffset>>32);
System.out.println("Returned fragment: " + contentStartIndex + ":" + contentEndIndex + ":\n'"
+ nv.toString(contentStartIndex,(int)(elementOffset>>32)));
//+ xmlDocument.substring(contentStartIndex, contentEndIndex) + "'");
}