DOM写的xml工具类

package xml_util;


import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
 * 这里我的想法:是抛砖引玉,渴望大家提出更好和不足建议
 * 工具类只提供遍历操作,不提供增删操作
 * 在Dom xml增删操作中只是在内存中进行的
 * 在xml文档中我们是看不出的,在增删操作之后必须重新输出xml文档之后,才可以看到结果
 * @author kevin.wangwei
 * 2010-4-5
 */
public final class XmlUtil {
	/** xml 文档对象 */
	private Document document;
	/** 工具类单例对象 */
	private static XmlUtil instance;
	/** xml在系统中的路径 */
	private String xmlFilePath;
	/**xml 根元素名称 */
	private String documentElementName;
	
	private XmlUtil(String xmlFilePath)throws Exception{
		this.xmlFilePath=xmlFilePath;
		readXmlDocument();
	}

	public String getXmlFilePath() {
		return xmlFilePath;
	}

	public String getDocumentElementName() {
		return documentElementName;
	}

	/**
	 * 获得工具类实例
	 * @return
	 */
	public static XmlUtil newInstance(String xmlFilePath) throws Exception{
		if(instance==null){
			synchronized(XmlUtil.class){
			if(instance==null){
			  instance=new XmlUtil(xmlFilePath);
		    }
		  }
	    }
	  return instance;	
    }
	/**
	 * 读取xml文档
	 * @throws Exception
	 */
	private void readXmlDocument() throws Exception{
		DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
		DocumentBuilder db=dbf.newDocumentBuilder();
		document=db.parse(new File(xmlFilePath));
		Element documentElement=document.getDocumentElement();
		if(documentElement!=null){
			documentElementName=documentElement.getNodeName();
		}
	}
	/**
	 * 打印xml文档内容
	 */
	public String printerXmlDocument(){
		StringBuffer sb=new StringBuffer();
		sb.append("<?xml version=\""+document.getXmlVersion()+"\" encoding=\""+document.getXmlEncoding()+"\" ?>\n");
		printerNode(document,sb);
		return sb.toString();
	}
	/**
	 * 打印文本节点内容
	 * @param node
	 */
	private void printerTextNodeInfo(Node node,StringBuffer sb){
		if(node==null){
			return;
		}
		String value=StringUtil.validateString(node.getNodeValue());
		if(!(value.trim().equals(""))){//除去空白的文本节点
		   sb.append(value+"\n");
		}
	}
	/**
	 * 打印属性节点名称和值
	 * @param node
	 */
	private void printerAttributesNode(Node node,StringBuffer sb){
		if(node==null){
			return;
		}
		NamedNodeMap attrs=node.getAttributes();
		int length=attrs.getLength();
		for(int i=0;i<length;i++){
			Node attr=attrs.item(i);
			sb.append(" "+StringUtil.validateString(attr.getNodeName())+"=\""+StringUtil.validateString(attr.getNodeValue())+"\"");
		}
	}
	/**
	 * 打印xml文档中所有节点
	 * @param node
	 * @param sb
	 */
	private void printerNode(Node node,StringBuffer sb){
		if(node==null){
			return;
		}
		NodeList nl=node.getChildNodes();
		for(int i=0;i<(nl.getLength());i++){
			Node n=nl.item(i);
			if(n.getNodeType()==Node.ELEMENT_NODE){
				String nodeName=n.getNodeName();
				sb.append("<"+n.getNodeName());
				printerAttributesNode(n,sb);
				sb.append(">\n");
				printerNode(n,sb);
				sb.append("</"+nodeName+">\n");
			}else if(n.getNodeType()==Node.TEXT_NODE){
				printerTextNodeInfo(n,sb);
			}else if(n.getNodeType()==Node.COMMENT_NODE){
				sb.append("<"+n.getNodeName()+">");
				printerTextNodeInfo(n,sb);
				sb.append("</"+n.getNodeName()+">\n");
			}else{
				printerTextNodeInfo(n,sb);
			}
			
		}
	}
	/**
	 * 依据节点名称查找该节点的所有文本节点的值(包括该节点的子节点)
	 * @param nodeName
	 * @return
	 */
	public ArrayList<String> getNodeValue(String nodeName){
		if(!StringUtil.isValidateString(nodeName)){
			return null;
		}
		ArrayList<String> values=new ArrayList<String>();
		NodeList nl=document.getElementsByTagName(nodeName);
		int length=nl.getLength();
		for(int i=0;i<length;i++){
			Node node=nl.item(i);
			if(node.getNodeType()==Node.TEXT_NODE){
				String value=StringUtil.validateString(node.getNodeValue());
				if(!(value.trim().equals(""))){
					values.add(value);
				}
			}else if(node.getNodeType()==Node.ELEMENT_NODE){
				traversalNode(node,values);
			}
		}
		return values;
		
	}
	/**
	 * 递归查找当前节点的所有文本节点
	 * @param node
	 * @param values
	 */
	private void traversalNode(Node node,ArrayList<String> values){
		if(node==null){
			return ;
		}
		NodeList nl=node.getChildNodes();
		int length=nl.getLength();
		for(int i=0;i<length;i++){
			Node n=nl.item(i);
			if(n.getNodeType()==Node.TEXT_NODE){
				String value=StringUtil.validateString(n.getNodeValue());
				if(!(value.trim().equals(""))){
					values.add(value);
				}
			}else if(n.getNodeType()==Node.ELEMENT_NODE){
				traversalNode(n,values);
			}
		}
	}
	/**
	 *  依据当前元素名称,查找该元素的所有属性
	 * @param nodeName
	 * @return
	 */
	public Map<String,String> getNodeAttributes(String nodeName) throws Exception{
		if(!StringUtil.isValidateString(nodeName)){
			return null;
		}
		Map<String,String> map=new HashMap<String,String>();
		NodeList nl=document.getElementsByTagName(nodeName);
		int length=nl.getLength();
		for(int i=0;i<length;i++){
			Node node=nl.item(i);
			if(node.getNodeType()!=Node.ELEMENT_NODE){
				throw new Exception("这不是一个有效节点元素");
			}else{
				NamedNodeMap attrs=node.getAttributes();
				int len=attrs.getLength();
				for(int j=0;j<len;j++){
					Node attr=attrs.item(j);
					map.put(StringUtil.validateString(attr.getNodeName()), StringUtil.validateString(attr.getNodeValue()));
				}
			}
		}
		return map;
	}
	/**
	 * 依据指定输出流输出xml文档
	 * @param toFilePath
	 * @throws Exception
	 */
	public void transformer(OutputStream out) throws Exception{
		TransformerFactory tff=TransformerFactory.newInstance();
		Transformer tf=tff.newTransformer();
		DOMSource source=new DOMSource(document);
		StreamResult result=new StreamResult(out);
		tf.transform(source, result);
	}
	
