xml文档的内容如下:
<?xml version="1.0" encoding="UTF-8"?> <root> <name aa="sjs"/> <pass>sjs</pass> <script> <![CDATA[ int a = 3; int b = 5; System.out.println("a+b= "+(a+b)); ]]> </script> </root>
1.通过sax解析xml获取cdata中的脚本片段
package com.st.sax;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.Locator;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class SaxHelper {
class XMLParser extends DefaultHandler{
//用于保存该xml文件中text值
private StringBuffer buf;
//用于保存临时元素的内容
private String elementValue;
//用于保存指定cdata元素的文本内容
private String cdata = "";
//在构造函数中初始化内容缓存
public XMLParser() {
super();
buf = new StringBuffer();
elementValue = "";
}
public void setDocumentLocator(Locator locator) {
}
public void startDocument() throws SAXException {
// buf = new StringBuffer();
}
public void endDocument() throws SAXException {
}
public void startPrefixMapping(String prefix, String uri) {
}
public void endPrefixMapping(String prefix) {
}
public void processingInstruction(String target, String instruction)
throws SAXException {
}
public void ignorableWhitespace(char[] chars, int start, int length)
throws SAXException {
}
public void skippedEntity(String name) throws SAXException {
}
//一般在该方法中处理元素的属性
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) {
if("script".equals(qName)){
System.out.println("===================script node begin.");
}
}
//一般在该方法中将用于保存临时元素值的变量清空,以便于保存下一个元素的内容
public void endElement(String namespaceURI, String localName,
String fullName) throws SAXException {
if("name".equals(fullName)){
elementValue = "";
}
if("pass".equals(fullName)){
elementValue = "";
}
if("script".equals(fullName)){
System.out.println("=====================script node end.");
cdata = elementValue;
elementValue = "";
}
}
//处理元素的内容
public void characters(char[] chars, int start, int length)
throws SAXException {
// 将元素内容累加到StringBuffer中
buf.append(chars, start, length);
/**
* 取text值需要使用临时变量,并在startElement和endElement中控制
*/
char[] aa = new char[length];
System.arraycopy(chars, start, aa, 0, length);
elementValue += new String(aa);
System.out.println("elementValue= "+elementValue);
}
public String getCDATA(String node) {
try {
String path = "xml" +System.getProperty("file.separator")+"test.xml";
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
//如果此处的this 换成new XMLParser(),则方法后面的cdata无法取到,
//主要是因为作用域的问题;因为该类的属性cdata无法取到(匿名类,无引用),
//如果在try语句块中创建XMLParser sax = new XMLParser();也无法取到,原因作用域问题
sp.parse(new InputSource(path), this);
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
String value = cdata;
System.out.println("cdata="+cdata);
return value;
}
}
public String load(){
XMLParser sax = new XMLParser();
String aa = null;
aa = sax.getCDATA("script");
return aa;
}
}
2.通过bsh工具解析脚本
package com.st.sax;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.Locator;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class SaxHelper {
class XMLParser extends DefaultHandler{
//用于保存该xml文件中text值
private StringBuffer buf;
//用于保存临时元素的内容
private String elementValue;
//用于保存指定cdata元素的文本内容
private String cdata = "";
//在构造函数中初始化内容缓存
public XMLParser() {
super();
buf = new StringBuffer();
elementValue = "";
}
public void setDocumentLocator(Locator locator) {
}
public void startDocument() throws SAXException {
// buf = new StringBuffer();
}
public void endDocument() throws SAXException {
}
public void startPrefixMapping(String prefix, String uri) {
}
public void endPrefixMapping(String prefix) {
}
public void processingInstruction(String target, String instruction)
throws SAXException {
}
public void ignorableWhitespace(char[] chars, int start, int length)
throws SAXException {
}
public void skippedEntity(String name) throws SAXException {
}
//一般在该方法中处理元素的属性
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) {
if("script".equals(qName)){
System.out.println("===================script node begin.");
}
}
//一般在该方法中将用于保存临时元素值的变量清空,以便于保存下一个元素的内容
public void endElement(String namespaceURI, String localName,
String fullName) throws SAXException {
if("name".equals(fullName)){
elementValue = "";
}
if("pass".equals(fullName)){
elementValue = "";
}
if("script".equals(fullName)){
System.out.println("=====================script node end.");
cdata = elementValue;
elementValue = "";
}
}
//处理元素的内容
public void characters(char[] chars, int start, int length)
throws SAXException {
// 将元素内容累加到StringBuffer中
buf.append(chars, start, length);
/**
* 取text值需要使用临时变量,并在startElement和endElement中控制
*/
char[] aa = new char[length];
System.arraycopy(chars, start, aa, 0, length);
elementValue += new String(aa);
System.out.println("elementValue= "+elementValue);
}
public String getCDATA(String node) {
try {
String path = "xml" +System.getProperty("file.separator")+"test.xml";
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
//如果此处的this 换成new XMLParser(),则方法后面的cdata无法取到,
//主要是因为作用域的问题;因为该类的属性cdata无法取到(匿名类,无引用),
//如果在try语句块中创建XMLParser sax = new XMLParser();也无法取到,原因作用域问题
sp.parse(new InputSource(path), this);
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
String value = cdata;
System.out.println("cdata="+cdata);
return value;
}
}
public String load(){
XMLParser sax = new XMLParser();
String aa = null;
aa = sax.getCDATA("script");
return aa;
}
}
输出结果:
cdata=
int a = 3;
int b = 5;
System.out.println("a+b= "+(a+b));
a+b= 8