温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java XML处理工具

发布时间:2020-06-04 18:43:16 来源:网络 阅读:256 作者:专注地一哥 栏目:web开发

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);

 }

       

    }

 

}

 


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI