是的,是的,我知道有很多关于这个主题的问题.但我仍然无法找到解决问题的方法.我有一个属性注释的Java对象.例如Customer,就像在这个例子中一样.我想要一个String表示它.谷歌建议将JAXB用于此类目的.但是在所有示例中,创建的XML文件都打印到文件或控制台,如下所示:
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);
Run Code Online (Sandbox Code Playgroud)
但我必须使用此对象并以XML格式通过网络发送.所以我想得到一个代表XML的String.
String xmlString = ...
sendOverNetwork(xmlString);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
A4L*_*A4L 103
您可以使用Marshaler的编组方法将Writer作为参数:
并传递一个可以构建String对象的Implementation
直接已知子类: BufferedWriter,CharArrayWriter,FilterWriter,OutputStreamWriter,PipedWriter,PrintWriter,StringWriter
调用其toString方法以获取实际的String值.
这样做:
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(customer, sw);
String xmlString = sw.toString();
Run Code Online (Sandbox Code Playgroud)
jul*_*ano 32
一个方便的选择是使用javax.xml.bind.JAXB:
StringWriter sw = new StringWriter();
JAXB.marshal(customer, sw);
String xmlString = sw.toString();
Run Code Online (Sandbox Code Playgroud)
反向过程(unmarshal)将是:
Customer customer = JAXB.unmarshal(new StringReader(xmlString), Customer.class);
Run Code Online (Sandbox Code Playgroud)
这种方法无需处理已检查的异常.
小智 30
正如A4L所提到的,你可以使用StringWriter.这里提供示例代码:
private static String jaxbObjectToXML(Customer customer) {
String xmlString = "";
try {
JAXBContext context = JAXBContext.newInstance(Customer.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML
StringWriter sw = new StringWriter();
m.marshal(customer, sw);
xmlString = sw.toString();
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlString;
}
Run Code Online (Sandbox Code Playgroud)
你可以将它编组为a StringWriter并抓住它的字符串.来自toString().
在 Java 中将对象转换为 XML
客户.java
package com;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author ABsiddik
*/
@XmlRootElement
public class Customer {
int id;
String name;
int age;
String address;
ArrayList<String> mobileNo;
public int getId() {
return id;
}
@XmlAttribute
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
@XmlElement
public void setAddress(String address) {
this.address = address;
}
public ArrayList<String> getMobileNo() {
return mobileNo;
}
@XmlElement
public void setMobileNo(ArrayList<String> mobileNo) {
this.mobileNo = mobileNo;
}
}
Run Code Online (Sandbox Code Playgroud)
ConvertObjToXML.java
package com;
import java.io.File;
import java.io.StringWriter;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
/**
*
* @author ABsiddik
*/
public class ConvertObjToXML {
public static void main(String args[]) throws Exception
{
ArrayList<String> numberList = new ArrayList<>();
numberList.add("01942652579");
numberList.add("01762752801");
numberList.add("8800545");
Customer c = new Customer();
c.setId(23);
c.setName("Abu Bakar Siddik");
c.setAge(45);
c.setAddress("Dhaka, Bangladesh");
c.setMobileNo(numberList);
File file = new File("C:\\Users\\NETIZEN-ONE\\Desktop \\customer.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(c, file);// this line create customer.xml file in specified path.
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(c, sw);
String xmlString = sw.toString();
System.out.println(xmlString);
}
}
Run Code Online (Sandbox Code Playgroud)
尝试这个例子..