当前位置: 技术问答>java相关
关于把文件转成dom的问题?
来源: 互联网 发布时间:2015-11-03
本文导语: 把dom转成文件时这样做的: import java.io.*; import org.apache.xml.serialize.XMLSerializer; import org.apache.xml.serialize.OutputFormat; FileOutputStream fout = new FileOutputStream("gen.svg"); serialize(fout, "UTF-8"); public void serialize(java.io.OutputStr...
把dom转成文件时这样做的:
import java.io.*;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;
FileOutputStream fout = new FileOutputStream("gen.svg");
serialize(fout, "UTF-8");
public void serialize(java.io.OutputStream out, String encoding) throws java.io.IOException {
OutputFormat format = new OutputFormat(m_domFactory);
format.setEncoding(encoding);
format.setIndent(4);
XMLSerializer serial = new XMLSerializer( new java.io.BufferedWriter(new java.io.OutputStreamWriter(out, encoding)), format );
serial.asDOMSerializer();
serial.serialize(m_domFactory.getDocumentElement() );
}
现在要把文件"gen.svg"再转成dom
用transform(source,result)方法报下面的错误:
DEBUG catch2 poi:javax.xml.transform.TransformerException:
Namespace not supported by SAXParser
怎么解决呢?
还有其他方法可以把文件转成dom吗?
谢谢!
import java.io.*;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;
FileOutputStream fout = new FileOutputStream("gen.svg");
serialize(fout, "UTF-8");
public void serialize(java.io.OutputStream out, String encoding) throws java.io.IOException {
OutputFormat format = new OutputFormat(m_domFactory);
format.setEncoding(encoding);
format.setIndent(4);
XMLSerializer serial = new XMLSerializer( new java.io.BufferedWriter(new java.io.OutputStreamWriter(out, encoding)), format );
serial.asDOMSerializer();
serial.serialize(m_domFactory.getDocumentElement() );
}
现在要把文件"gen.svg"再转成dom
用transform(source,result)方法报下面的错误:
DEBUG catch2 poi:javax.xml.transform.TransformerException:
Namespace not supported by SAXParser
怎么解决呢?
还有其他方法可以把文件转成dom吗?
谢谢!
|
看看底下这个:
import java.net.URL;
import java.io.*;
import oracle.xml.parser.v2.*;
import org.w3c.dom.*;
public class OurDOMParser
{
private static DOMParser parser = null;
private static String encode = ConstLib.ENCODE;
/***
功能描述:构造器。
*/
public OurDOMParser()
{
}
/***
功能描述:将一串 XML 字符串转成 XML DOM 模式。
@param xmlString XML格式字符串
@return 转换成的XML节点
@throws ClassException
*/
public static Element parse(String xmlString) throws ClassException{
Element ele = null;
try{
parser = new DOMParser();
byte[] tempByte = xmlString.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(tempByte);
BufferedReader reader = new BufferedReader(new InputStreamReader(bais,encode));
parser.parse(reader);
Document doc = (Document)(parser.getDocument());
ele = doc.getDocumentElement();
}catch(Exception e){
e.printStackTrace();
throw new ClassException("0000001");//解析XML出错。
}
return ele;
}
/***
功能描述:将一个URL所指文件的内容转成DOM模式。
@param xml文件的url
@return 转换成的XML节点
@throws ClassException
*/
public Element parse(URL url) throws ClassException{
Element ele = null;
try{
parser = new DOMParser();
parser.parse(url);
Document doc = (Document)(parser.getDocument());
ele = doc.getDocumentElement();
}catch(Exception e){
e.printStackTrace();
throw new ClassException("0000001");//解析XML出错。
}
return ele;
}
}
import java.net.URL;
import java.io.*;
import oracle.xml.parser.v2.*;
import org.w3c.dom.*;
public class OurDOMParser
{
private static DOMParser parser = null;
private static String encode = ConstLib.ENCODE;
/***
功能描述:构造器。
*/
public OurDOMParser()
{
}
/***
功能描述:将一串 XML 字符串转成 XML DOM 模式。
@param xmlString XML格式字符串
@return 转换成的XML节点
@throws ClassException
*/
public static Element parse(String xmlString) throws ClassException{
Element ele = null;
try{
parser = new DOMParser();
byte[] tempByte = xmlString.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(tempByte);
BufferedReader reader = new BufferedReader(new InputStreamReader(bais,encode));
parser.parse(reader);
Document doc = (Document)(parser.getDocument());
ele = doc.getDocumentElement();
}catch(Exception e){
e.printStackTrace();
throw new ClassException("0000001");//解析XML出错。
}
return ele;
}
/***
功能描述:将一个URL所指文件的内容转成DOM模式。
@param xml文件的url
@return 转换成的XML节点
@throws ClassException
*/
public Element parse(URL url) throws ClassException{
Element ele = null;
try{
parser = new DOMParser();
parser.parse(url);
Document doc = (Document)(parser.getDocument());
ele = doc.getDocumentElement();
}catch(Exception e){
e.printStackTrace();
throw new ClassException("0000001");//解析XML出错。
}
return ele;
}
}