【发布时间】:2017-11-24 05:48:10
【问题描述】:
我有下面的xml结构
<fs:AsReportedItem>
<fs:BookMark>/BODY[1]/DIV[3135]/DIV[0]/TABLE[0]/TBODY[0]/TR[32]/TD[5]/DIV[0]/FONT[0]/substr(1,2)
</fs:BookMark>
</fs:AsReportedItem>
我正在使用 SAX 解析并在 endElement() 方法中读取税值
这是我的示例代码
private void parseDocument() {
// parse
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
parser.parse(FileName, this);
} catch (ParserConfigurationException e) {
System.out.println("ParserConfig error");
} catch (SAXException e) {
System.out.println("SAXException : xml not well formed");
} catch (IOException e) {
System.out.println("IO error");
}
}
public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {
if (OrgDataPartitonObj != null && "fs:FinancialStatementLineItemDataItem".equals(OrgDataPartitonObj.getType())) {
FinancialStatementLineItemParser.startFinancialStatementLineItemParser(OrgDataPartitonObj,financialStatementLineItemObj, elementName, attributes);
}
}
public void endElement(String s, String s1, String element) throws SAXException {
if (OrgDataPartitonObj != null && "fs:FinancialStatementLineItemDataItem".equals(OrgDataPartitonObj.getType())) {
FinancialStatementLineItemParser.getEndElementFinancialStatementLineItemParser(financialStatementLineItemObj, element, tmpValue);
}
}
public static void getEndElementFinancialStatementLineItemParser(FinancialStatementLineItem financialStatementLineItemObj, String element, String tmpValue) {
if (element.equals("fs:BookMark")) {
financialStatementLineItemObj.setBookMark(tmpValue);
}
}
@Override
public void characters(char[] buffer, int start, int length) {
tmpValue = new String(buffer, start, length);
}
当我调试时,我只能看到这个值/substr(1,2) 所有带有"/" 的值都被转义了
我不知道为什么我没有获得全部价值/BODY[1]/DIV[3135]/DIV[0]/TABLE[0]/TBODY[0]/TR[32]/TD[5]/DIV[0]/FONT[0]/substr(1,2)
如果使用了任何转义字符,那么我必须在哪里使用。
【问题讨论】:
-
为什么
"/"被转义了? -
@DevilsHnd 我的问题是什么
-
与“/”无关。 Sax-DefaultHandler 的 characters() 方法总是返回有限的字符数。要获取全文,您必须收集 characters() 方法的文本。
-
只是为了扩展@RalfRenz 的答案:SAX 解析器可以分解文本并在多次调用 characters() 时将其移交。它可以在任何它选择的地方拆分文本,但许多解析器发现在实体扩展发生的点拆分它很方便。
-
@MichaelKay 那么您建议进行哪些更改以获得全文