从xml创建没有架构的Jaxb

从xml创建没有架构的Jaxb

本文介绍了从xml创建没有架构的Jaxb类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个简单的jaxb Java类来表示以下xml

How Do I create a simple jaxb Java class to represent the following xml

<rootelem>
  <myelem name="abc" myatt="true"/>

  <myelem name="def">
     <Key value="newvalue"/>
  </myelem>
  <myelem name="xyz">
     <Key value="42"/>
  </myelem>
</rootelem>

可以有多个 myelem ,每个 myelem 可以包含多个

There can be multiple myelem and each myelem can contain multiple key

我不想使用xsd

推荐答案

这是我们用来使用类和没有XSD的JAXB转换为XML的类的副本。 (我们也使用JAXB来生成我们的XSD。)

Here is a copy of a class we use to convert to/from XML using JAXB using classes and no XSD. (We also use JAXB to generate our XSD).

编辑:我刚刚重新阅读了这个问题。如果您正在询问如何从该XML生成Java源代码,那么您将不得不自己解决这个问题,或者使用XSD并使用JAXB将其转换为类。如果您已经拥有该类并且想要将XML转换为Java对象,那么下面的代码将适合您。

I just re-read the question. If you're asking how to generate the Java source from that XML, then you're going to have to figure that out on your own or use an XSD and use JAXB to convert it to classes. If you already have the class and you want to convert the XML into a Java object, then my code below will work for you.

package com.mycompany.types;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlTransient;

/**
 * Utility class to make it convenient to marshal and unmarshal the classes
 * generated by JAXB.
 */
@XmlTransient
public final class Utility {

  //
  // Static initialization
  //

  static {
    try {
      JAXB_CONTEXT = JAXBContext.newInstance(TestClass.class);
// The following fails with a javax.xml.bind.JAXBException.
// class mycompany.types.TestClass nor any of its super class is known
// to this context.
//      JAXB_CONTEXT =
//        JAXBContext.newInstance("com.mycompany.types",
//                                Utility.class.getClassLoader());
    }
    catch (Exception e) {
      throw new ExceptionInInitializerError(e);
    }
  }

  //
  // Constructors
  //

  //
  // Hidden constructor that prevents an object from being created.
  //
  private Utility() {
    // Do nothing.
  }

  //
  // Additional methods
  //

  /**
   * Unmarshals an XML string to a TestClass object.
   *
   * @param xml the XML string to parse
   * @return the resulting TestClass
   * @throws JAXBException if there are XML errors
   */
  public static TestClass parseTestClass(String xml) throws JAXBException {
    Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
    return (TestClass)unmarshaller.unmarshal(new StringReader(xml));
  }

  /**
   * Marshals a TestClass object to an XML string.
   *
   * @param testClass
   * @return the resulting XML string
   * @throws JAXBException if there are XML errors
   */
  public static String printTestClass(TestClass testClass) throws JAXBException {
    Marshaller marshaller = JAXB_CONTEXT.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    StringWriter writer = new StringWriter();
    marshaller.marshal(testClass, writer);
    return writer.toString();
  }

  //
  // Attributes
  //

  private static final JAXBContext JAXB_CONTEXT;

}

这篇关于从xml创建没有架构的Jaxb类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:47