编译xml中的脚本

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值