gpt4 book ai didi

java - 使用 SAXParser 仅解析 CDATA

转载 作者:行者123 更新时间:2023-12-02 00:16:52 26 4
gpt4 key购买 nike

快速提问:

如果我有这样的 XML:

<?xml version="1.0" encoding="utf-8"?>
<cop xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="cop.xsd">
<auth>
<uid mioattributo="20">16<![CDATA[
function matchwo(a,b) ]]></uid>
</auth>
</cop>

所以uid有两个 child ,对吗? Node.CDATA_SECTION_NODE 之一和 Node.TEXT_NODE 之一。

实现这个快速类(扩展通常的 DefaultHandler):

public class MyHandler extends DefaultHandler {
/**
* Logger for this class
*/
private static final Log log = LogFactory.getLog(MyHandler.class);
private StringBuilder sb;

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("STARTUri: " + uri);
System.out.println("STARTLocalName: " + localName);
System.out.println("STARTqName: " + qName);
// for(int i=0;i<attributes.getLength();i++) {
// System.out.println("LocalName: "+attributes.getLocalName(i));
// System.out.println("Type: "+attributes.getType(i));
// System.out.println("qName: "+attributes.getQName(i));
// System.out.println("URI: "+attributes.getURI(i));
// System.out.println("Value: "+attributes.getValue(i));
// }
sb = new StringBuilder();
//super.startElement(uri, localName, qName, attributes);
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
sb.append(ch, start, length);
System.out.println("TEMPORARY: " + sb.toString());
System.out.println();
}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("ENDUri: " + uri);
System.out.println("ENDLocalName: " + localName);
System.out.println("ENDqName: " + qName);
System.out.println("Content: " + sb.toString());
sb.replace(0, sb.length()-1,"");
}

}

输出的解析类似于:

Is Validating: true
STARTUri:
STARTLocalName: cop
STARTqName: cop
TEMPORARY:


STARTUri:
STARTLocalName: auth
STARTqName: auth
TEMPORARY:


STARTUri:
STARTLocalName: uid
STARTqName: uid
TEMPORARY: 16

TEMPORARY: 16
function matchwo(a,b)

ENDUri:
ENDLocalName: uid
ENDqName: uid
Content: 16
function matchwo(a,b)
TEMPORARY:


ENDUri:
ENDLocalName: auth
ENDqName: auth
Content:

TEMPORARY:


ENDUri:
ENDLocalName: cop
ENDqName: cop
Content:

从输出中我们可以看到,characters() 方法在节点 uid 内部被调用了两次,因此它识别了两个子节点。有没有办法知道哪一个是CDATA,哪一个是TEXT?

最佳答案

您应该查看 LexicalHandler它告诉您有关 CDATA 开始/结束的信息。

请注意,SAX 解析器可以根据需要多次(或尽可能少)地调用您的 characters() 方法,以便您构建一个字符串(您只需知道在 endElement() 被调用时完成),并且您不能依赖它来确定文档结构。

关于java - 使用 SAXParser 仅解析 CDATA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11763462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com