jaxb unmarshalling with namespace [英] jaxb unmarshalling with namespace

查看:118
本文介绍了jaxb unmarshalling with namespace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的xml,需要将其转换为java。我曾经使用过jaxb

This is my xml, need to convert it into java. I had used jaxb

<?xml version="1.0"?>
<lm:order Id="PLG24M240U" JD="" aCount="2" SUCount="1" xmlns:lm="http://www.ae.com/Event/Load">
  <lm:master>
   <lm:ID>3</lm:ID>
    <lm:Number>313</lm:Number>
    <lm:ANumber>323</lm:ANumber>     
  </lm:master>
  <lm:detail>
    <lm:ID>3</lm:ID>
    <lm:Number>3131</lm:Number>
    <lm:ANumber>3232</lm:ANumber>      
  </lm:detail>
 <lm:detail>
    <lm:ID>3</lm:ID>
    <lm:Number>3131</lm:Number>
    <lm:ANumber>3232</lm:ANumber>    
  </lm:detail>
  <lm:detail>
    <lm:ID>3</lm:ID>
    <lm:Number>313</lm:Number>
    <lm:ANumber>323</lm:ANumber>    
  </lm:detail>
</lm:order>

抛出以下异常
javax.xml.bind.UnmarshalException:意外元素(uri : http://www.ae.com/Event/Load ,当地:订购)。预期元素是< {} lm:Order>

And throwing the following exception javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.ae.com/Event/Load", local:"Order"). Expected elements are <{}lm:Order>

这是我的解组代码

jaxbContext = JAXBContext.newInstance(Order.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Order order = (Order) jaxbUnmarshaller.unmarshal(file);
System.out.println(order );

订购Pojo类

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "lm:Order")
public class OrderPay {
    @XmlAttribute
    private String Id;
    @XmlAttribute
    private String JD;
    @XmlAttribute
    private String aCount;
    @XmlAttribute
    private String pCount;
    /*@XmlElement
    private Master master;
    @XmlElement
    private List<Detail> details = new ArrayList<Detail>();*/

}

你也可以帮我阅读,目前阅读文件,需要读作XML字符串。

Can you please help me in reading also, currently reading through file, need to read as an XML String.

推荐答案

命名空间属性 xmlns:lm =http://www.ae.com/Event/Load可能是这里的罪魁祸首。为了指定名称空间前缀,您可以添加 @XmlSchema 注释到 package-info.java 这样的文件:

The namespace attribute xmlns:lm="http://www.ae.com/Event/Load" might be the culprit here. In order to specify the namespace prefix, you can add the @XmlSchema annotation to a package-info.java file like this:

@XmlSchema(
    namespace="http://www.ae.com/Event/Load",
    elementFormDefault=XmlNsForm.QUALIFIED),
    xmlns={@XmlNs(prefix="lm", namespaceURI="http://www.ae.com/Event/Load")})  

package your.package;
import javax.xml.bind.annotation.*;

这篇关于jaxb unmarshalling with namespace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