javaXML处理工具
import java.io.FileNotFoundException;
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、网页空间、营销软件、网站建设、托里网站维护、网站推广。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import net.sf.json.JSONObject;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import oracle.net.aso.p;
public class XmlUtil {
/**
*
* @param document
* @param path 指明路径和文件名
* @param flag 是否转义
*/
public static void writeXML(Document root,String path,boolean flag) {
OutputFormat format = new OutputFormat(" ",flag);
format.setEncoding("utf-8");//设置编码格式
XMLWriter xmlWriter;
try {
xmlWriter = new XMLWriter(new FileOutputStream(path),format);
xmlWriter.write(root);
xmlWriter.close();
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
*
* @param element
* @param path 指明路径和文件名
*/
public static void writeXML(Element root,String path) {
function(){ //点差 http://www.fx61.com/faq/HantecGlobal/480.html
OutputFormat format = new OutputFormat(" ",true);
format.setEncoding("utf-8");//设置编码格式
XMLWriter xmlWriter;
try {
xmlWriter = new XMLWriter(new FileOutputStream(path),format);
xmlWriter.write(root);
xmlWriter.close();
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static String jsonToXML(String json) {
try {
net.sf.json.xml.XMLSerializer xmlSerializer = new net.sf.json.xml.XMLSerializer();
// 根节点名称
xmlSerializer.setRootName("resource");
// 不对类型进行设置
xmlSerializer.setTypeHintsEnabled(false);
String xmlStr = "";
JSONObject jobj = JSONObject.fromObject(json);
xmlStr = xmlSerializer.write(jobj);
return xmlStr;
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException(e);
}
}
}
分享题目:javaXML处理工具
URL网址:http://scyanting.com/article/jcgdhe.html