java XML处理工具
admin
2023-02-13 02:00:02
0

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

 }

       

    }

 

}

 


相关内容

热门资讯

活力中国调研行丨一束“光”的故... 它被称为“最快的刀”“最准的尺”“最亮的光”。从一束“光”到一颗“芯”,再到规模千亿的产业集群,今天...
携手前沿技术 共创智能未来 携手前沿技术 共创智能未来 ——来自2026年世界互联网大会数字丝路发展论坛的声音 夏日长安,秦岭叠...
上海浩劢取得桌面式轻型悬臂吊专... 国家知识产权局信息显示,上海浩劢工业科技有限公司取得一项名为“桌面式轻型悬臂吊”的专利,授权公告号C...
毒油风暴未歇民进党却急着上架,... 海峡导报综合报道 岛内毒油食安风暴持续延烧,台北市长蒋万安持续号召民众7·25上凯道,但“食药署”2...
WAIC挤满“卖铲人”,但算力... 2026年的WAIC,机器人依然抢占了绝大多数镜头。但展馆里数量增长最快的,是自称“AI Infra...
对话芯展速许玮:“内存墙”下,... Token经济时代,衡量AI价值的标准,正从模型能力转向Token生产效率。AI因此从工具演变为新的...
菲尔兹奖得主邓煜写过这些诗词 邓煜 李少君 徐俪成 | 澎湃新闻记者 徐萧 整理北京时间7月23日晚,中国数学家王虹、邓煜获颁菲尔...
中国海警局新闻发言人就菲位南海... 中国海警局新闻发言人姜略表示,7月24日,菲律宾组织7艘公务船、3艘海警舰、1艘运鱼船,并唆使大量渔...
外交部回应美国对数十国加征关税 7月24日,外交部发言人林剑主持例行记者会。有记者问:美国新一轮关税措施将于今天生效,外交部能否就此...
台湾岛以东相关海域治理进入经济... 为全面了解我国管辖海域渔业资源状况,7月17日至23日,农业农村部中国水产科学研究院东海水产研究所,...