Hi all,
I need to get the data from the xml file and store it onto the database. Is there anyway of doing this?
thanks
Printable View
Hi all,
I need to get the data from the xml file and store it onto the database. Is there anyway of doing this?
thanks
You can do this using SAX or DOM and parse the xml file. You can write Xpath queries to get parts that you require using DOM? I cant quite remember but I have used both methods in the past.
Let me know if you need anymore help.
Cheers
Can u give example...?
Thanks
Ok here is a small example, you will need to pick through the method and get the bits you want out. I have also removed some parts as it is sensitive to my employment role... But it shouldnt be anything of any importance but this method may not compile now, it will probably just be a bracket missing or something. Good luck! ps this is using DOM:
Code:
//Class imports YOU may not need all of these
import javax.xml.xpath.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import javax.xml.parsers.*;
import javax.xml.namespace.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.messaging.URLEndpoint;
public static org.w3c.dom.Node getPayload(String xPath, String xml, String nodeName) throws Exception {
//Create a new XPath object and add a namespace context reference to the
//XPath object that allows the XPath to map the prefix to a namespace
XPath xpath = XMLUtilities.getXPath(new MyContext());
//System.out.println(xml);
//Perform the transform
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document document = db.parse(new InputSource(new StringReader(xml)));
//Obtain the part of the SOAP message that we are interested in as
//a org.w3c.dom.Node object.
org.w3c.dom.Node titleNode = (org.w3c.dom.Node) xpath.evaluate(xPath, document, XPathConstants.NODE);
NodeList nodeList = titleNode.getChildNodes();
org.w3c.dom.Node tempNode = null;
org.w3c.dom.Node payLoad = null;
org.w3c.dom.Node controlActEventNode = null;
org.w3c.dom.Node reasonNode = null;
for(int i = 0; i < nodeList.getLength(); i++) {
tempNode = nodeList.item(i);
if(tempNode.getNodeName().equals(nodeName)) {
payLoad = nodeList.item(i);
} else if (tempNode.getNodeName().equals("Node I want")) {
//New changes to the message mean we can no longer rely on
org.w3c.dom.NodeList reasonNodes = (NodeList) xpath.evaluate(XPathQueryConstants.PROCESS_ERROR_RESPONSE, tempNode, XPathConstants.NODESET);
for(int j = 1; j <= reasonNodes.getLength(); j++) {
reasonNode = reasonNodes.item(j - 1);
reasonCode = (String)xpath.evaluate("//prefix:reason[" + j + "]/prefix:justifyingDetectedIssueEvent/prefix:code/@code", reasonNode, XPathConstants.STRING);
if(reasonCode.length() == 5) {
reasonString = (String)xpath.evaluate("//prefix:reason[" + j + "]/prefix:justifyingDetectedIssueEvent/prefix:code/@displayName", reasonNode, XPathConstants.STRING);
}
}
return payLoad;
}