我有一个WSDL文件,我正在使用生成我的Java代码。 WSDL文件具有以下SOAP地址代码
<wsdl:service name="SomeService">
<wsdl:port binding="tns:HttpBinding_SomeService" name="HttpBinding_SomeService">
<soap:address location="https://www.someservice.com/testenv/SomeService?client_id=XXXX-XXXXX-XXXX&client_secret=XXXXXXXXXX"/>
</wsdl:port>
</wsdl:service>
我想要动态更改<soap:address>
位置。有没有办法可以用Java做到这一点?
答案 0 :(得分:0)
使用DocumentBuilder
有一种方法可以做到这一点,我刚刚给出了语法行,只需用import和try-catch修改它们
String filepath = "fakepath\\wsdl.xml"; //Give your wsdl file path
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
Node address= doc.getElementsByTagName("soap:address").item(0);
NamedNodeMap attr = address.getAttributes();
Node nodeAttr = attr.getNamedItem("location");
nodeAttr.setTextContent("Your new location here");
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
您可以将其写入函数并通过将位置作为参数传递来调用它。并动态更改wsdl文件。
答案 1 :(得分:0)
这是导入
的完整示例import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class WsdlDynamicPath {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
changePath();
} catch (ParserConfigurationException | SAXException | IOException |
TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void changePath() throws ParserConfigurationException, SAXException, IOException, TransformerException{
String filepath = "path to wsdl\\demo.wsdl"; //Give your wsdl file path
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
Node address= doc.getElementsByTagName("soap:address").item(0);
NamedNodeMap attr = address.getAttributes();
Node nodeAttr = attr.getNamedItem("location");
nodeAttr.setTextContent("new path to wsdl");
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
}
}