If I understand you correctly you are trying to print something you removed;
it gone baby.  If for some reason you need it, preserve a copy, not a
reference.

 

package org.eltesto;

 

import java.io.File;

import java.math.BigDecimal;

import java.math.BigInteger;

 

import org.openuri.easypo.LineItem;

import org.openuri.easypo.PurchaseOrderDocument;

import org.openuri.easypo.PurchaseOrderDocument.PurchaseOrder;

 

public class Main {

       public static void main(String[] args) throws Exception {

              Main m = new Main();

              m.go(args);

       }

       private void go(String[] args) throws Exception {

              File f = new File("easypo.xml");

              PurchaseOrderDocument pd =
PurchaseOrderDocument.Factory.parse(f);

              System.out.println("ORIGINAL\n"+pd.xmlText());

              

              PurchaseOrder p = pd.getPurchaseOrder();

              

              LineItem l = p.addNewLineItem();

              l.setDescription("foo");

              l.setPerUnitOunces(new BigDecimal("1"));

              l.setPrice(new BigDecimal("2"));

              l.setQuantity(new BigInteger("3"));

              System.out.println("\nLINE ITEM ADDED\n"+pd.xmlText());

              

              LineItem nl =
LineItem.Factory.parse(p.getLineItemArray(0).toString());

              

              p.removeLineItem(0);

              System.out.println("\nLINE ITEM DELETED\n"+pd.xmlText());

              

              System.out.println("\nCOPY OF DELETED LINE
ITEM\n"+nl.xmlText());

       }

}

 

Output:

ORIGINAL

<purchase-order xmlns="http://openuri.org/easypo";>

    <customer>

        <name>Gladys Kravitz</name>

        <address>Anytown, PA</address>

    </customer>

    <date>2003-01-07T14:16:00-05:00</date>

    <line-item>

        <description>Burnham's Celestial Handbook, Vol 1</description>

        <per-unit-ounces>5</per-unit-ounces>

        <price>21.79</price>

        <quantity>2</quantity>

    </line-item>

    <line-item>

        <description>Burnham's Celestial Handbook, Vol 2</description>

        <per-unit-ounces>5</per-unit-ounces>

        <price>19.89</price>

        <quantity>2</quantity>

    </line-item>

    <shipper>

        <name>ZipShip</name>

        <per-ounce-rate>0.74</per-ounce-rate>

    </shipper>

</purchase-order>

 

LINE ITEM ADDED

<purchase-order xmlns="http://openuri.org/easypo";>

    <customer>

        <name>Gladys Kravitz</name>

        <address>Anytown, PA</address>

    </customer>

    <date>2003-01-07T14:16:00-05:00</date>

    <line-item>

        <description>Burnham's Celestial Handbook, Vol 1</description>

        <per-unit-ounces>5</per-unit-ounces>

        <price>21.79</price>

        <quantity>2</quantity>

    </line-item>

    <line-item>

        <description>Burnham's Celestial Handbook, Vol 2</description>

        <per-unit-ounces>5</per-unit-ounces>

        <price>19.89</price>

        <quantity>2</quantity>

    </line-item>

 
<line-item><description>foo</description><per-unit-ounces>1</per-unit-ounces
><price>2</price><quantity>3</quantity></line-item><shipper>

        <name>ZipShip</name>

        <per-ounce-rate>0.74</per-ounce-rate>

    </shipper>

</purchase-order>

 

LINE ITEM DELETED

<purchase-order xmlns="http://openuri.org/easypo";>

    <customer>

        <name>Gladys Kravitz</name>

        <address>Anytown, PA</address>

    </customer>

    <date>2003-01-07T14:16:00-05:00</date>

    

    <line-item>

        <description>Burnham's Celestial Handbook, Vol 2</description>

        <per-unit-ounces>5</per-unit-ounces>

        <price>19.89</price>

        <quantity>2</quantity>

    </line-item>

 
<line-item><description>foo</description><per-unit-ounces>1</per-unit-ounces
><price>2</price><quantity>3</quantity></line-item><shipper>

