0

嗨,我正在尝试使用 jaxb 解析 xml,但它的主体始终返回 null。我试图创建该类的对象并将其转换为同样好的结果但是当尝试解析它时它不起作用。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <St_PurchaseResponse xmlns="http://tempuri.org/">
            <St_PurchaseResult>
                <string>RESULT STRING</string>
            </St_PurchaseResult>
        </St_PurchaseResponse>
    </soap:Body>
</soap:Envelope>

使用以下代码:

 val responseObj: PurchaseResponseEnvelope? =
            JAXBContext.newInstance(PurchaseResponseEnvelope::class.java).createUnmarshaller()
                .unmarshal(StreamSource(StringReader(response)), PurchaseResponseEnvelope::class.java).value


@XmlRootElement(name = "soap:Envelope")
@XmlAccessorType(XmlAccessType.FIELD)
open class ApiEnvelope {
    @XmlAttribute(name = "xmlns:soap")
    val soapAttr = "http://schemas.xmlsoap.org/soap/envelope/"

    @XmlAttribute(name = "xmlns:xsi")
    val xsiAttr = "http://www.w3.org/2001/XMLSchema-instance"

    @XmlAttribute(name = "xmlns:xsd")
    val xsdAttr = "http://www.w3.org/2001/XMLSchema"

    constructor()
}


@XmlRootElement(name = "soap:Envelope")
@XmlAccessorType(XmlAccessType.FIELD)
class PurchaseResponseEnvelope : ApiEnvelope {
    @XmlElement(name = "soap:Body")
    var body: PurchaseResponseBody? = null

    constructor(body: PurchaseResponseBody) {
        this.body = body
    }

    constructor() : super()
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class PurchaseResponseBody {
    @XmlElement(name = "St_PurchaseResponse")
    var body: PurchaseResponseBodyWrap? = null
}


@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class PurchaseResponseBodyWrap {
    @XmlAttribute(name = "xmlns")
    val namespace = "http://tempuri.org/"

    @XmlElement(name = "St_PurchaseResult")
    var result: PurchaseResponseResult? = null
}


@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class PurchaseResponseResult {
    @XmlElement(name = "string")
    var result: String? = null
}

但这不是工作体总是空的我试图编组对象很好,相同的xml结果但是当解组时总是空体

4

0 回答 0