Ⅰ java 將Object轉化為String類型的XML
只能拼接字元串。把要的xml不變的寫出來,然後把要變的用變數進行填補
Ⅱ 求 Java序列化對象轉xml方法....
/**這里應用了JAVA的Marshall方法
*對象轉xml
*返回xml
*@paramtXLife
*@return
*/
publicstaticStringtXLiftToXML(com.TXLifetXLife){
Stringxml="";
try{
ByteArrayOutputStreamout=newByteArrayOutputStream();
JAXBContextjc=JAXBContext
.newInstance("com");//包的命名空間
Marshallerm=null;
synchronized(jc){
m=jc.createMarshaller();
}
m.setProperty(Marshaller.JAXB_ENCODING,"GBK");
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
m.marshal(tXLife,out);
xml=out.toString();//賦值
}catch(Exceptione){
xml=null;
}
returnxml;
}
Ⅲ java 對象序列化到xml java 對象到xml 轉化為string
marshaller.marshal,明顯就有很多參數。用OutputStream那個就可以寫入String了。。
用XmlStreamWriter也是可以。
Ⅳ 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());
}
}
Ⅳ java如何把String轉換成xml
二種方式:
1、直接拼接字元串,如下
StringBuffer xml = new StringBuffer();
xml.append("<xml>");
xml.append("\r\n");
Iterator<Map.Entry<String, String>> entries = jovalue.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, String> entry = entries.next();
xml.append("<"+entry.getKey()+">");
xml.append(entry.getValue());
xml.append("</"+entry.getKey()+">");
xml.append("\r\n");
}
xml.append("</xml>");
return xml.toString();
用第三方包:dom4j-1.6.1.jar,示例如下
Document document = DocumentHelper.createDocument();
Element xml = document.addElement("xml");
Iterator<Map.Entry<String, String>> entries = jovalue.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, String> entry = entries.next();
Element xml_sub = xml.addElement(entry.getKey());
xml_sub.setText(entry.getValue());
}
return document.asXML();
Ⅵ java對象如何轉換成xml
rt.jar就可以操作xml了,其次是dom4j,別的都沒有什麼名氣
Ⅶ java怎麼將XML轉成對象
給你一個專業的轉換例子說明:
http://blog.csdn.net/is_zhoufeng/article/details/8273008
看完後你就明白了。