0

我正在使用 dom4j 开发一个 XML 消息处理程序,并且遇到了 XML 节点属性解析的问题。业务需求是:解析并验证输入的XML,如果字段或属性无效,则返回一个XML,其结构表示错误。我在下面截取了一个测试程序:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class XPathStudy {
    private final static Logger log = LoggerFactory.getLogger(XPathStudy.class);
    private static String xmlInput;

    private static void initXmlInput() {
        xmlInput = "<RootTag> <BizRec FLAG=\"5\">";
        xmlInput += " <FieldOne>11111</FieldOne>";
        xmlInput += " <FieldTwo></FieldTwo>";
        xmlInput += " <FieldThree>33333</FieldThree>";
        xmlInput += " </BizRec> </RootTag>";
    }

    private static Document makeErrRspsDoc(String xpath, String errCode, String errDesc) {
        Document errDoc = DocumentHelper.createDocument();
        Element errElem = DocumentHelper.makeElement(errDoc, xpath);
        errElem.addElement("ErrCode").addText(errCode);
        errElem.addElement("ErrDesc").addText(errDesc);
        return errDoc;
    }

    public static void main(String[] args) throws Exception {
        initXmlInput();
        log.info("xmlInput = " + xmlInput);
        Document doc = DocumentHelper.parseText(xmlInput);
        log.info("xmlInput parsed done");

        String xpath = "*//FieldTwo";
        Node node = doc.getRootElement().selectSingleNode(xpath);
        if (node == null) {
            log.warn("node [" + xpath + "] not found");
            System.exit(1);
        }
        log.info("node [" + node.getPath() + "] located");
        if (node.getText().trim().isEmpty()) {
            Document errDoc = makeErrRspsDoc(node.getPath(), "1201", "FieldTwo can not be empty");
            log.warn("errDoc: " + errDoc.asXML());
        } else {
            log.info("FieldTwo validation ok");
        }

        xpath = "*//@FLAG";
        node = doc.getRootElement().selectSingleNode(xpath);
        if (node == null) {
            log.warn("node [" + xpath + "] not found");
            System.exit(1);
        }
        log.info("node [" + node.getPath() + "] located");
        int flagVal = Integer.parseInt(node.getText().trim());
        if (flagVal == 1 || flagVal == 2) {
            log.info("FLAG " + flagVal + " is ok");
        } else {
            Document errDoc = makeErrRspsDoc(node.getPath(), "1001", "FLAG attr should be 1 or 2");
            log.warn("errDoc: " + errDoc.asXML());
        }
    }
}

运行它,日志信息如下:

15:01:08.143 [main] INFO  XPathStudy - xmlInput = <RootTag> <BizRec FLAG="5"> <FieldOne>11111</FieldOne> <FieldTwo></FieldTwo> <FieldThree>33333</FieldThree> </BizRec> </RootTag>
15:01:08.203 [main] INFO  XPathStudy - xmlInput parsed done
15:01:08.255 [main] INFO  XPathStudy - node [/RootTag/BizRec/FieldTwo] located
15:01:08.259 [main] WARN  XPathStudy - errDoc: <?xml version="1.0" encoding="UTF-8"?>
<RootTag><BizRec><FieldTwo><ErrCode>1201</ErrCode><ErrDesc>FieldTwo can not be empty</ErrDesc></FieldTwo></BizRec></RootTag>
15:01:08.260 [main] INFO  XPathStudy - node [/RootTag/BizRec/@FLAG] located
15:01:08.260 [main] WARN  XPathStudy - errDoc: <?xml version="1.0" encoding="UTF-8"?>
<RootTag><BizRec><@FLAG><ErrCode>1001</ErrCode><ErrDesc>FLAG attr should be 1 or 2</ErrDesc></@FLAG></BizRec></RootTag>

一切似乎都正常,errDoc 将被记录到 Oracle10g 表(一个 VARCHAR2(1000) 字段),以下 SQL 是可以的:

select extractvalue(xmltype('<RootTag><BizRec><FieldTwo><ErrCode>1201</ErrCode><ErrDesc>FieldTwo can not be empty</ErrDesc></FieldTwo></BizRec></RootTag>'),
'*//ErrCode') as err_code from dual;
1201

但是这个 SQL 会失败:

select extractvalue(xmltype('<RootTag><BizRec><@FLAG><ErrCode>1001</ErrCode><ErrDesc>FLAG attribute should be 1 or 2</ErrDesc></@FLAG></BizRec></RootTag>'),
'*//ErrCode') as err_code from dual;

Error starting at line 1 in command:
select extractvalue(xmltype('<RootTag><BizRec><@FLAG><ErrCode>1001</ErrCode><ErrDesc>FLAG attribute should be 1 or 2</ErrDesc></@FLAG></BizRec></RootTag>'),
'*//ErrCode') as err_code from dual
Error report:
SQL Error: ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00231: invalid character 64 ('@') found in a Name or Nmtoken
Error at line 1
ORA-06512: at "SYS.XMLTYPE", line 301
ORA-06512: at line 1
31011. 00000 -  "XML parsing failed"
*Cause:    XML parser returned an error while trying to parse the document.
*Action:   Check if the document to be parsed is valid.

Oracle 的文档解释说 XML 的节点不能包含特殊字符。所以,我的问题是:如何更改我的 Java 代码来解决 'FLAG' 属性的错误响应问题?

4

1 回答 1

1

<@FLAG>不是合法的 XML 元素名称,因为该@字符不能是名称的开头。DOM4J 在执行格式良好且有效的 XML 文档方面并不以同类最佳而著称。

要将失败节点作为 XPath 表达式进行通信,请考虑将其存储在属性中或作为文本节点。

于 2012-08-06T22:01:44.440 回答