将Java对象转换为XML并使用DOM/SAX对其进行解析
我需要将Java对象转换为XML对象,并将其从服务器发送到客户端浏览器 在客户端浏览器中,我需要使用DOM/SAX或任何适合的工具解析获得的XML对象,并在UI中显示它将Java对象转换为XML并使用DOM/SAX对其进行解析,xml,json,jaxb,marshalling,xstream,Xml,Json,Jaxb,Marshalling,Xstream,我需要将Java对象转换为XML对象,并将其从服务器发送到客户端浏览器 在客户端浏览器中,我需要使用DOM/SAX或任何适合的工具解析获得的XML对象,并在UI中显示它 哪一个适合上面的?他们中有谁能帮我解决这个问题吗?看看,它使用注释在xml和java字段之间进行映射,非常方便。看看,它使用注释在xml和java字段之间进行映射,非常方便。您可以使用JAXBAPI解决您的问题。JAXB(用于XML绑定的Java体系结构)使用注释将Java对象与XML内容进行转换。JAXB解决了以下问题: 编组
哪一个适合上面的?他们中有谁能帮我解决这个问题吗?看看,它使用注释在xml和java字段之间进行映射,非常方便。看看,它使用注释在xml和java字段之间进行映射,非常方便。您可以使用JAXBAPI解决您的问题。JAXB(用于XML绑定的Java体系结构)使用注释将Java对象与XML内容进行转换。JAXB解决了以下问题: 编组–将Java对象转换为XML内容 解组–将XML内容转换为Java对象
您可以在找到JAXB的简单示例。您可以使用JAXBAPI解决您的问题。JAXB(用于XML绑定的Java体系结构)使用注释将Java对象与XML内容进行转换。JAXB解决了以下问题: 编组–将Java对象转换为XML内容 解组–将XML内容转换为Java对象
您可以在找到JAXB的简单示例,这里有一个示例JAVA类使用JAXB将JAVA对象转换为XML内容
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
class JavaToXMLContent {
public static void main(String[] args) throws Throwable {
// =============================================================================================================
// Setup JAXB
// =============================================================================================================
// Create a JAXB context passing in the class of the object we want to marshal/unmarshal
final JAXBContext context = JAXBContext.newInstance(JavaObject.class);
// =============================================================================================================
// Marshalling OBJECT to XML
// =============================================================================================================
// Create the marshaller, this is the nifty little thing that will actually transform the object into XML
final Marshaller marshaller = context.createMarshaller();
// Create a stringWriter to hold the XML
final StringWriter stringWriter = new StringWriter();
// Create the sample object we wish to transform into XML
final JavaObject javaObject = new JavaObject();
javaObject.setName("Json");
javaObject.setRole("Moderator");
javaObject.setAge(28);
// Marshal the javaObject and write the XML to the stringWriter
marshaller.marshal(javaObject, stringWriter);
// Print out the contents of the stringWriter
System.out.println(stringWriter.toString());
// =============================================================================================================
// Unmarshalling XML to OBJECT
// =============================================================================================================
// Create the unmarshaller, this is the nifty little thing that will actually transform the XML back into an object
final Unmarshaller unmarshaller = context.createUnmarshaller();
// Unmarshal the XML in the stringWriter back into an object
final JavaObject javaObject2 = (JavaObject) unmarshaller.unmarshal(new StringReader(stringWriter.toString()));
// Print out the contents of the JavaObject we just unmarshalled from the XML
System.out.println(javaObject2.toString());
}
/**
* JavaObject is the sample object we've created to use for marshalling to and from XML.
* Make sure you have the @XmlRootElement annotation at the top there as well or JAXB
* might moan.
*/
@XmlRootElement
private static class JavaObject {
private String name;
private String role;
private int age;
public JavaObject() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Name [" + this.name + "], Role [" + this.role + "], Age [" + this.age + "]";
}
}
}
希望这能对您有所帮助。下面是一个示例JAVA类,它使用JAXB将JAVA对象转换为XML内容
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
class JavaToXMLContent {
public static void main(String[] args) throws Throwable {
// =============================================================================================================
// Setup JAXB
// =============================================================================================================
// Create a JAXB context passing in the class of the object we want to marshal/unmarshal
final JAXBContext context = JAXBContext.newInstance(JavaObject.class);
// =============================================================================================================
// Marshalling OBJECT to XML
// =============================================================================================================
// Create the marshaller, this is the nifty little thing that will actually transform the object into XML
final Marshaller marshaller = context.createMarshaller();
// Create a stringWriter to hold the XML
final StringWriter stringWriter = new StringWriter();
// Create the sample object we wish to transform into XML
final JavaObject javaObject = new JavaObject();
javaObject.setName("Json");
javaObject.setRole("Moderator");
javaObject.setAge(28);
// Marshal the javaObject and write the XML to the stringWriter
marshaller.marshal(javaObject, stringWriter);
// Print out the contents of the stringWriter
System.out.println(stringWriter.toString());
// =============================================================================================================
// Unmarshalling XML to OBJECT
// =============================================================================================================
// Create the unmarshaller, this is the nifty little thing that will actually transform the XML back into an object
final Unmarshaller unmarshaller = context.createUnmarshaller();
// Unmarshal the XML in the stringWriter back into an object
final JavaObject javaObject2 = (JavaObject) unmarshaller.unmarshal(new StringReader(stringWriter.toString()));
// Print out the contents of the JavaObject we just unmarshalled from the XML
System.out.println(javaObject2.toString());
}
/**
* JavaObject is the sample object we've created to use for marshalling to and from XML.
* Make sure you have the @XmlRootElement annotation at the top there as well or JAXB
* might moan.
*/
@XmlRootElement
private static class JavaObject {
private String name;
private String role;
private int age;
public JavaObject() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Name [" + this.name + "], Role [" + this.role + "], Age [" + this.age + "]";
}
}
}
希望这对您有所帮助。谢谢。它会将Java转换为XML文件还是XML对象?因为我需要将其转换为xml对象,因为我需要将其传递给客户机,所以在客户机中,我需要解析xml内容并在UIT中显示它,它将是xml内容。您可以将XML内容放在XML文件中,也可以将其作为XML字符串传递给客户端。是否可以将静态java类转换为xmlstring?是的,当您要在XML内容中封送的类是带有修饰符
private static
的本地bean时,可能会出现这种情况。谢谢。它会将Java转换为XML文件还是XML对象?因为我需要将其转换为xml对象,因为我需要将其传递给客户机,所以在客户机中,我需要解析xml内容并在UIT中显示它,它将是xml内容。您可以将XML内容放在XML文件中,或者将其作为XML字符串传递给客户端。是否可以将静态java类转换为xmlstring?是的,当您要在XML内容中封送的类是带有修饰符private static
的本地bean时,可能会出现这种情况。