我正在尝试使用特定的命名空间来构建一个
XML文档.我想要生成的最终文档应该是这样的:
@H_502_17@解决方法
<m:documentObject xmlns:m="http://www.myschema.com"> <sender>token</sender> <receiver>token</receiver> <payload>token</payload> </m:documentObject>
这是我到目前为止.
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element requestElement = document.createElementNS("http://www.myschema.com","documentObject"); document.appendChild(requestElement); Element sender = document.createElement("sender"); requestElement.appendChild(sender); Text senderText = document.createTextNode("Xmlsender"); sender.appendChild(senderText); Element receiver = document.createElement("receiver"); requestElement.appendChild(receiver); Text receiverText = document.createTextNode("Xmlreceiver"); receiver.appendChild(receiverText); Element payload = document.createElement("payload"); requestElement.appendChild(payload); Text payloadText = document.createTextNode("Xmlpayload"); payload.appendChild(payloadText); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(requestElement); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING,"utf-8"); transformer.transform(source,result); String xmlString = sw.toString(); System.out.println(xmlString)
由于某些原因,当我运行上面的架构出来没有前缀.如下所示:
<?xml version="1.0" encoding="utf-8"?> <documentObject xmlns="http://www.myschema.com"> <sender>Xmlsender</sender> <receiver>Xmlreceiver</receiver> <payload>Xmlpayload</payload> </documentObject>
我需要做什么,以便XML完全如第一个XML示例所示,名称空间前缀和标签具有命名空间前缀?
我正在尝试创建一个XML字符串,该字符串将用于Spring-WS webservice,该Web服务器希望使用第一个示例中显示的格式的JAXB对象.