You are on page 1of 4

import import import import import import import import import import import

javax.persistence.EntityManager; javax.persistence.EntityManagerFactory; javax.persistence.EntityTransaction; javax.persistence.Persistence; javax.persistence.Query; javax.xml.parsers.*; org.xml.sax.InputSource; org.w3c.dom.*; java.io.*; java.util.ArrayList; java.util.List;

public class ParseXMLString { public static void main(String arg[]) { String xmlRecords = "<data>" + " <employee>" + " <name>John</name>" + " <title>Manager</title>" + " </employee>" + " <employee>" + " <name>Sara</name>" + " <title>Clerk</title>" + " </employee>" + "</data>";

try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse (new File("fxs_message.xml")); //required if a String is being passed /* InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xmlRecords)); Document doc = db.parse(is);*/ NodeList nodes = doc.getElementsByTagName("FX_RATE"); // iterate the employees List<Fxs> fxsLst = new ArrayList<Fxs>(); Fxs fxsEntity = null; for (int i = 0; i < nodes.getLength(); i++) { fxsEntity = new Fxs(); Element element = (Element) nodes.item(i); NodeList fundingCurrencyCode = element.getElementsByTagName("FXS_FXC_ CODE"); Element line = (Element) fundingCurrencyCode.item(0); System.out.println("FXS_FXC_CODE: " + getCharacterDataFromElement(lin e));

fxsEntity.setFundingCurrencyCode(getCharacterDataFromElement(line)); NodeList fxsRate = element.getElementsByTagName("FXS_SELL_RATE"); line = (Element) fxsRate.item(0); System.out.println("FX_RATE: " + getCharacterDataFromElement(line)); fxsEntity.setFxRate(Double.parseDouble(getCharacterDataFromElement(li ne))); fxsLst.add(fxsEntity); } renderFxs(fxsLst); System.out.println(fxsLst.toString()); //persist to db } catch (Exception e) { e.printStackTrace(); } /* output : Name: John Title: Manager Name: Sara Title: Clerk */ } public static String getCharacterDataFromElement(Element e) { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData().replace("\\n",""); } return "?"; } private static void renderFxs(List<Fxs> fxsList) { // Creates an instance of book /*Book book = new Book(); book.setTitle("The Hitchhiker's Guide to the Galaxy"); book.setPrice(12.5F); book.setDescription("Science fiction comedy book"); book.setIsbn("1-84023-742-2"); book.setNbOfPage(354); book.setIllustrations(false);*/ // Gets an entity manager and a transaction ( chapter02PU is the scheema) EntityManagerFactory emf = null; EntityManager em = null; EntityTransaction tx = null; try

{ emf = 2PU"); em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); Query query = em.createNamedQuery("findAll"); @SuppressWarnings("unchecked") List<Fxs> fxsEntityList = query.getResultList(); String markForDeletion = null; for (Fxs fxsEntity:fxsEntityList) { markForDeletion = "Y"; for (Fxs fxs:fxsList) { if (fxsEntity.getFundingCurrencyCode() .equalsIgnoreCase(fxs.getFundingCurrencyCode()) && (fxsEntity.getFxRate() != fxs .getFxRate())) { fxsEntity.setFxRate(fxs.getFxR ate()); markForDeletion ="N"; break; } } fxsEntity.setMarkedForDeletion(markForDeletion ); } //add the new fxs entities for(Fxs fxs:fxsList) { boolean fundingCurrencyExists = false; for (Fxs fxsEntity:fxsEntityList) { if (fxsEntity.getFundingCurrencyCode(). equalsIgnoreCase(fxs.getFundingCurrencyCode())) { fundingCurrencyExists = true; break; } } if(!fundingCurrencyExists) em.persist(fxs); } tx.commit(); Persistence.createEntityManagerFactory("chapter0

/*if (fxsList != null && fxsList.size()>0)

{ emf = chapter02PU"); em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); for(Fxs fxs:fxsList) { em.persist(fxs); //CRUD operations(persi st, update, remove, load) } tx.commit(); }*/ } catch(Exception ex) { tx.rollback(); } finally { em.close(); emf.close(); } } } Persistence.createEntityManagerFactory("

You might also like