JAXB和STAX,当树中的元素具有属性时为空

时间:2018-10-17 12:07:57

标签: java xml jaxb stax

我无法解组xml。当我解组没有任何属性的XML时,它就可以正常工作,但是当我尝试解组任何元素中具有属性的XML时,我只会得到空值。

这是我的代码:

<Document xmlns="sd">
<CstmrCdtTrfInitn>
    <GrpHdr>
        <NbOfTxs>2</NbOfTxs>
        <CtrlSum>100</CtrlSum>
        <blabla>bla</blabla>
    </GrpHdr>
    <PmtInf>
        <NbOfTxs>2</NbOfTxs>
        <CtrlSum>100</CtrlSum>
        <blabla>bla</blabla>
    </PmtInf>
</CstmrCdtTrfInitn>
</Document>

xml模式:

package XMLElement;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "GrpHdr")
@XmlAccessorType(XmlAccessType.FIELD)
public class GrpHdr {   

@XmlElement(name = "MsgId")
private String msgId;
@XmlElement(name = "CreDtTm")
private String creDtTm;
@XmlElement(name = "NbOfTxs")
private String nbOfTxs;
@XmlElement(name = "CtrlSum")
private String ctrlSum;
@XmlElement(name = "InitgPty")
private String initgPty;

// MsgId
public String getMsgId() {
    return msgId;
}

public void setMsgId(String string_msgId) {
    msgId = string_msgId;
}


// CreDtTm
public String getCreDtTm() {
    return creDtTm;
}

public void setCreDtTm(String string_creDtTm) {
    creDtTm = string_creDtTm;
}


// NbOfTxs
public String getNbOfTxs() {
    return nbOfTxs;
}

public void setNbOfTxs(String string_nbOfTxs) {
    nbOfTxs = string_nbOfTxs;
}


// CtrlSum
public String getCtrlSum() {
    return ctrlSum;
}

public void setCtrlSum(String string_ctrlSum) {
    ctrlSum = string_ctrlSum;
}


// InitgPty
public String getInitgPty() {
    return initgPty;
}

public void setInitgPty(String string_initgPty) {
    initgPty = string_initgPty;
}




}

和Main.java

package application;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;

import XMLElement.CstmrCdtTrfInitn;
import XMLElement.Document;
import XMLElement.GrpHdr;
import XMLElement.PmtInf;

public class Main {

public static void main(String[] args) throws Exception {               

        XMLInputFactory xif2 = XMLInputFactory.newFactory();
        StreamSource xml2 = new StreamSource("D:\\test2.xml");
        XMLStreamReader xsr2 = xif2.createXMLStreamReader(xml2);
        xsr2.nextTag();
        while(xsr2.hasNext()) {
            if(xsr2.isStartElement() && 
xsr2.getLocalName().equals("GrpHdr")) {
                break;
            }
            xsr2.next();
        }

        JAXBContext jc2 = JAXBContext.newInstance(GrpHdr.class);
        Unmarshaller unmarshaller2 = jc2.createUnmarshaller();
        JAXBElement<GrpHdr> jb2 = unmarshaller2.unmarshal(xsr2, 
 GrpHdr.class);
        xsr2.close();


        GrpHdr grpHdr = jb2.getValue();
        System.out.println(grpHdr.getNbOfTxs());
        System.out.println(grpHdr.getCtrlSum());

        }
}

当我删除xmlns="sd"时,我得到的结果没有任何问题,但是否则,我只会得到null。关于我做错了什么建议吗?

1 个答案:

答案 0 :(得分:1)

请注意, BodyContentHandler handler = new BodyContentHandler(); Metadata metadata = new Metadata(); ParseContext context = new ParseContext(); PDFParser pdfParser = new PDFParser(); PDFParserConfig config = pdfParser.getPDFParserConfig(); config.setSortByPosition(true); // needed for text in correct order pdfParser.setPDFParserConfig(config); pdfParser.parse(is, handler, metadata, context); 代表XML命名空间。因此,如果您在XML文档中有名称空间,它将将XML文档的所有标签与该名称空间绑定,因此,很明显XML unmarshalling 无法正常工作,因为您的标签会说{{ 1}}和JAXB将仅检查xmlns

您还必须在XML映射类中声明名称空间,以使其与名称空间一起使用(或在不需要时删除名称空间)。您可以在 document 类中添加XmlSchema批注以使其与sd:XYZ属性一起使用。

XYZ