	public static void main(String args[]) throws Exception{
		XmlUtil xml=XmlUtil.newInstance("d:\\test2.xml");
		//System.out.println(xml.printerXmlDocument());
		//for(String str:xml.getNodeValue("id")){
		//	System.out.println(str);
		//}
		//System.out.println(xml.getNodeAttributes("student"));
		//xml.transformer(System.out);
	}
}

 

内容概要:本文针对国内加密货币市场预测研究较少的现状,采用BP神经网络构建了CCi30指数预测模型。研究选取2018年3月1日至2019年3月26日共391天的数据作为样本,通过“试凑法”确定最优隐结点数目,建立三层BP神经网络模型对CCi30指数收盘价进行预测。论文详细介绍了数据预处理、模型构建、训练及评估过程,包括数据归一化、特征工程、模型架构设计(如输入层、隐藏层、输出层)、模型编译与训练、模型评估(如RMSE、MAE计算)以及结果可视化。研究表明,该模型在短期内能较准确地预测指数变化趋势。此外,文章还讨论了隐层节点数的优化方法及其对预测性能的影响,并提出了若干改进建议,如引入更多技术指标、优化模型架构、尝试其他时序模型等。 适合人群:对加密货币市场预测感兴趣的研究人员、投资者及具备一定编程基础的数据分析师。 使用场景及目标:①为加密货币市场投资者提供一种新的预测工具和方法;②帮助研究人员理解BP神经网络在时间序列预测中的应用;③为后续研究提供改进方向,如数据增强、模型优化、特征工程等。 其他说明:尽管该模型在短期内表现出良好的预测性能,但仍存在一定局限性,如样本量较小、未考虑外部因素影响等。因此,在实际应用中需谨慎对待模型预测结果,并结合其他分析工具共同决策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值