Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB从XML中解组空值_Java_Jaxb - Fatal编程技术网

Java JAXB从XML中解组空值

Java JAXB从XML中解组空值,java,jaxb,Java,Jaxb,我在尝试将值从XML提取到Java时遇到了一些问题。我的XML文件输入示例: <?xml version="1.0" encoding="UTF-8" ?> <TRB_TRX xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <HEADER ReqNotf="N" TransactionCode="L9_BLACKLIST_SET" PublisherApplID="CL" PublisherApplTh

我在尝试将值从XML提取到Java时遇到了一些问题。我的XML文件输入示例:

<?xml version="1.0" encoding="UTF-8" ?>
 <TRB_TRX xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<HEADER ReqNotf="N" TransactionCode="L9_BLACKLIST_SET" PublisherApplID="CL" PublisherApplThreadID="1" EntityId="58241962" RoutingId="2626289" EntityType="ACCOUNT" IssueDate="2019-06-18T23:59:59" EffectiveDate="2019-06-18T23:59:59"/>
    <DATA>
        <BlacklistCodeSet>
            <TransactionHeaderInfoExt>
                <ApplicationId>CL</ApplicationId>
                <RequestDate>2019-06-18T23:59:59</RequestDate>
            </TransactionHeaderInfoExt>
            <ClEntityIdInfoExt>
                <EntityId>58241962</EntityId>
                <EntityType>ACCOUNT</EntityType>
            </ClEntityIdInfoExt>
            <GeneralCollectionEntityInfoExt>
                <BlacklistCode>D</BlacklistCode>
            </GeneralCollectionEntityInfoExt>
        </BlacklistCodeSet>
    </DATA>
</TRB_TRX>
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="BlacklistCodeSet")
public class BlackListCode {
    String entityId;
String blackListCode;

public String getEntityId() {
    return entityId;
}

public void setEntityId(String entityId) {
    this.entityId = entityId;
}

public String getBlackListCode() {
    return blackListCode;
}

public void setBlackListCode(String blackListCode) {
    this.blackListCode = blackListCode;
}

@Override
public String toString() {
    return "BlackListCode{" +
            "entityId='" + entityId + '\'' +
            ", blackListCode='" + blackListCode + '\'' +
            '}';
}
}
我的代码用于提取值:

XMLInputFactory xif = XMLInputFactory.newFactory();

                FileReader reader = new FileReader("src/main/resources/xml/blacklistcode.xml");
                XMLStreamReader xsr = xif.createXMLStreamReader(reader);

                try {
                    JAXBContext jc = JAXBContext.newInstance(BlackListCode.class);
                    Unmarshaller unmarshaller = jc.createUnmarshaller();

                    while (xsr.hasNext()) {
                        while (xsr.hasNext() && (!xsr.isStartElement() || !xsr.getLocalName().equals("BlacklistCodeSet"))) {
                            xsr.next();
                        }
                        if (xsr.hasNext()) {
                            BlackListCode blacklistcode = (BlackListCode) unmarshaller.unmarshal(xsr);
                            log.debug("BLACKLIST CODE IS " + blacklistcode);
                            log.debug("ACCOUNT NO IS " + blacklistcode.getEntityId());
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
在提取值之前,我尝试循环该节点,直到它与BlacklistCodeSet匹配。但是,打印出的值为null,并且没有抛出错误消息


有什么想法吗?谢谢

为了让JAXB在此时解析到该类中,的XML必须是:

D 58241962 如果您创建一个BlackListCode实例并使用JAXB将其转换为XML,您自己就可以看到这一点。这是调试JAXB的最佳方法

BlackListCode blackListCode = new BlackListCode();
blackListCode.setEntityId("58241962");
blackListCode.setBlackListCode("D");

JAXBContext jaxbContext = JAXBContext.newInstance(BlackListCode.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(blackListCode, System.out);
如您所见,JAXB所需的XML与您提供给JAXB的实际XML有很大的不同。元素的名称甚至都不正确,以小写字母开头,因此需要指定@XmlElement注释来正确命名它们,并且需要额外的对象来获得更复杂的XML

@XmlRootElementname=BlacklistCodeSet 类黑名单代码{ 私人宽频带宽频带宽频带; 专用GeneralCollectionEntityInfoExt GeneralCollectionEntityInfoExt; @XmlElementname=ClentityIdinFoText public clentityidifext getclentityidifext{ 返回此.clentidinofext; } 公共无效设置ClentityIdinFoExtClentityIdinFoExt clEntityIdInfoExt{ this.clentityidifext=clentityidifext; } @XmlElementname=GeneralCollectionEntityInfoExt 公共GeneralCollectionEntityInfoExt getGeneralCollectionEntityInfoExt{ 返回this.generalCollectionEntityInfoExt; } public void setGeneralCollectionEntityInfoExtGeneralCollectionEntityInfoExt generalCollectionEntityInfoExt{ this.generalCollectionEntityInfoExt=generalCollectionEntityInfoExt; } @凌驾 公共字符串toString{ 返回BlackListCode{entityId='+this.clentityidifext.getEntityId+'\+ ,BlacklistCode='+this.generalCollectionEntityInfoExt.getBlacklistCode+'\+'}'; } } 类ClentityIDifOext{ 私有字符串entityId; 公共宽大{ } public clentityidifextstring entityId{ this.entityId=entityId; } @XmlElementname=EntityId 公共字符串getEntityId{ 返回此.entityId; } public void setEntityId字符串entityId{ this.entityId=entityId; } } 类GeneralCollectionEntityInfoExt{ 私有字符串黑名单码; 公共GeneralCollectionEntityInfoExt{ } 公共GeneralCollectionEntityInfoExtString黑名单代码{ this.BlacklistCode=BlacklistCode; } @XmlElementname=BlacklistCode 公共字符串getBlacklistCode{ 返回此.BlacklistCode; } 公共无效设置LackListCodeString黑名单代码{ this.BlacklistCode=BlacklistCode; } } 试验

输出

58241962 D
既然生成的XML与预期的XML匹配,JAXB就可以正确地将XML解组到对象中。

我怀疑这是因为EntityId和BlacklistCode都不是BlacklistCodeSet标记的直接子项。尝试读取直接子项并提取其子项标记。
BlackListCode blackListCode = new BlackListCode();
blackListCode.setClEntityIdInfoExt(new ClEntityIdInfoExt("58241962"));
blackListCode.setGeneralCollectionEntityInfoExt(new GeneralCollectionEntityInfoExt("D"));

JAXBContext jaxbContext = JAXBContext.newInstance(BlackListCode.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(blackListCode, System.out);