我想在 java 中解析一个包含二进制数据的 xml 文件:这是 xml 文件的一个示例:
<?xml version="1.0" encoding="utf-8"?>
<documents>
<document>
<element name="docid">
<value><![CDATA[0902307e8004c74c]]></value>
</element>
<element name="published">
<value><![CDATA[2012-01-01T00:00:00]]></value>
</element>
<element name="documenttype">
<value><![CDATA[Circular]]></value>
</element>
<element name="data">
<value><![CDATA[%PDF-1.6
%����
1020 0 obj
<</Filter/FlateDecode/First 20/Length 270/N 3/Type/ObjStm>>stream
�o^���)|�,�Ypoef�
l���o�>����u���b"Cb�|���%&��D�yD��q�q�q�q�q��%_ja�LJob��/��3"=����o���]V11}� }a�+'6@����C�,^}�d%�۠�`s��q��5�^(�N��{S<S�����A��������-������f\ڌ��|U/�z���f�I9����g�g���s���0z'��X~
endstream
endobj
startxref
55097
%%EOF
]]></value>
</element>
<element name="dataname">
<value><![CDATA[sdfsfsfsdsdfsd.pdf]]></value>
</element>
</document>
</documents>
通常我会这样解析这样的xml文件:
Document doc = null;
DocumentBuilder documentBuilder = null;
documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
documentBuilder = documentBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
try {
doc = documentBuilder.parse(fastXMLFile);
} catch (SAXException e) {
System.out.println("SAXExept");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Test");
return;
}
但是由于包含二进制数据的“数据”元素,调试器告诉我:
[Fatal Error] xmlfile.xml:58:10: An invalid XML character (Unicode: 0x1a) was found in the CDATA section.
SAXExept
org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x1a) was found in the CDATA section.
我现在不需要解析这个数据字段,我可以跳过它。我只想解析其余的数据。这可能吗?