精华内容
下载资源
问答
  • Unmarshaller 使客户端应用程序能够将 XML 数据转换Java 内容对象树。备注:marshal(序列化、排列、整理)Marshaller 使客户端应用程序能够将 Java 内容树转换XML 数据。package hb.jaxb;public class ...

    Unmarshaller 类使客户端应用程序能够将 XML 数据转换为 Java 内容对象树。

    备注:marshal(序列化、排列、整理)

    Marshaller 类使客户端应用程序能够将 Java 内容树转换回 XML 数据。

    package hb.jaxb;

    public class Classroom {

    private int id;

    private String name;

    private int grade;

    public int getId() {

    return id;

    }

    public void setId(int id) {

    this.id = id;

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public int getGrade() {

    return grade;

    }

    public void setGrade(int grade) {

    this.grade = grade;

    }

    public Classroom(int id, String name, int grade) {

    super();

    this.id = id;

    this.name = name;

    this.grade = grade;

    }

    public Classroom() {

    super();

    }

    }

    package hb.jaxb;

    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement

    public class Student {

    private int id;

    private String name;

    private int age;

    private Classroom classroom;

    public int getId() {

    return id;

    }

    public void setId(int id) {

    this.id = id;

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    this.age = age;

    }

    public Classroom getClassroom() {

    return classroom;

    }

    public void setClassroom(Classroom classroom) {

    this.classroom = classroom;

    }

    public Student(int id, String name, int age, Classroom classroom) {

    super();

    this.id = id;

    this.name = name;

    this.age = age;

    this.classroom = classroom;

    }

    //无参够着函数一定需要,否则JXBContext无法正常解析。

    public Student() {

    super();

    }

    }

    注意:

    1、需要转换的model对象一定要添加@XmlRootElement注解,其里面的其他对象则不需要

    2、需要转换的model对象一定要有不带参数的构造方法,包括该对象里面引用的对象。

    package hb.jaxb;

    import java.io.StringReader;

    import javax.xml.bind.JAXBContext;

    import javax.xml.bind.JAXBException;

    import javax.xml.bind.Marshaller;

    import javax.xml.bind.Unmarshaller;

    import org.junit.Test;

    public class TestJaxb {

    @Test

    public void beanToXML() {

    Classroom classroom = new Classroom(1, "软件工程", 4);

    Student student = new Student(101, "张三", 22, classroom);

    try {

    JAXBContext context = JAXBContext.newInstance(Student.class);

    Marshaller marshaller = context.createMarshaller();

    marshaller.marshal(student, System.out);

    } catch (JAXBException e) {

    e.printStackTrace();

    }

    }

    @Test

    public void XMLStringToBean(){

    String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>2241软件工程101张三";

    try {

    JAXBContext context = JAXBContext.newInstance(Student.class);

    Unmarshaller unmarshaller = context.createUnmarshaller();

    Student student = (Student)unmarshaller.unmarshal(new StringReader(xmlStr));

    System.out.println(student.getAge());

    System.out.println(student.getClassroom().getName());

    } catch (JAXBException e) {

    e.printStackTrace();

    }

    }

    }

    展开全文
  • Unmarshaller 能将 XML 数据转换Java 内容对象。Marshaller 能够将 Java对象转换XML 数据。packagejaxb;/*** Created by sheting on 10/17/2017*/public classClassroom {private intid;privateString ...

    Unmarshaller 类能将 XML 数据转换为 Java 内容对象。

    Marshaller 类能够将 Java 对象转换回 XML 数据。

    packagejaxb;/*** Created by sheting on 10/17/2017*/

    public classClassroom {private intid;privateString name;private intgrade;publicClassroom() {

    }public Classroom(int id, String name, intgrade) {super();this.id =id;this.name =name;this.grade =grade;

    }public intgetId() {returnid;

    }public void setId(intid) {this.id =id;

    }publicString getName() {returnname;

    }public voidsetName(String name) {this.name =name;

    }public intgetGrade() {returngrade;

    }public void setGrade(intgrade) {this.grade =grade;

    }

    }

    packagejaxb;importjavax.xml.bind.annotation.XmlRootElement;/*** Created by sheting on 10/17/2017*/@XmlRootElementpublic classStudent {private intid;privateString name;private intage;privateClassroom classroom;public intgetId() {returnid;

    }public void setId(intid) {this.id =id;

    }publicString getName() {returnname;

    }public voidsetName(String name) {this.name =name;

    }public intgetAge() {returnage;

    }public void setAge(intage) {this.age =age;

    }publicClassroom getClassroom() {returnclassroom;

    }public voidsetClassroom(Classroom classroom) {this.classroom =classroom;

    }public Student(int id, String name, intage, Classroom classroom) {super();this.id =id;this.name =name;this.age =age;this.classroom =classroom;

    }//无参够着函数一定需要,否则JXBContext无法正常解析。

    publicStudent() {super();

    }

    }

    packagejaxb;importorg.junit.Test;importjavax.xml.bind.JAXBContext;importjavax.xml.bind.JAXBException;importjavax.xml.bind.Marshaller;importjavax.xml.bind.Unmarshaller;importjava.io.StringReader;/*** Created by sheting on 10/17/2017*/

    public classTestJaxb {

    @Testpublic voidbeanToXML() {

    Classroom classroom= new Classroom(1, "软件工程", 4);

    Student student= new Student(101, "张三", 22, classroom);try{

    JAXBContext context= JAXBContext.newInstance(Student.class);

    Marshaller marshaller=context.createMarshaller();

    marshaller.marshal(student, System.out);

    }catch(JAXBException e) {

    e.printStackTrace();

    }

    }

    @Testpublic voidXMLStringToBean(){

    String xmlStr= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>2241软件工程101张三";try{

    JAXBContext context= JAXBContext.newInstance(Student.class);

    Unmarshaller unmarshaller=context.createUnmarshaller();

    Student student= (Student)unmarshaller.unmarshal(newStringReader(xmlStr));

    System.out.println(student.getAge());

    System.out.println(student.getClassroom().getName());

    }catch(JAXBException e) {

    e.printStackTrace();

    }

    }

    }

    注意:

    1、需要转换的model对象一定要添加@XmlRootElement注解,其里面的其他对象则不需要

    2、需要转换的model对象一定要有不带参数的构造方法,包括该对象里面引用的对象。

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。

    JAXBContext 类提供到 JAXB API 的客户端入口点。它提供了管理实现 JAXB 绑定框架操作所需的 XML/Java 绑定信息的抽象,这些操作包括:解组、编组和验证。

    展开全文
  • //转换类 JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); // 格式化输出的格式 marshaller.setProperty(Marshaller.JAXB_FRAGNENT,true);//...
    //输出
    StringWriter sw = new StringWriter();
    //转换类
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller marshaller = context.createMarshaller();
    // 格式化输出的格式
    marshaller.setProperty(Marshaller.JAXB_FRAGNENT,true);//去掉xml默认生成的报文头
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);//设置xml格式
    marshaller.setProperty(Marshaller.JAXB_ENCODING,"GBK");//设置编码格式
    marshaller.marshal(obj, sw);
    
    展开全文
  • public class XmlHelper {/*** Object转XML** @param object* @return* @throws Exception*/public static String ObjectToXml(Object object) throws Exception {JAXBContext context = JAXBContext.newInstance...

    public class XmlHelper {

    /**

    * Object转XML

    *

    * @param object

    * @return

    * @throws Exception

    */

    public static String ObjectToXml(Object object) throws Exception {

    JAXBContext context = JAXBContext.newInstance(object.getClass()); // 获取上下文对象

    Marshaller marshaller = context.createMarshaller(); // 根据上下文获取marshaller对象

    marshaller.setProperty(Marshaller.JAXB_ENCODING, "GB2312"); // 设置编码字符集

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化XML输出,有分行和缩进

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    marshaller.marshal(object, baos);

    String xmlObj = new String(baos.toByteArray()); // 生成XML字符串

    return xmlObj.trim();

    }

    /**

    * Object转XML

    *

    * @param object

    * @return

    * @throws Exception

    */

    public static String ObjectToXmlUtf8(Object object) throws Exception {

    JAXBContext context = JAXBContext.newInstance(object.getClass()); // 获取上下文对象

    Marshaller marshaller = context.createMarshaller(); // 根据上下文获取marshaller对象

    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // 设置编码字符集

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化XML输出,有分行和缩进

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    marshaller.marshal(object, baos);

    String xmlObj = new String(baos.toByteArray()); // 生成XML字符串

    return xmlObj.trim();

    }

    /**

    * XML转Object

    *

    * @param xmlStr

    * @param classz

    * @return

    */

    public static Object XmlToObject(String xmlStr, Class classz) {

    try {

    JAXBContext context = JAXBContext.newInstance(classz);

    Unmarshaller unmarshaller = context.createUnmarshaller();

    Object object = unmarshaller.unmarshal(new StringReader(xmlStr));

    return object;

    } catch (JAXBException ex) {

    ex.printStackTrace();

    return null;

    }

    }

    /**

    * xml格式字符串获取所有子节点存入list,每个List分两个部分:name和value,中间用“:”隔开

    *

    * @param srcXml

    * @return

    */

    public static List getListXML(String srcXml) {

    //存储xml元素信息的容器

    List elemList = new ArrayList();

    List list = new ArrayList();

    Document srcdoc = null;

    try {

    srcdoc = DocumentHelper.parseText(srcXml);

    } catch (DocumentException e) {

    e.printStackTrace();

    }

    Element elem = srcdoc.getRootElement();

    getElementList(elem, elemList);

    for (Iterator it = elemList.iterator(); it.hasNext(); ) {

    Leaf leaf = it.next();

    list.add(leaf.getName() + ":" + leaf.getValue());

    }

    return list;

    }

    /**

    * 递归遍历方法

    *

    * @param element

    */

    public static void getElementList(Element element, List elemList) {

    List elements = element.elements();

    if (elements.size() == 0) {

    //没有子元素

    //String xpath = element.getPath();

    String xpath = element.getName();

    String value = element.getTextTrim();

    Leaf leaf = new Leaf(xpath, value);

    elemList.add(leaf);

    } else {

    //有子元素

    for (Iterator it = elements.iterator(); it.hasNext(); ) {

    Element elem = (Element) it.next();

    //递归遍历

    getElementList(elem, elemList);

    }

    }

    }

    static class Leaf {

    private String name;

    private String value;

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public String getValue() {

    return value;

    }

    public void setValue(String value) {

    this.value = value;

    }

    public Leaf(String name, String value) {

    this.name = name;

    this.value = value;

    }

    }

    }

    展开全文
  • 将字符串类型的字符串转换成的Java对象时,需要先将字符的格式转换成JSON格式的字符串(所以用到了json.jar包),在使用JSON格式的字符串转换成的Java对象类型字符串原类型(XML格式): 将字符串类型的字符串转换...
  • public class XmlHelper {/*** Object转XML** @param object* @return* @throws Exception*/public static String ObjectToXml(Object object) throws Exception {JAXBContext context = JAXBContext.newInstance...
  • public class XmlHelper {/*** Object转XML** @param object* @return* @throws Exception*/public static String ObjectToXml(Object object) throws Exception {JAXBContext context = JAXBContext.newInstance...
  • package javax.utils;import java.io.File;import java.io....import java.io.StringReader;import java.io.StringWriter;import java.net.URL;import java.nio.file.Paths;import org.apache.commons.io.F...
  • 在本XML教程中,我们将学习-将XML string to XML转换string to XML文档将XML文件内容转换XML文档1) Convert String to XML Document要将XML字符串转换XML Dom ,我们需要以下:javax.xml.parsers....
  • package com.lppz.wms.bir.util; import java.io.StringReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Mars
  • public class XmlHelper { /** * Object转XML * * @param object * @return * @throws Exception */ public static String ObjectToXml(Object object) throws Exception { ...
  • xml文件中,根元素是...编辑以下是上述xml的pojo:public class MyXML{private String string; // Change the class as String is Wrapper classpublic String getString (){return string;}public void setStr...
  • 源代码可看:https://pan.baidu.com/s/1jniElgsZ4g39ISz1Vl5U-g,里面有xstream.jar包结构显示样式: ItemInterface:public class ItemInterface {private String Sender = "";private String Receiver = "";...
  • 1.首先要写工具,处理读取和写入xml文件使用的工具。XMLUtil.javaimport java.io.FileInputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io....
  • 如何将XML转换java对象 /** * XML转成对象 * * @param * @param inputXML 需要转对象XML * @param classes 包含的类类对象里的自定义,数组传入 * @return 返回对象 */ public static T toEntity(String ...
  • xstream转换xmlJava对象

    千次阅读 2018-08-01 10:08:45
    源代码可看:... 结构显示样式: ItemInterface: public class ItemInterface {  private String Sender = "";  private String Receiver = "";  ...
  • 先看工具:import org.slf4j.Logger;import javax.xml.bind.JAXBContext;import javax.xml.bind.Marshaller;import javax.xml.bind....import java.io.StringReader;import java.io.StringWriter;import stati...
  • 直接上代码,我就不废话了!xml.js类似于java中的工具,通用!xml.js文件/** * xml工具 * xml对象和String字符串之间的转换 ...function string2XML(xmlString) { // for IE(IE浏览器) if (wi...
  • java对象转换xml格式字符串

    千次阅读 2017-06-13 22:35:49
    它提供了管理实现 JAXB 绑定框架操作所需的 XML/Java 绑定信息的抽象,这些操作包括:解组、编组和验证。 1.编写工具 /** * Jaxb工具 * * @author LittleNewbie * @date 2016年10月11日 */ public ...
  • Unmarshaller 使客户端应用程序能够将 XML 数据转换Java 内容对象树。 备注:marshal(序列化、排列、整理) Marshaller 使客户端应用程序能够将 Java 内容树转换XML 数据。 package hb.jaxb; ...
  • JAXB可以轻松实现java对象xml的互相转换 首先准备好一个JavaBean供实验: 注意 1、文件注解:@XmlRootElement不可缺少 2、2个Student的构造方法不能少   XmlToJava //xml转换Java @Test public void test02...
  • 1.首先要写工具,处理读取和写入xml文件使用的工具。XMLUtil.javaimportjava.io.FileInputStream;importjava.io.FileWriter;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.String...
  • 工具public class Ben2XmlUtil {/*** 将对象直接转换String类型的 XML输出** @param obj* @return*/public static String convertToXml(Object obj) {// 创建输出流StringWriter sw = new StringWriter();...
  • XMLJava对象转换十分方便,使用 xStream.fromXML() 和 xStream.toXML()方法即可。 这里对复杂XML与JavaBean的转换问题进行记录: 首先,是复杂XML的格式,如下: <Parameters> <InImgtype=...
  • 1 public classJaxbUtil {23 /**4 * java对象转换xml文件5 *6 *@paramxmlPath xml文件路径7 *@paramload java对象.Class8 *@returnxml文件的String9 *@throwsJAXBException10 */11 public static String beanToXml...
  • 1 public classJaxbUtil {23 /**4 * java对象转换xml文件5 *6 *@paramxmlPath xml文件路径7 *@paramload java对象.Class8 *@returnxml文件的String9 *@throwsJAXBException10 */11 public static String beanToXml...
  • 测试:package com.yanek.test;import java.util.ArrayList;import java.util.List;import ...public class Test {/*** @param args*/public static void main(String[] args)...
  • 给出XML示例:LOLOLOLOL哪个应该映射到:...不应转换XML,它是作为解析的org.w3c.dom.Document对象提供的.>不会也不会将1:1映射到XML.>我只想将XML的特定路径映射到对象的特定字段.我的梦想解决方案如下:@...
  • XmlUtil:不多说,直接撸代码: ... * java 转换xml * @Title: toXml * @Description: TODO * @param obj 对象实例 * @return String xml字符串 */ public static String toXml(...

空空如也

空空如也

1 2 3 4 5 ... 13
收藏数 242
精华内容 96
关键字:

java类转换xmlstring对象

java 订阅