-1

How can I parse and send data fields to db using java. I need code for store data to db. It would require any extra dependencies.

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <WS_FulfillmentResponse xmlns="https://imfawstest.codemantra.com/AIDC/">
         <WS_FulfillmentResult>
            <Date_Generated>12/2/2019 2:24:03 AM</Date_Generated>
            <product>
               <PUB_ID>25905</PUB_ID>
               <LOG_NO>46725</LOG_NO>
               <PIN_NO>94389</PIN_NO>
               <SERIES_CODE>001</SERIES_CODE>
               <SERIES_DESC>IMF Working Papers</SERIES_DESC>
               <TITLE><![CDATA[cM_Safe and Wholesome Food - Nordic Reflections]]></TITLE>
               <SUBTITLE />
               <PUBL_SERIES_VOLNO>Working Paper No. 12/596</PUBL_SERIES_VOLNO>
               <LANGUAGE_ID>4</LANGUAGE_ID>
               <EDITION />
               <PRC_STATUS_ID>11</PRC_STATUS_ID>
               <PRC_STATUS_CODE>40</PRC_STATUS_CODE>
               <Process_Status_Desc>Key' metadata required for promotion is entered by now (Project ID &amp; description, Stock No.)</Process_Status_Desc>
               <PUBLISHED_DATE />
               <REVISION_DATE />
               <EST_COMPL_DATE />
               <ISBN>9781498302258</ISBN>
               <ISSN_ID>152</ISSN_ID>
               <COPYRIGHT_YR>2019</COPYRIGHT_YR>
               <DOI>10.5089/9781498302258.001</DOI>
               <Persistent_Link>https://elibrary.imf.org/view/IMF001/25905-9781498302258/25905-9781498302258/25905-9781498302258.xml</Persistent_Link>
               <SKU>802258</SKU>
               <DRAFT>0</DRAFT>
               <FRONT_MATTERS>0</FRONT_MATTERS>
               <MAIN>0</MAIN>
               <Size>Small</Size>
               <TOTAL>0</TOTAL>
               <ROMAN_ARABIC />
               <Dept_Phone>37779</Dept_Phone>
               <Dept_Email>JLI2</Dept_Email>
               <Dept_Contact_ID>22382</Dept_Contact_ID>
               <Dept_Contact_Name />
               <EXR_Editor_Name>Jim Beardow</EXR_Editor_Name>
               <EXR_Editor_ID>45</EXR_Editor_ID>
               <EXR_Editor_Phone>37899</EXR_Editor_Phone>
               <EXR_Editor_Email>jbeardow@imf.org</EXR_Editor_Email>
               <OutsidePublisher_Id>0</OutsidePublisher_Id>
               <OutsidePublisher_Name />
               <OutsidePublisher_Email />
               <OutsidePublisher_Phone />
               <OutsidePublisher_Address />
               <PUBL_MANUS_EXPT_DATE>10/31/2019</PUBL_MANUS_EXPT_DATE>
               <PUBL_DATE_TO_GRAPHICS />
               <PUBL_DATE_TRANSLATION_RECD />
               <PUBL_DATE_SENT_TRANSLATION>10/31/2019</PUBL_DATE_SENT_TRANSLATION>
               <PUBL_MANUS_RCVD_DATE>10/31/2019</PUBL_MANUS_RCVD_DATE>
               <PUBL_ASG_EDITOR_DATE>10/31/2019</PUBL_ASG_EDITOR_DATE>
               <FIRST_RECIEPT_DATE>10/31/2019</FIRST_RECIEPT_DATE>
               <PUBL_OUT_OF_PRINT_DATE />
               <PRIORITY>Medium</PRIORITY>
               <PUBL_DEPT_SUMMARY />
               <PUBL_NOTE_CORESP />
               <PUBL_INTERNAL_REMARKS />

above is sample format how to parse and take some fields name and isbn,price some more send to db how.

halfer
  • 19,824
  • 17
  • 99
  • 186
kesav
  • 17
  • 2
  • using org.json you can convert XML into JSON https://stackoverflow.com/questions/1823264/quickest-way-to-convert-xml-to-json-in-java – Ganesh Gudghe Dec 02 '19 at 09:42
  • It's not ,i wanted to store the soap api response fields to my mongo database E.X:i call and got response from that response i wanted to store some fields into my db can you please help on that i am new of java using spring boot – kesav Dec 02 '19 at 10:08
  • you can filter your data after converting to JSON and store filtered JSON in mongo – Ganesh Gudghe Dec 02 '19 at 10:16

1 Answers1

0

You can extract each data using inbuilt Java API for XML Processing called DOM XML Parser. It turns the XML file into DOM or Tree structure, and you have to traverse a node by node to get what you want. Here is an example that shows how to do it,

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

try {
     // Creating a constructor of file class and parsing an XML file
     File file = new File("XMLFile.xml");
     // An instance of factory that gives a document builder
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     // An instance of builder to parse the specified XML file
     DocumentBuilder db = dbf.newDocumentBuilder();
     Document doc = db.parse(file);
     doc.getDocumentElement().normalize();
     System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
     NodeList nodeList = doc.getElementsByTagName("product");

     // NodeList is not iterable, so we are using for loop
     for (int i = 0; i < nodeList.getLength(); i++) {
         Node node = nodeList.item(i);
         System.out.println("\nNode Name :" + node.getNodeName());
         if (node.getNodeType() == Node.ELEMENT_NODE) {
             Element eElement = (Element) node;
             System.out.println("PUB ID: " + eElement.getElementsByTagName("PUB_ID").item(0).getTextContent());
             System.out.println("LOG NO: " + eElement.getElementsByTagName("LOG_NO").item(0).getTextContent());
             System.out.println("PIN NO: " + eElement.getElementsByTagName("PIN_NO").item(0).getTextContent());
             System.out.println("SERIES CODE: " + eElement.getElementsByTagName("SERIES_CODE").item(0).getTextContent());
             System.out.println("SERIES DESC: " + eElement.getElementsByTagName("SERIES_DESC").item(0).getTextContent());
         }
     }
 } catch (SAXException | IOException | ParserConfigurationException ex) {
     Logger.getLogger(YourClass.class.getName()).log(Level.SEVERE, null, ex);
 }

And then you can code an insert query to send data to your database.

Hope this helps you!

Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36