Re: Modifying XmlBeans document without XmlValueDisconnectedException?
"Lott, Christopher M" <[email protected]> Tue, 20 Aug 2013 01:33:03 +0000
| Newsgroups | gmane.text.xml.xmlbeans.user |
|---|---|
| Message-ID | <[email protected]> |
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 do=
ing something wrong in our code; I don't suspect an XmlBeans bug at this po=
int. This code is derived from (and depends on the schema and example XML =
instance from) the XmlBeans tutorial at http://xmlbeans.apache.org/document=
ation/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 =3D new File(args[0]);
String updatedPoXml =3D 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 {
=09
// Read in the file content
PurchaseOrderDocument poDoc =3D PurchaseOrderDocument.Factory
.parse(purchaseOrder);
// Convert incoming data to types that can be used in accessors.
BigDecimal perUnitOunces =3D new BigDecimal(perUnitOuncesString);
BigDecimal itemPrice =3D new BigDecimal(itemPriceString);
BigInteger itemQuantity =3D new BigInteger(itemQuantityString);
// Add a new <line-item> element.
LineItem newItem =3D poDoc.getPurchaseOrder().addNewLineItem();
newItem.setDescription(itemDescription);
newItem.setPerUnitOunces(perUnitOunces);
newItem.setPrice(itemPrice);
newItem.setQuantity(itemQuantity);
// Delete the old first line item
LineItem removedItem =3D 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 XmlValueDisconnectedExcepti=
on
System.out.println("removed item: " + removedItem.toString());
=09
return poDoc.toString();
}
}