VTD-XML可以将String作为输入吗?

时间:2021-09-08 23:31:43

Hey, I'm trying to use VTD-XML to parse XML given to it as a String, but I can't find how to do it. Any help would be appreciated.

嘿,我正在尝试使用VTD-XML来解析作为String给出的XML,但我找不到如何做到这一点。任何帮助,将不胜感激。

http://vtd-xml.sourceforge.net

2 个解决方案

#1


5  

It seems VTD-XML library lets you read byte array data. I'd suggest in that case, convert the String to bytes using the correct encoding.

似乎VTD-XML库允许您读取字节数组数据。在这种情况下,我建议使用正确的编码将String转换为字节。

If there's an encoding signaled in the begining of the XML string:

如果在XML字符串的开头有一个编码信号:

<?xml version="1.0" encoding="UTF-8"?>

Then use that:

然后使用:

myString.getBytes("UTF-8")

If there's not an encoding, please use one, for VTD-XML know how to decode the bytes:

如果没有编码,请使用一个,因为VTD-XML知道如何解码字节:

String withHeader  = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + myString;
byte[] bytes = withHeader.getBytes("UTF-8");
VTDGen vg = new VTDGen();
vg.setDoc(bytes);
vg.parse(true);

Note that in the later case you can use any valid encoding because the string you have in memory is encoding-agnosting (it's in UTF-16 but when you ask for the bytes it will be converted).

请注意,在后一种情况下,您可以使用任何有效的编码,因为您在内存中的字符串是encoding-agnosting(它是UTF-16,但是当您要求字节时它将被转换)。

#2


2  

VTD-XML doesn't accept a string because string implies UCS-16 encoding, which means it is not really a xml document.. as defined by the spec, xml is usually encoded in utf-8, ascii, iso-8859-1 or UTF-16LE or BE format... does my answer make sense?

VTD-XML不接受字符串,因为字符串意味着UCS-16编码,这意味着它不是真正的xml文档..如规范所定义,xml通常以utf-8编码,ascii,iso-8859-1或UTF-16LE或BE格式......我的回答有意义吗?

#1


5  

It seems VTD-XML library lets you read byte array data. I'd suggest in that case, convert the String to bytes using the correct encoding.

似乎VTD-XML库允许您读取字节数组数据。在这种情况下,我建议使用正确的编码将String转换为字节。

If there's an encoding signaled in the begining of the XML string:

如果在XML字符串的开头有一个编码信号:

<?xml version="1.0" encoding="UTF-8"?>

Then use that:

然后使用:

myString.getBytes("UTF-8")

If there's not an encoding, please use one, for VTD-XML know how to decode the bytes:

如果没有编码,请使用一个,因为VTD-XML知道如何解码字节:

String withHeader  = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + myString;
byte[] bytes = withHeader.getBytes("UTF-8");
VTDGen vg = new VTDGen();
vg.setDoc(bytes);
vg.parse(true);

Note that in the later case you can use any valid encoding because the string you have in memory is encoding-agnosting (it's in UTF-16 but when you ask for the bytes it will be converted).

请注意,在后一种情况下,您可以使用任何有效的编码,因为您在内存中的字符串是encoding-agnosting(它是UTF-16,但是当您要求字节时它将被转换)。

#2


2  

VTD-XML doesn't accept a string because string implies UCS-16 encoding, which means it is not really a xml document.. as defined by the spec, xml is usually encoded in utf-8, ascii, iso-8859-1 or UTF-16LE or BE format... does my answer make sense?

VTD-XML不接受字符串,因为字符串意味着UCS-16编码,这意味着它不是真正的xml文档..如规范所定义,xml通常以utf-8编码,ascii,iso-8859-1或UTF-16LE或BE格式......我的回答有意义吗?

相关文章