Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////
- package org.xmlfiles;
- import java.io.File;
- import java.io.IOException;
- import java.io.StringWriter;
- import java.io.Writer;
- import java.util.Scanner;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.transform.OutputKeys;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerException;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import org.basex.data.Result;
- import org.basex.query.QueryException;
- import org.w3c.dom.Attr;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- public class NewXML
- {
- static String xmlfilename;
- static CheckXML comxml=new CheckXML();//object need to call compareXml()
- public static void main(String ar[]) throws IOException, QueryException
- {
- System.out.println("Enter id to create the records:");
- Scanner kb=new Scanner(System.in);
- int i = kb.nextInt();
- try
- {
- DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
- // root elements
- Document doc = docBuilder.newDocument();
- Element rootElement = (Element) doc.createElement("Transperfect");
- doc.appendChild(rootElement);
- // staff elements
- Element staff = doc.createElement("DEV");
- rootElement.appendChild(staff);
- // set attribute to staff element
- String id=String.valueOf(i);
- Attr attr = doc.createAttribute("id");
- attr.setValue(id);
- staff.setAttributeNode(attr);
- // shorten way
- // staff.setAttribute("id", "1");
- // firstname elements
- String fn="yong"+i;
- Element firstname = doc.createElement("firstname");
- firstname.appendChild(doc.createTextNode(fn));
- staff.appendChild(firstname);
- // lastname elements
- String ln="mook kim"+i;
- Element lastname = doc.createElement("lastname");
- lastname.appendChild(doc.createTextNode(ln));
- staff.appendChild(lastname);
- // nickname elements
- String nicknm="mkyong"+i;
- Element nickname = doc.createElement("nickname");
- nickname.appendChild(doc.createTextNode(nicknm));
- staff.appendChild(nickname);
- // salary elements
- Element salary = doc.createElement("salary");
- salary.appendChild(doc.createTextNode("100000"));
- staff.appendChild(salary);
- // write the content into xml file
- TransformerFactory transformerFactory = TransformerFactory.newInstance();
- Transformer transformer = transformerFactory.newTransformer();
- transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- transformer.setOutputProperty(OutputKeys.METHOD, "xml");
- transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
- transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "staff");
- transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
- DOMSource source = new DOMSource(doc);
- java.io.StringWriter sw = new java.io.StringWriter();
- StreamResult sr = new StreamResult(sw);
- transformer.transform(source, sr);
- String xml = sw.toString();
- xmlfilename="Staff_Member"+""+id+".xml";
- //StreamResult result = new StreamResult(new File(xmlfilename));
- StreamResult result = new StreamResult(System.out);
- transformer.transform(source, result);
- System.out.println(xmlfilename+":is createdn");
- //==============================Compare Xml file with the Database Xml Files=========================//
- checkAndCompare(xml);
- }
- catch (ParserConfigurationException pce)
- {
- pce.printStackTrace();
- }
- catch (TransformerException tfe)
- {
- tfe.printStackTrace();
- }
- }
- //checkAndCompare method
- public static void checkAndCompare(String xml) throws QueryException, IOException
- {
- Result result=comxml.compareXml(xml);
- //System.out.println(result);
- if((result.toString()).equals("true"))
- {
- System.out.println(result);
- System.out.println("Both Xml FIles have same contents");
- }
- else
- {
- System.out.println(result);
- System.out.println("Both Xml Files have some different contents");
- }
- }
- }
- ////////////////
- package org.xmlfiles;
- import java.io.IOException;
- import javax.xml.parsers.DocumentBuilder;
- import org.basex.core.BaseXException;
- import org.basex.core.Context;
- import org.basex.core.cmd.Open;
- import org.basex.core.cmd.XQuery;
- import org.basex.data.Result;
- import org.basex.query.QueryException;
- import org.basex.query.QueryProcessor;
- import org.basex.query.value.item.Str;
- import org.w3c.dom.NodeList;
- public class CheckXML
- {
- Context context = new Context();
- public Result compareXml(String xml) throws QueryException, IOException
- {
- new Open("db").execute(context);
- //System.out.println("n* Open collection database");
- String xmlFile=xml;
- // System.out.println("=================Comparing XML files====================");
- String query = "declare variable $xml-file as xs:string external;" +
- "for $doc in collection('db')where matches(document-uri($doc), '" + xmlFile + "') return deep-equal($doc,$xml-file)";
- QueryProcessor proc = new QueryProcessor(query, context);
- proc.bind("xml-file", xmlFile);
- Result result = proc.execute(); // your result
- //System.out.println(result);
- context.close();
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement