A. 請問java中怎麼將生成的XML轉化為字元串
此方法傳入文件名即可獲得對象
-------------------------------------------
import java.beans.XMLDecoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
public class ReadXml {
public static Object getXml(String path){
File file=new File(path);
FileInputStream fileInputStream;
Object object=null;
try {
fileInputStream = new FileInputStream(file);
XMLDecoder decoder=new XMLDecoder(fileInputStream);
object=decoder.readObject();
fileInputStream.close();
decoder.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return object;
}
}
---------------------------------------------------------------------
如果不可以 用這個寫的方式 直接寫對象到XML里 此方法需傳入對象和寫入路徑
mport java.beans.XMLEncoder;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteXml {
public static void setXml(Object o,String fileName){
File file=new File(fileName);
try {
FileOutputStream fileOutputStream=new FileOutputStream(file);
XMLEncoder encoder=new XMLEncoder(fileOutputStream);
encoder.writeObject(o);
encoder.flush();
encoder.close();
fileOutputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
B. XStream將java對象解析為xml字元串時,過濾掉節點值為空的節點
使用註解 @XStreamOmitField 或者omitField(definedIn, fieldName)即可
C. java中xml的Document對象怎麼轉成String形式
/***
* 將Document對象轉換成String
* @param document
* @return
*/
public String transformXMLToString(Document document) {
try {
XMLOutputter xmlout = new XMLOutputter();
Format tFormat = Format.getPrettyFormat();
tFormat.setEncoding("GBK");
xmlout.setFormat(tFormat);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
xmlout.output(document, bo);
String xmlStr = bo.toString();
bo.flush();
bo.close();
return xmlStr;
} catch (Exception e) {
LogUtil.getLogger().error(LogUtil.getExceptionDetail(e));
return null;
}
}
D. java json字元串轉換xml字元串
java json字元串轉成 xml可以用XStream 這個jar來轉換, 首先要把json轉成java對象, 再轉成xml字元串.
E. Java中object和xml互相轉換
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Object2XML {
public static String object2XML(Object obj, String outFileName)
throws FileNotFoundException {
// 構造輸出XML文件的位元組輸出流
File outFile = new File(outFileName);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(outFile));
// 構造一個XML編碼器
XMLEncoder xmlEncoder = new XMLEncoder(bos);
// 使用XML編碼器寫對象
xmlEncoder.writeObject(obj);
// 關閉編碼器
xmlEncoder.close();
return outFile.getAbsolutePath();
}
public static Object xml2Object(String inFileName)
throws FileNotFoundException {
// 構造輸入的XML文件的位元組輸入流
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(inFileName));
// 構造一個XML解碼器
XMLDecoder xmlDecoder = new XMLDecoder(bis);
// 使用XML解碼器讀對象
Object obj = xmlDecoder.readObject();
// 關閉解碼器
xmlDecoder.close();
return obj;
}
public static void main(String[] args) throws IOException {
// 構造一個StudentBean對象
StudentBean student = new StudentBean();
student.setName("wamgwu");
student.setGender("male");
student.setAge(15);
student.setPhone("55556666");
// 將StudentBean對象寫到XML文件
String fileName = "AStudent.xml";
Object2XML.object2XML(student, fileName);
// 從XML文件讀StudentBean對象
StudentBean aStudent = (StudentBean)Object2XML.xml2Object(fileName);
// 輸出讀到的對象
System.out.println(aStudent.toString());
}
}
F. java中xml的Document對象怎麼轉成String形式
package com.webdesk.swing.powertable.util; import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileWriter;import java.io.IOException; import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter; public class XmlUtil { public static String xmlChangeString(String fileName){ try { SAXReader saxReader = new SAXReader();//新建一個解析類 Document tempDocument = saxReader.read(XmlUtil.class.getClassLoader().getResourceAsStream(fileName));//讀入一個文件 return tempDocument.asXML(); } catch (DocumentException e) { e.printStackTrace(); } return null; } //將字元串string類型轉換成xml文件 public static void strChangeXML(String str) throws IOException { SAXReader saxReader = new SAXReader(); Document document; try { document = saxReader.read(new ByteArrayInputStream(str.getBytes("UTF-8"))); OutputFormat format = OutputFormat.createPrettyPrint(); /** 將document中的內容寫入文件中 */ XMLWriter writer = new XMLWriter(new FileWriter(new File("src/com/webdesk/swing/powertable/digester/cctv.xml")),format); writer.write(document); writer.close(); } catch (DocumentException e) { e.printStackTrace(); } }}
G. java中xml的Document對象怎麼轉成String形式
/***
* 將Document對象轉換成String
* @param document
* @return
*/
public String transformXMLToString(Document document) {
try {
XMLOutputter xmlout = new XMLOutputter();
Format tFormat = Format.getPrettyFormat();
tFormat.setEncoding("GBK");
xmlout.setFormat(tFormat);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
xmlout.output(document, bo);
String xmlStr = bo.toString();
bo.flush();
bo.close();
return xmlStr;
} catch (Exception e) {
LogUtil.getLogger().error(LogUtil.getExceptionDetail(e));
return null;
}
}
H. java如何解析xml格式的字元串
使用dom4j,在網路下搜一個dom4j包,然後在網上找個例子看dom4j操作xml的使用方法,很簡單的。
I. java XML創建 Element 轉為字元串,如果節點為空 則會變成<TrainNo/> 可以改為<TrainNo></TrainNo>嗎
這個沒必要改吧,<TrainNo/>這種方式在XML中表示空元素,不影響被解析。
J. java 對象序列化到xml java 對象到xml 轉化為string
marshaller.marshal,明顯就有很多參數。用OutputStream那個就可以寫入String了。。
用XmlStreamWriter也是可以。