Results 1 to 4 of 4
  1. #1
    palanikumark is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default Extract xml to store into db

    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

  2. #2
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    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
    Last edited by pao; 06-06-2008 at 03:50 PM.

  3. #3
    palanikumark is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Can u give example...?

    Thanks

  4. #4
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    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:

    Java 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;
         }

Similar Threads

  1. How to extract information from a sentece
    By luisarca in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 12-03-2007, 12:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •