Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

com.ximpleware.ModifyException: There can be

Saraswathy
2010-09-24
2013-05-15
  • Saraswathy
    Saraswathy
    2010-09-24

    Hi,

    I have 2 xmls and I need to create a 3rd xml out of the 2 xmls and modify one of the input xml

    Input

    XMl 1 is

    <aroot>
    <a1>apple</a1>
    <a2>apricot></a2>
    </aroot>

    XMl 2 is

    <broot>
    <b1>banana</b1>
    <b2>Blueberry</b2>
    <a3>acaiberry></a3>
    </broot>

    XMl 3 is

    <root3></root3>

    Expected output is

    XMl 1 is

    <aroot>
    <a1>apple</a1>
    <a2>apricot></a2>
    <a3>acaiberry</a3>
    </aroot>

    XMl 2 is

    <broot>
    <b1>banana</b1>
    <b2>Blueberry</b2>
    <a3>acaiberry></a3>
    </broot>

    XMl 3 is

    <root3>
    <a1>apple</a1>
    <b1>banana</b1>
    </root3>

    Here is my code:

    VTDGen vg1 = new VTDGen();
    VTDGen vg2 = new VTDGen();
    VTDGen vg3 = new VTDGen();

    String xml1 = "<aroot><a1>apple</a1><a2>apricot></a2></aroot>";
    String xml2 = "<broot><b1>banana</b1><b2>Blueberry</b2><a3>acaiberry</a3></broot>";
    String xml3 = "<root3></root3>";

    byte theBytes1 = xml1.getBytes();
            byte theBytes2 = xml2.getBytes();
            byte theBytes3 = xml3.getBytes();
         
            vg1.setDoc(theBytes1);
            vg1.parse(true);
            VTDNav vn1 = vg1.getNav();
          
           vg2.setDoc(theBytes2);
           vg2.parse(true);
           VTDNav vn2 = vg2.getNav();

          vg3.setDoc(theBytes3);
          vg3.parse(true);
          VTDNav vn3 = vg3.getNav();
           
        XMLModifier xm1 = new XMLModifier(vn1);
        XMLModifier xm3 = new XMLModifier(vn3);

          AutoPilot ap1 = new AutoPilot(vn1);
          AutoPilot ap2 = new AutoPilot(vn2);
          AutoPilot ap3 = new AutoPilot(vn3);
         
          ap1.selectXPath("/aroot/a2");
          ap2.selectXPath("/broot/a3");
          ap3.selectXPath("/root3");

          if (vn2.toElement(VTDNav.ROOT,  "broot")) {
          if(vn2.toElement(VTDNav.FIRST_CHILD,  "b1")){
          long efn = vn2.getElementFragment();
          while(ap3.evalXPath()!=-1) {
        xm3.insertAfterHead(vn2, efn);
        }
        if (vn2.toElement(VTDNav.LAST_CHILD,  "a3")){
        efn = vn2.getElementFragment();
        while(ap1.evalXPath()!=-1){
    xm1.insertAfterElement(vn2, efn);
    }
        }
          }
          }
           if (vn1.toElement(VTDNav.ROOT,  "aroot")) {
           if (vn1.toElement(VTDNav.FIRST_CHILD,  "a1")) {
    long efn = vn1.getElementFragment();
    ap3.resetXPath();
    if(ap3.evalXPath()!=-1){
    System.out.println("--inserting in root3-");
    xm3.insertAfterHead(vn1, efn);
    }
        }
           }

    xm1.output(new FileOutputStream("C:/web/aroot.xml"));
    xm3.output(new FileOutputStream("C:/web/root3.xml"));

    14:06:50,508 INFO   --inserting in root3-
    14:06:50,508 ERROR  com.ximpleware.ModifyException: There can be only one insert per offset
    14:06:50,508 ERROR      at com.ximpleware.XMLModifier.insertBytesAt(XMLModifier.java:371)
    14:06:50,508 ERROR      at com.ximpleware.XMLModifier.insertAfterHead(XMLModifier.java:907)
    14:06:50,508 ERROR      at com.ximpleware.XMLModifier.insertAfterHead(XMLModifier.java:831)
    14:06:50,508 ERROR      at com.ximpleware.XMLModifier.insertAfterHead(XMLModifier.java:1106)
    14:06:50,508 ERROR      at com.csa. action.SplitAction.execute(Unknown Source)
    14:06:50,508 ERROR      at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
    14:06:50,508 ERROR      at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
    14:06:50,508 ERROR      at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    14:06:50,508 ERROR      at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    14:06:50,508 ERROR      at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    14:06:50,508 ERROR      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    14:06:50,508 ERROR      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    14:06:50,508 ERROR      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    14:06:50,508 ERROR      at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    14:06:50,508 ERROR      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    14:06:50,508 ERROR      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    14:06:50,508 ERROR      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    14:06:50,508 ERROR      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:173)
    14:06:50,508 ERROR      at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
    14:06:50,508 ERROR      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
    14:06:50,508 ERROR      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    14:06:50,508 ERROR      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    14:06:50,508 ERROR      at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
    14:06:50,508 ERROR      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    14:06:50,508 ERROR      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
    14:06:50,508 ERROR      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    14:06:50,508 ERROR      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    14:06:50,508 ERROR      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    14:06:50,508 ERROR      at java.lang.Thread.run(Thread.java:595)
    14:06:50,586 ERROR  Servlet.service() for servlet jsp threw exception

    Can someone please help me?

     
  • jimmy zhang
    jimmy zhang
    2010-09-25

    Hi, I think that, instead of inserting twice, would it be possible to create xml from scratch, open an input stream

    append  <root>
    append  <a1>apple</a1>
    append  <b1>banana</b1>
    append </root>

    that should get rid of the problem

     
  • Saraswathy
    Saraswathy
    2010-09-27

    Did you mean adding adding to a stringbuffer.

    I could do that.

    Thank you.
    Saraswathy.

     
  • jimmy zhang
    jimmy zhang
    2010-09-28

    what I meant is the byte array concatnation? think this way, by adding two byte array to the same offset, it is not clear whether you want to have

    <root3>  <a1>apple</a1>  <b1>banana</b1> </root3>

    or

    <root3>  <b1>banana</b1> <a1>apple</a1>  </root3>

    so you need to specify that in your code

     
  • Saraswathy
    Saraswathy
    2010-09-28

    Thank you.

    I agree the code I have written is for <root3>  <b1>banana</b1> <a1>apple</a1>  </root3>  even though I mentioned that the desired output was <root3>  <a1>apple</a1>  <b1>banana</b1> </root3>