读取xml指定节点值并生成csv文件
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XmlReader {
/**
* 读取xml文件指定节点内容并导出到csv文件中
* @param path
* 指定文件夹路径
* @param destNode
* 目标节点
* @param fileName
* 出力csv文件名
*/
public void readXmlFile(String path, String destNode, String fileName) {
File file = new File(fileName);
FileOutputStream out;
try {
// 建立csv输出文件流
out = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(out);
BufferedWriter bw = new BufferedWriter(osw);
// 初始化csv文件
initCSV(bw);
// 得到指定文件夹下的所有xml文件
List fileList = getXmlFiles(path);
// 创建xml文件
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = null;
String xmlFileName;
Node root;
NodeList links;
for (int k = 0; k < fileList.size(); k++) {
xmlFileName = fileList.get(k).toString();
doc = builder.parse(xmlFileName);
doc.normalize();
// xml文件根节点
root = doc.getDocumentElement();
// xml文件中所有指定节点
links = doc.getElementsByTagName(destNode);
for (int i = 0; i < links.getLength(); i++) {
Node link = links.item(i);
// 指定节点的所有子节点
NodeList childNodes = link.getChildNodes();
StringBuffer sb = new StringBuffer();
// 遍历子节点
for (int j = 0; j < childNodes.getLength(); j++) {
Node chi
相关文档:
解读PHP DOMDocument在解析XML文件中的作用
http://developer.51cto.com 2009-12-02 10:39 佚名 柳城博客 我要评论(0)
PHP DOMDocument的功能非常强大,我们在这篇文章中将介绍如何正确的运用PHP DOMDocument来进行XML文件的解析。希望对又需要的朋友有所帮助。
在使用PHP对XML文件进行解析的时 ......
<!-- xml 格式
<books>
<book id='1001'>
<author>andylin</author>
<title>c language</title>
<publisher id="aaa">O'Reilly</publisher>
</book>
<book id='1002'>
<author>congfeng</author>
<t ......
用URLLoader加载XML,然后将data转化为ByteArray
用byteArray.readMultiByte(bytes.length,"utf-8")
然后将转化后的字符串再强制转化为XML
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.Binary;
var urlRequest:URLRequest = new URLRequest(source);
loader.addEventListener( ......
PKM2这个个人知识管理软件相信很多人用过,可以把数据导出为chm电子书,但是不知道为什么倒出来的chm不能按照标题排序,所以我就导出为xml格式,弄个xsl来显示它.
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output me ......