gpt4 book ai didi

java - 使用命名空间的 jaxb 解码

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:01:48 28 4
gpt4 key购买 nike

这是我的xml,需要将它转换成java。我用过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”,本地:“Order”)。预期的元素是 <{}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 字符串的形式阅读。

最佳答案

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

@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.*;

关于java - 使用命名空间的 jaxb 解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25819934/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com