        <name>ZipShip</name>

        <per-ounce-rate>0.74</per-ounce-rate>

    </shipper>

</purchase-order>

 

COPY OF DELETED LINE ITEM

<xml-fragment xmlns:eas="http://openuri.org/easypo";>

  <eas:description>Burnham's Celestial Handbook, Vol 1</eas:description>

  <eas:per-unit-ounces>5</eas:per-unit-ounces>

  <eas:price>21.79</eas:price>

  <eas:quantity>2</eas:quantity>

</xml-fragment>

 

On the other hand I might be completely FOS.  Please let me know if this is
the case.

 

Cordially,

Paul Gillen

 

 

 

-----Original Message-----
From: Lott, Christopher M [mailto:cl...@appcomsci.com] 
Sent: Monday, August 19, 2013 9:33 PM
To: <user@xmlbeans.apache.org>
Subject: Re: Modifying XmlBeans document without
XmlValueDisconnectedException?

 

Thanks to Nick B. and Paul G. for their quick replies!  Below is code that
demonstrates, at least superficially, the exception that we get.  We are
doing something wrong in our code; I don't suspect an XmlBeans bug at this
point.  This code is derived from (and depends on the schema and example XML
instance from) the XmlBeans tutorial at
<http://xmlbeans.apache.org/documentation/tutorial_getstarted.html>
http://xmlbeans.apache.org/documentation/tutorial_getstarted.html

 

--

package my.xb.example;

 

import java.io.File;

import java.math.BigDecimal;

import java.math.BigInteger;

 

import org.openuri.easypo.LineItem;

import org.openuri.easypo.PurchaseOrderDocument;

 

public class POUpdater {

                public static void main(String[] args) throws Exception {

                                if (args.length < 5)

                                                throw new Exception("Usage:
inputfile desc ounces price quantity");

                                File poXmlFile = new File(args[0]);

                                String updatedPoXml = addLineItem(poXmlFile,
args[1], args[2], args[3],

                                                                args[4]);

                                System.out.println(updatedPoXml);

                }

 

                private static String addLineItem(File purchaseOrder,

                                                String itemDescription,
String perUnitOuncesString,

                                                String itemPriceString,
String itemQuantityString) throws Exception {

                                

                                // Read in the file content

                                PurchaseOrderDocument poDoc =
PurchaseOrderDocument.Factory

 
.parse(purchaseOrder);

 

                                // Convert incoming data to types that can
be used in accessors.

                                BigDecimal perUnitOunces = new
BigDecimal(perUnitOuncesString);

                                BigDecimal itemPrice = new
BigDecimal(itemPriceString);

                                BigInteger itemQuantity = new
BigInteger(itemQuantityString);

 

                                // Add a new <line-item> element.

                                LineItem newItem =
poDoc.getPurchaseOrder().addNewLineItem();

                                newItem.setDescription(itemDescription);

                                newItem.setPerUnitOunces(perUnitOunces);

                                newItem.setPrice(itemPrice);

                                newItem.setQuantity(itemQuantity);

 

                                // Delete the old first line item

                                LineItem removedItem =
poDoc.getPurchaseOrder().getLineItemArray(0);

                                poDoc.getPurchaseOrder().removeLineItem(0);

 

                                // Try to access the newly added one; is the
ref still good?

                                System.out.println("new item: " +
newItem.toString());

 
newItem.setDescription(newItem.getDescription() + " fu");

 

                                // Try to access the removed one; this
throws XmlValueDisconnectedException

                                System.out.println("removed item: " +
removedItem.toString());

                                

                                return poDoc.toString();

                }

}

 

 

 

 

---------------------------------------------------------------------

To unsubscribe, e-mail:  <mailto:user-unsubscr...@xmlbeans.apache.org>
user-unsubscr...@xmlbeans.apache.org

For additional commands, e-mail:  <mailto:user-h...@xmlbeans.apache.org>
user-h...@xmlbeans.apache.org

 

Reply via email to