比较xquery中的两个xml文件内容

时间:2014-07-21 12:50:18

标签: java xml xquery basex

我正在尝试创建一个新的xml文件而不提供任何路径,这意味着它不应存储在我的系统(磁盘)中(请参阅NewXML类)。现在我想检查这个xml到数据库(' db')是否存在,然后将这个xml文件的内容与现有的xml文件比较到数据库中(' db')和if contents如果相同则返回true,否则为false(参见类CheckXML)。

请注意,为什么我不想将新的xml文件存储到磁盘中,因为如果文件存储在磁盘中,那么首先我需要读取它,之后我们可以将它与数据库中的现有文件进行比较,这是不可行的解决方案当我们尝试比较数百万个xml文件时,它会引发性能问题。  所以,请有人可以解决这个问题。如果您有任何其他方式,请告诉我。

public class NewXML
{

    /**
     * @param args
     * @throws QueryException 
     * @throws BaseXException 
     */
    public static void main(String[] args) 
    {

        CheckXML cXml=new CheckXML();
        System.out.println("Enter value 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("ABC");
                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);

                //target name
                //Node node=dom.c


                // 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);


                String xmlfilename = "Staff_Member"+""+id+".xml";

                StreamResult result = new StreamResult(System.out);

                transformer.transform(source, result);

                System.out.println(xmlfilename);

            //""""""""""CHECK AND COMPARE XML FILE CONTENTS  INTO DATABASE""""""""""""  

              cXml.comparingXML(xmlfilename);




        }
        catch (ParserConfigurationException pce)
        {
            pce.printStackTrace();
        }
        catch (TransformerException tfe) 
        {
            tfe.printStackTrace();
        } catch (BaseXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (QueryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

public class CheckXML
{/*
    public static void main(String[] args) throws QueryException, IOException {
*/      DatabaseConnection processor = new DatabaseConnection();
//Database context.
        Context context = new Context();



      public void comparingXML(String xml) throws BaseXException, QueryException
      {

        System.out.println("\n* Open collection database");

        new Open("db").execute(context);

        String xmlFile=xml;

        System.out.println(xmlFile);

        // 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);



  // ------------------------------------------------------------------------
        // Close the database context
        context.close();


        }



}

0 个答案:

没有答案