gpt4 book ai didi

java - 使用 java 解析 SOAP/XML 时出现问题

转载 作者:数据小太阳 更新时间:2023-10-29 02:29:49 27 4
gpt4 key购买 nike

我编写了一个 java 程序来解析 SOAP/XML。这是我的代码。

public static void main(String[] args) throws Exception {
Customer customer = new Customer();
customer.id = 123;
customer.firstName = "Jane";
customer.lastName = "Doe";
QName root = new QName("return");
JAXBElement<Customer> je = new JAXBElement<Customer>(root, Customer.class, customer);

XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);
xsw.writeStartDocument();
xsw.writeStartElement("soapenv", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
xsw.writeStartElement("soapenv", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
xsw.writeStartElement("", "InitializePayment", "http://service.jaxws.blog/");
xsw.writeStartElement("", "request", "http://service.jaxws.blog/");

JAXBContext jc = JAXBContext.newInstance(Customer.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.marshal(je, xsw);

xsw.writeEndDocument();
xsw.close();
}

我得到这样的输出。

<?xml version="1.0" ?>
<soapenv:Envelope>
<soapenv:Body>
<InitializePayment>
<request>
<return id="123">
<firstName>Jane</firstName>
<lastName>Doe</lastName>
</return>
</request>
</InitializePayment>
</soapenv:Body>
</soapenv:Envelope>

但我需要 id 的输出 i必须作为标 checkout 现在 <return> 中.像这样

<return>
<id>123</id>
<firstName>Jane</firstName>
<lastName>Doe</lastName>
</return>

最佳答案

您只需删除 id 字段/属性上的 @XmlAttribute 注释。


thanks it works. But url which in a envelope is not come in output

您需要利用 writeNamespacewriteDefaultNamespace 方法,如下所示:

    xsw.writeStartElement("soapenv", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
xsw.writeNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
xsw.writeStartElement("soapenv", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
xsw.writeStartElement("", "InitializePayment", "http://service.jaxws.blog/");
xsw.writeDefaultNamespace("http://service.jaxws.blog/");
xsw.writeStartElement("", "request", "http://service.jaxws.blog/");

关于java - 使用 java 解析 SOAP/XML 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28191561/

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