带有@xpath的dom4j makeElement,但是Oracle extractvalue函数无法解析asXML结果

时间:2012-08-03 07:24:35

标签: oracle xml-parsing dom4j

我正在使用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'属性的错误响应问题?

1 个答案:

答案 0 :(得分:1)

<@FLAG>不是合法的XML元素名称,因为@字符不能是名称的开头。在强制执行格式良好且有效的XML文档时,DOM4J并不是最佳的。

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