包含特殊字符“/”的解析值使用 SAX 解析器给出错误的输出

Parse value containing special character "/" gives wrong output using SAX parser

我有以下 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)

如果使用任何转义字符,那么我必须使用 .

这里是收集文本的 DefaultHandler 的源代码:

private static DefaultHandler getHandler() {
    return new DefaultHandler() {
        String text;

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            if ("BookMark".equals(qName)) {
                text = "";
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            text += new String(ch).trim();
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if ("BookMark".equals(qName)) {
                System.out.println("endElement: " + text);
            }
        }

    };
}

您需要将字符 () 方法更改为

@Override
public void characters(char[] buffer, int start, int length) {
    tmpValue += new String(buffer, start, length);
}

并且您必须将 startElement() 方法中的 tmpValue 重置为“”。

只需更改 character() 方法

@Override
    public void characters(char[] buffer, int start, int length) {
        tmpValue += new String(buffer, start, length);
    }

并将其添加到 endElement 方法的最后一行。

public void endElement(String s, String s1, String element) throws SAXException {

if (OrgDataPartitonObj != null && "fs:FinancialStatementLineItemDataItem".equals(OrgDataPartitonObj.getType())) {

  FinancialStatementLineItemParser.getEndElementFinancialStatementLineItemParser(financialStatementLineItemObj, element, tmpValue);
        }
tmpValue="";
}