溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java XML處理工具

發(fā)布時間:2020-06-04 18:43:16 來源:網(wǎng)絡(luò) 閱讀:249 作者:專注地一哥 欄目:web開發(fā)

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 ?是否轉(zhuǎn)義

?*/

public static void writeXML(Document root,String path,boolean flag) {

OutputFormat format = new OutputFormat(" ???",flag); ?

??format.setEncoding("utf-8");//設(shè)置編碼格式 ?

??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(){ //點(diǎn)差?http://www.fx61.com/faq/HantecGlobal/480.html

OutputFormat format = new OutputFormat(" ???",true); ?

??format.setEncoding("utf-8");//設(shè)置編碼格式 ?

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

????????// 根節(jié)點(diǎn)名稱

????????xmlSerializer.setRootName("resource");

????????// 不對類型進(jìn)行設(shè)置

????????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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI