javax.xml.bind.JAXB类的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(286)

本文整理了Java中javax.xml.bind.JAXB类的一些代码示例,展示了JAXB类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JAXB类的具体详情如下:
包路径:javax.xml.bind.JAXB
类名称:JAXB

JAXB介绍

[英]Class that defines convenience methods for common, simple use of JAXB.

Methods defined in this class are convenience methods that combine several basic operations in the JAXBContext, Unmarshaller, and Marshaller. They are designed to be the prefered methods for developers new to JAXB. They have the following characterstics:

  1. Generally speaking, the performance is not necessarily optimal. It is expected that people who need to write performance critical code will use the rest of the JAXB API directly.
  2. Errors that happen during the processing is wrapped into DataBindingException (which will have JAXBExceptionas its Throwable#getCause(). It is expected that people who prefer the checked exception would use the rest of the JAXB API directly.

In addition, the unmarshal methods have the following characteristic:

  1. Schema validation is not performed on the input XML. The processing will try to continue even if there are errors in the XML, as much as possible. Only as the last resort, this method fails with DataBindingException.

Similarly, the marshal methods have the following characteristic:

  1. The processing will try to continue even if the Java object tree does not meet the validity requirement. Only as the last resort, this method fails with DataBindingException.

All the methods on this class require non-null arguments to all parameters. The unmarshal methods either fail with an exception or return a non-null value.
[中]类,该类为JAXB的通用、简单使用定义方便的方法。
此类中定义的方法是方便的方法,它们结合了JAXBContext、Unmarshaller和Marshaller中的几个基本操作。它们被设计成JAXB新手的首选方法。它们具有以下特点:
1.一般来说,性能不一定是最优的。预计需要编写性能关键代码的人将直接使用JAXB API的其余部分。
1.处理过程中发生的错误被包装到DataBindingException中(这将使JaxBeException成为其可丢弃的#getCause()。希望喜欢检查异常的人会直接使用JAXB API的其余部分。
此外,解组方法具有以下特点:
1.未对输入XML执行架构验证。即使XML中存在错误,处理也将尽可能继续。只有在万不得已的情况下,此方法才会因DataBindingException而失败。
类似地,封送处理方法具有以下特征:
1.即使Java对象树不满足有效性要求,处理仍将尝试继续。只有在万不得已的情况下,此方法才会因DataBindingException而失败。
此类上的所有方法都要求所有参数都具有非null参数。解组方法要么因异常而失败,要么返回非null值。

代码示例

代码示例来源:origin: igniterealtime/Openfire

final HttpUriRequest getRequest = RequestBuilder.get(crowdServer.resolve(request.toString() + startIndex))
  .setConfig(requestConfig)
  .addHeader(HEADER_ACCEPT_APPLICATION_XML)
    handleHTTPError(response);
  final Groups groups = JAXB.unmarshal(response.getEntity().getContent(), Groups.class);
  if (groups != null && groups.group != null) {
    for (final Group group : groups.group) {

代码示例来源:origin: igniterealtime/Openfire

/**
 * Authenticates a user with crowd. If authentication failed, raises a <code>RemoteException</code>
 * @param username
 * @param password
 * @throws RemoteException
 */
public void authenticate(String username, String password) throws RemoteException {
  username = JID.unescapeNode(username);
  LOG.debug("authenticate '" + String.valueOf(username) + "'");
  final AuthenticatePost authenticatePost = new AuthenticatePost();
  authenticatePost.value = password;
  final StringWriter writer = new StringWriter();
  JAXB.marshal(authenticatePost, writer);
  final HttpUriRequest postRequest = RequestBuilder.post(crowdServer.resolve("authentication?username=" + urlEncode(username)))
    .setConfig(requestConfig)
    .setEntity(new StringEntity(writer.toString(), StandardCharsets.UTF_8))
    .setHeader(HEADER_CONTENT_TYPE_APPLICATION_XML)
    .build();
  try(final CloseableHttpResponse response = client.execute(postRequest, clientContext)) {
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
      handleHTTPError(response);
    }
    
  } catch (IOException ioe) {
    handleError(ioe);
  }
  
  LOG.info("authenticated user:" + username);
}

代码示例来源:origin: javax.xml.bind/jaxb-api

/**
 * Reads in a Java object tree from the given XML input.
 *
 * @param xml
 *      The XML infoset that the {@link Source} represents is read.
 */
public static <T> T unmarshal( Source xml, Class<T> type ) {
  try {
    JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(toSource(xml), type);
    return item.getValue();
  } catch (JAXBException e) {
    throw new DataBindingException(e);
  } catch (IOException e) {
    throw new DataBindingException(e);
  }
}

代码示例来源:origin: javax.xml.bind/jaxb-api

context = getContext(((JAXBElement<?>)jaxbObject).getDeclaredType());
  } else {
    Class<?> clazz = jaxbObject.getClass();
    XmlRootElement r = clazz.getAnnotation(XmlRootElement.class);
    context = getContext(clazz);
    if(r==null) {
      jaxbObject = new JAXBElement(new QName(inferName(clazz)),clazz,jaxbObject);
  Marshaller m = context.createMarshaller();
  m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
  m.marshal(jaxbObject, toResult(xml));
} catch (JAXBException e) {
  throw new DataBindingException(e);
} catch (IOException e) {
  throw new DataBindingException(e);

代码示例来源:origin: org.apache.openejb/javaee-api

public static <T> T unmarshal(String str, Class<T> type) {
  if (str == null) {
    throw new IllegalStateException("No string destination is given");
  }
  try {
    return unmarshal(new URI(str), type);
  } catch (URISyntaxException e) {
    return unmarshal(new File(str), type);
  }
}

代码示例来源:origin: org.apache.openejb/javaee-api

public static void marshal(Object object, String str) {
  if (str == null) {
    throw new IllegalStateException("No string destination is given");
  }
  try {
    marshal(object, new URI(str));
  } catch (URISyntaxException e) {
    marshal(object, new File(str));
  }
}

代码示例来源:origin: org.apache.servicemix.specs/org.apache.servicemix.specs.jaxb-api-2.1

public static <T> T unmarshal(URI uri, Class<T> type) {
  if (uri == null) {
    throw new IllegalStateException("No uri is given");
  }
  try {
    return unmarshal(uri.toURL(), type);
  } catch (MalformedURLException e) {
    throw new DataBindingException(e);
  }
}

代码示例来源:origin: org.apache.servicemix.specs/org.apache.servicemix.specs.jaxb-api-2.1

public static void marshal(Object object, URI uri) {
  if (uri == null) {
    throw new IllegalStateException("No uri is given");
  }
  try {
    marshal(object, uri.toURL());
  } catch (IOException e) {
    throw new DataBindingException(e);
  }
}

代码示例来源:origin: stackoverflow.com

Exception in thread "main" java.lang.IllegalArgumentException: URI is not absolute
 at java.net.URI.toURL(URI.java:1095)
 at javax.xml.bind.JAXB.toSource(JAXB.java:291)
 at javax.xml.bind.JAXB.unmarshal(JAXB.java:205)
 at forum23652823.Demo.main(Demo.java:8)

代码示例来源:origin: octo-online/reactive-audit

@Test(expected = FileReactiveAuditException.class)
  public void unmarshal_URL()
      throws MalformedURLException
  {
    TestTools.strict.commit();
    JAXB.unmarshal(IOTestTools.getTempFile().toURI().toURL(), null);
  }
}

代码示例来源:origin: octo-online/reactive-audit

@Test(expected = FileReactiveAuditException.class)
public void marshal_URL()
    throws MalformedURLException
{
  TestTools.strict.commit();
  JAXB.marshal(null, IOTestTools.getTempFile().toURI().toURL());
}

代码示例来源:origin: org.aktin/broker-client

public void setRequestTargetNodes(int requestId, int[] nodes) throws IOException{
  URI uri = getQueryURI(requestId);
  HttpURLConnection c = openConnection("PUT", uri.resolve(uri.getPath()+"/nodes"));
  c.setDoOutput(true);
  RequestTargetNodes tn = new RequestTargetNodes(nodes);
  c.setRequestProperty("Content-Type", "application/xml");
  try( OutputStream out = c.getOutputStream() ){
    JAXB.marshal(tn, out);
  }
  c.getInputStream().close();        
}
public void clearRequestTargetNodes(int requestId) throws IOException{

代码示例来源:origin: stackoverflow.com

javax.xml.bind.JAXB.unmarshal(stringFormOfOutput, Long.class)

代码示例来源:origin: org.apache.openejb/javaee-api

public static void marshal(Object object, File file) {
  if (file == null) {
    throw new IllegalStateException("No file is given");
  }
  marshal(object, new StreamResult(file));
}

代码示例来源:origin: javax.xml.bind/jaxb-api

/**
 * Writes a Java object tree to XML and store it to the specified location.
 *
 * @param jaxbObject
 *      The Java object to be marshalled into XML. If this object is
 *      a {@link JAXBElement}, it will provide the root tag name and
 *      the body. If this object has {@link XmlRootElement}
 *      on its class definition, that will be used as the root tag name
 *      and the given object will provide the body. Otherwise,
 *      the root tag name is {@link Introspector#decapitalize(String) infered} from
 *      {@link Class#getSimpleName() the short class name}.
 *      This parameter must not be null.
 *
 * @param xml
 *      The XML will be sent to the {@link Result} object.
 *
 * @throws DataBindingException
 *      If the operation fails, such as due to I/O error, unbindable classes.
 */
public static void marshal( Object jaxbObject, Result xml ) {
  _marshal(jaxbObject,xml);
}

代码示例来源:origin: org.apache.geronimo.specs/geronimo-jaxb_2.1_spec

public static <T> T unmarshal(String str, Class<T> type) {
  if (str == null) {
    throw new IllegalStateException("No string destination is given");
  }
  try {
    return unmarshal(new URI(str), type);
  } catch (URISyntaxException e) {
    return unmarshal(new File(str), type);
  }
}

代码示例来源:origin: org.apache.servicemix.specs/org.apache.servicemix.specs.jaxb-api-2.1

public static void marshal(Object object, String str) {
  if (str == null) {
    throw new IllegalStateException("No string destination is given");
  }
  try {
    marshal(object, new URI(str));
  } catch (URISyntaxException e) {
    marshal(object, new File(str));
  }
}

代码示例来源:origin: org.apache.openejb/javaee-api

public static <T> T unmarshal(URI uri, Class<T> type) {
  if (uri == null) {
    throw new IllegalStateException("No uri is given");
  }
  try {
    return unmarshal(uri.toURL(), type);
  } catch (MalformedURLException e) {
    throw new DataBindingException(e);
  }
}

代码示例来源:origin: org.apache.geronimo.specs/geronimo-jaxb_2.1_spec

public static void marshal(Object object, URI uri) {
  if (uri == null) {
    throw new IllegalStateException("No uri is given");
  }
  try {
    marshal(object, uri.toURL());
  } catch (IOException e) {
    throw new DataBindingException(e);
  }
}

代码示例来源:origin: stackoverflow.com

Exception in thread "main" java.lang.ExceptionInInitializerError
  at java.lang.Class.forName0(Native Method)
  at java.lang.Class.forName(Class.java:190)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:87)
Caused by: javax.xml.bind.DataBindingException: javax.xml.bind.UnmarshalException
 - with linked exception:
[java.io.FileNotFoundException: C:\dev\src\misc\[29, 23, 19, 17, 13, 11, 7, 5, 3, 2] (The system cannot find the file specified)]
  at javax.xml.bind.JAXB.unmarshal(JAXB.java:208)
...

相关文章