add xml document parsing; clean up typos

This commit is contained in:
Greg Gauthier 2021-04-09 22:03:56 +01:00
parent 87253ab4ff
commit 92a4247cd3
2 changed files with 34 additions and 3 deletions

View File

@ -5,9 +5,18 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.Objects; import java.util.Objects;
@ -18,7 +27,18 @@ import java.util.Objects;
public class MockController { public class MockController {
@PostMapping(path ="/call1") @PostMapping(path ="/call1")
public String postCall1(@RequestBody String xmlRequestBody){ public String postCall1(@RequestBody String xmlRequestBody) throws Exception {
Document xmlRequest = parseRequestBody(xmlRequestBody);
Element root = xmlRequest.getDocumentElement();
NodeList nodeList = root.getChildNodes();
System.out.println(root.getNodeName());
for (int i = 0; i < nodeList.getLength(); i++){
System.out.println(nodeList.item(i).getNodeName());
System.out.println(nodeList.item(i).getNodeValue());
}
return getStaticResponse("soap-example.xml"); return getStaticResponse("soap-example.xml");
} }
@ -29,7 +49,9 @@ public class MockController {
ClassLoader classLoader = getClass().getClassLoader(); ClassLoader classLoader = getClass().getClassLoader();
try { try {
file = new File(Objects.requireNonNull(classLoader.getResource(filePath)).getFile()); file = new File(
Objects.requireNonNull(classLoader
.getResource(filePath)).getFile());
} catch (NullPointerException e) { } catch (NullPointerException e) {
e.printStackTrace();} e.printStackTrace();}
try { try {
@ -39,4 +61,13 @@ public class MockController {
e.printStackTrace();} e.printStackTrace();}
return content; return content;
} }
private Document parseRequestBody(String xmlRequestBody) throws Exception {
DocumentBuilderFactory dBfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dBfactory.newDocumentBuilder();
Document document =
builder.parse(new InputSource(new StringReader(xmlRequestBody)));
document.getDocumentElement().normalize();
return document;
}
} }

View File

@ -1,4 +1,4 @@
<?xml version = "1.0 encoding="utf-8""?> <?xml version = "1.0" encoding="utf-8" ?>
<SOAP-ENV:Envelope <SOAP-ENV:Envelope
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope" xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding"> SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding">