Ejemplo n.º 1
0
        public static void Main(String[] args)
        {
            VTDGen    vg  = new VTDGen();
            AutoPilot ap0 = new AutoPilot();
            AutoPilot ap1 = new AutoPilot();
            AutoPilot ap2 = new AutoPilot();

            ap0.selectXPath("/root/a");
            ap1.selectXPath("/root/b");
            ap2.selectXPath("/root/c");
            Encoding eg = System.Text.Encoding.GetEncoding("utf-8");

            if (vg.parseFile("old.xml", false))
            {
                VTDNav vn = vg.getNav();
                ap0.bind(vn);
                ap1.bind(vn);
                ap2.bind(vn);
                FileStream fos = new FileStream("new.xml", System.IO.FileMode.OpenOrCreate);
                //fos.Write("<root>".getBytes());
                byte[] ba0, ba1, ba2, ba3, ba4;
                //ba0 = eg.GetBytes("
                ba1 = eg.GetBytes("<root>");
                ba2 = eg.GetBytes("</root>");
                ba3 = eg.GetBytes("\n");
                fos.Write(ba1, 0, ba1.Length);
                byte[] ba = vn.getXML().getBytes();
                while (ap0.evalXPath() != -1)
                {
                    long l      = vn.getElementFragment();
                    int  offset = (int)l;
                    int  len    = (int)(l >> 32);
                    fos.Write(ba3, 0, ba3.Length);
                    fos.Write(ba, offset, len);
                }
                ap0.resetXPath();
                while (ap1.evalXPath() != -1)
                {
                    long l      = vn.getElementFragment();
                    int  offset = (int)l;
                    int  len    = (int)(l >> 32);
                    fos.Write(ba3, 0, ba3.Length);
                    fos.Write(ba, offset, len);
                }
                ap1.resetXPath();
                while (ap2.evalXPath() != -1)
                {
                    long l      = vn.getElementFragment();
                    int  offset = (int)l;
                    int  len    = (int)(l >> 32);
                    fos.Write(ba3, 0, ba3.Length);
                    fos.Write(ba, offset, len);
                }
                ap2.resetXPath();
                fos.Write(ba3, 0, ba3.Length);
                fos.Write(ba2, 0, ba2.Length);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            try
            {
                // open file to output extracted fragments
                System.IO.FileInfo   f1  = new System.IO.FileInfo("./out.txt");
                System.IO.FileStream fos = new System.IO.FileStream(f1.FullName, System.IO.FileMode.Create);

                // instantiate the parser
                VTDGen vg = new VTDGen();
                vg.selectLcDepth(5);
                if (vg.parseFile("./soap2.xml", true))
                {
                    VTDNav vn = vg.getNav();
                    // get to the SOAP header
                    if (vn.toElementNS(VTDNav.FC, "http://www.w3.org/2003/05/soap-envelope", "Header"))
                    {
                        if (vn.toElement(VTDNav.FC))
                        // to first child
                        {
                            do
                            {
                                // test MUSTHAVE
                                if (vn.hasAttrNS("http://www.w3.org/2003/05/soap-envelope", "mustUnderstand"))
                                {
                                    long   l      = vn.getElementFragment();
                                    int    len    = (int)(l >> 32);
                                    int    offset = (int)l;
                                    byte[] b      = vn.getXML().getBytes();
                                    fos.Write(b, offset, len); //write the fragment out into out.txt
                                    System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("ASCII");
                                    byte[] bytes = encoder.GetBytes("\n=========\n");

                                    fos.Write(bytes, 0, bytes.Length);
                                }
                            }while (vn.toElement(VTDNav.NS)); // navigate next sibling
                        }
                        else
                        {
                            System.Console.Out.WriteLine("Header has not child elements");
                        }
                    }
                    else
                    {
                        System.Console.Out.WriteLine(" Dosesn't have a header");
                    }

                    fos.Close();
                }
            }
            catch (NavException e)
            {
                System.Console.Out.WriteLine(" Exception during navigation " + e);
            }
            catch (System.IO.IOException e)
            {
                System.Console.Out.WriteLine(" IO exception condition" + e);
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            try
            {
                // open file to output extracted fragments
                System.IO.FileInfo   f1  = new System.IO.FileInfo("./out.txt");
                System.IO.FileStream fos = new System.IO.FileStream(f1.FullName, System.IO.FileMode.Create);

                // instantiate the parser
                VTDGen vg = new VTDGen();
                vg.selectLcDepth(5);
                if (vg.parseFile("./soap2.xml", true))
                {
                    VTDNav vn = vg.getNav();
                    // get to the SOAP header
                    AutoPilot ap = new AutoPilot();
                    ap.bind(vn);
                    ap.declareXPathNameSpace("ns1", "http://www.w3.org/2003/05/soap-envelope");
                    // get to the SOAP header
                    ap.selectXPath("/ns1:Envelope/ns1:Header/*[@ns1:mustUnderstand]");
                    Console.WriteLine("expr string is " + ap.getExprString());
                    while (ap.evalXPath() != -1)
                    {
                        long   l      = vn.getElementFragment();
                        int    len    = (int)(l >> 32);
                        int    offset = (int)l;
                        byte[] b      = vn.getXML().getBytes();
                        fos.Write(b, offset, len); //write the fragment out into out.txt
                        System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("ASCII");
                        byte[] bytes = encoder.GetBytes("\n=========\n");

                        fos.Write(bytes, 0, bytes.Length);
                    }

                    fos.Close();
                }
            }
            catch (NavException e)
            {
                System.Console.Out.WriteLine(" Exception during navigation " + e);
            }
            catch (System.IO.IOException e)
            {
                System.Console.Out.WriteLine(" IO exception condition" + e);
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            try
            {
                int t;
                System.IO.FileInfo   f   = new System.IO.FileInfo("./soap2.xml");
                System.IO.FileStream fis =
                    new System.IO.FileStream(f.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                System.IO.FileInfo   f1  = new System.IO.FileInfo("./out.xml");
                System.IO.FileStream fos = new System.IO.FileStream(f1.FullName, System.IO.FileMode.Create);
                byte[] b = new byte[(int)f.Length];
                fis.Read(b, 0, (int)f.Length);
                AutoPilot ap = new AutoPilot();
                ap.declareXPathNameSpace("ns1", "http://www.w3.org/2003/05/soap-envelope");
                // get to the SOAP header
                ap.selectXPath("/ns1:Envelope/ns1:Header/*[@ns1:mustUnderstand]");
                Console.WriteLine("expr string is " + ap.getExprString());
                // instantiate the parser
                VTDGen vg = new VTDGen();
                int    j  = 0;
                VTDNav vn = null;
                while (j < 10)
                {
                    vg.setDoc_BR(b); // use setDoc_BR (instead of setDoc) to turn on buffer reuse
                    vg.parse(true);  // set namespace awareness to true
                    vn = vg.getNav();
                    ap.bind(vn);     // bind calls resetXPath() so

                    while ((t = ap.evalXPath()) != -1)
                    {
                        Console.WriteLine("j t--> " + j + " " + t);
                        long l      = vn.getElementFragment();
                        int  len    = (int)(l >> 32);
                        int  offset = (int)l;
                        fos.Write(b, offset, len); //write the fragment out into out.txt
                        System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("ASCII");
                        byte[] bytes = encoder.GetBytes("\n=========\n");

                        fos.Write(bytes, 0, bytes.Length);
                    }
                    ap.resetXPath();
                    j++;
                }
                j = 0;
                Console.WriteLine("j -->" + j);
                vg.setDoc_BR(b); // use setDoc_BR (instead of setDoc) to turn on buffer reuse
                vg.parse(true);  // set namespace awareness to true
                vn = vg.getNav();
                ap.bind(vn);     // bind calls resetXPath() so
                t = -1;
                while (j < 10)
                {
                    while ((t = ap.evalXPath()) != -1)
                    {
                        Console.WriteLine("j t --> " + j + " " + t);
                        long l      = vn.getElementFragment();
                        int  len    = (int)(l >> 32);
                        int  offset = (int)l;
                        fos.Write(b, offset, len); //write the fragment out into out.txt
                        System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("ASCII");
                        byte[] bytes = encoder.GetBytes("\n=========\n");

                        fos.Write(bytes, 0, bytes.Length);
                    }
                    ap.resetXPath();
                    j++;
                }

                fis.Close();
                fos.Close();
            }
            catch (ParseException e)
            {
            }
            catch (NavException e)
            {
            }
            catch (XPathParseException e)
            {
            }
            catch (XPathEvalException e)
            {
            }
            catch (System.IO.IOException e)
            {
            }
        }