RE: Modifying XmlBeans document without XmlValueDisconnectedException?
"Paul Gillen" <[email protected]> Mon, 19 Aug 2013 21:02:25 -0400
| Newsgroups | gmane.text.xml.xmlbeans.user |
|---|---|
| Message-ID | <[email protected]> |
------=_NextPart_000_00AB_01CE9D1F.6913DEE0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
Since I think I can answer your question I probably misunderstand it. :)
However ...
Given an XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="org.eltesto" elementFormDefault="qualified"
attributeFormDefault="unqualified" xmlns="org.eltesto"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Rootie">
<xs:complexType>
<xs:sequence>
<xs:element name="ItLives" type="xs:string"/>
<xs:element name="DoorNail" type="xs:string"
minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And XML:
<?xml version="1.0" encoding="UTF-8"?>
<Rootie xmlns="org.eltesto"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="org.eltesto
xsd\Rootie.xsd">
<ItLives>foo</ItLives>
<DoorNail>bar</DoorNail>
</Rootie>
This program modifies and deletes element DoorNail:
package org.eltesto;
import java.io.File;import eltesto.org.RootieDocument;
import eltesto.org.RootieDocument.Rootie;
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("Rootie.xml");
RootieDocument rd = RootieDocument.Factory.parse(f);
System.out.println(rd.xmlText());
Rootie r = rd.getRootie();
r.setDoorNail("modified");
System.out.println(rd.xmlText());
r.unsetDoorNail();
System.out.println(rd.xmlText());
}
}
Output:
<Rootie xsi:schemaLocation="org.eltesto xsd\Rootie.xsd" xmlns="org.eltesto"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ItLives>foo</ItLives>
<DoorNail>bar</DoorNail>
</Rootie>
<Rootie xsi:schemaLocation="org.eltesto xsd\Rootie.xsd" xmlns="org.eltesto"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ItLives>foo</ItLives>
<DoorNail>modified</DoorNail>
</Rootie>
<Rootie xsi:schemaLocation="org.eltesto xsd\Rootie.xsd" xmlns="org.eltesto"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ItLives>foo</ItLives>
</Rootie>
Note that you only get the "unset." method if the element is declared as
optional, i.e. 'minOccurs="0"' which is as it should be.
Cordially,
Paul Gillen
-----Original Message-----
From: Lott, Christopher M [mailto:[email protected]]
Sent: Monday, August 19, 2013 5:33 PM
To: [email protected]
Subject: Modifying XmlBeans document without XmlValueDisconnectedException?
I tried asking this on StackOverflow, but then realized that this email list
is probably the better place, so please forgive if you read both places. I
hope someone can please find time to answer.
We use XmlBeans, code generated from a schema, to read in an XML instance
that conforms to the schema and modify it. Read and validate work fine;
modify is killing me. For example, change the value of an element, delete
an element from a list, etc. I am using Java method calls like
removeMyElement(i) to remove the element at position (i). I am not using
Cursors or any low-level DOM access.
We thought it would be convenient to walk thru the document, gather a list
of elements (i.e., references to XmlBeans objects) that need to be modified
or nuked, then iterate over the list changing them. But use of a cached
object reference is where I run into the ditch: I get
XmlValueDisconnectedException. I read that this means the XmlObject has
become disconnected from its underlying store. But I have not discarded the
Document!
Does every change to an XmlObject backed by the XmlBeans XmlStore cause all
existing references to become invalid? I don't yet have the right mental
model for what's going on when I call the java methods.
I checked the XmlBeans FAQ and the sample code for guidelines. I found many
samples at <http://xmlbeans.apache.org/samples/>
http://xmlbeans.apache.org/samples/ that show how to create an object from
scratch, how to read in XML, how to validate XML, etc. Unfortunately I
didn't find a sample that does a messy change to the content. The FAQ at
<http://wiki.apache.org/xmlbeans/XmlBeansFaq>
http://wiki.apache.org/xmlbeans/XmlBeansFaq doesn't have a good question on
this either.
Thanks in advance.
---------------------------------------------------------------------
To unsubscribe, e-mail: <mailto:[email protected]>
[email protected]
For additional commands, e-mail: <mailto:[email protected]>
[email protected]
------=_NextPart_000_00AB_01CE9D1F.6913DEE0
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
<html xmlns:v=3D"urn:schemas-microsoft-com:vml" =
xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" =
xmlns=3D"http://www.w3.org/TR/REC-html40"><head><meta =
http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dus-ascii"><meta name=3DGenerator content=3D"Microsoft Word 14 =
(filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p.MsoPlainText, li.MsoPlainText, div.MsoPlainText
{mso-style-priority:99;
mso-style-link:"Plain Text Char";
margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
span.PlainTextChar
{mso-style-name:"Plain Text Char";
mso-style-priority:99;
mso-style-link:"Plain Text";
font-family:"Calibri","sans-serif";}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]--></head><body lang=3DEN-US link=3Dblue =
vlink=3Dpurple><div class=3DWordSection1><p class=3DMsoPlainText>Since I =
think I can answer your question I probably misunderstand it. =
:) However ...<o:p></o:p></p><p =
class=3DMsoPlainText><o:p> </o:p></p><p class=3DMsoPlainText>Given =
an XSD:<o:p></o:p></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:9.0pt;font-family:"Courier New"'><?xml =
version=3D"1.0" =
encoding=3D"UTF-8"?><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:9.0pt;font-family:"Courier New"'><xs:schema =
targetNamespace=3D"org.eltesto" =
elementFormDefault=3D"qualified" =
attributeFormDefault=3D"unqualified" =
xmlns=3D"org.eltesto" =
xmlns:xs=3D"http://www.w3.org/2001/XMLSchema"><o:p></o:p></s=
pan></p><p class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:9.0pt;font-family:"Courier =
New"'> <xs:element =
name=3D"Rootie"><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:9.0pt;font-family:"Courier =
New"'> &=
nbsp; <xs:complexType><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:9.0pt;font-family:"Courier =
New"'> &=
nbsp; =
<xs:sequence><o:p></o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:9.0pt;font-family:"Courier =
New"'> &=
nbsp; &n=
bsp; <xs:element name=3D"ItLives" =
type=3D"xs:string"/><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:9.0pt;font-family:"Courier =
New"'> &=
nbsp; &n=
bsp; <xs:element name=3D"DoorNail" =
type=3D"xs:string" =
minOccurs=3D"0"/><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:9.0pt;font-family:"Courier =
New"'> &=
nbsp; =
</xs:sequence><o:p></o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:9.0pt;font-family:"Courier =
New"'> &=
nbsp; </xs:complexType><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:9.0pt;font-family:"Courier =
New"'> =
</xs:element><o:p></o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:9.0pt;font-family:"Courier =
New"'></xs:schema><o:p></o:p></span></p><p =
class=3DMsoPlainText><o:p> </o:p></p><p class=3DMsoPlainText>And =
XML:<o:p></o:p></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier New"'><?xml =
version=3D"1.0" =
encoding=3D"UTF-8"?><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier New"'><Rootie =
xmlns=3D"org.eltesto" =
xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance" =
xsi:schemaLocation=3D"org.eltesto<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'>xsd\Rootie.xsd"><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> =
<ItLives>foo</ItLives><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> =
<DoorNail>bar</DoorNail><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'></Rootie><o:p></o:p></span></p><p =
class=3DMsoPlainText><o:p> </o:p></p><p class=3DMsoPlainText>This =
program modifies and deletes element DoorNail:<o:p></o:p></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier New"'>package =
org.eltesto;<o:p></o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'><o:p> </o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier New"'>import =
java.io.File;import eltesto.org.RootieDocument;<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier New"'>import =
eltesto.org.RootieDocument.Rootie;<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'><o:p> </o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier New"'>public class Main =
{<o:p></o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> public static void =
main(String[] args) throws Exception {<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> &=
nbsp; Main m =3D new Main();<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> &=
nbsp; m.go(args);<o:p></o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> }<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> private void go(String[] =
args) throws Exception {<o:p></o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> &=
nbsp; File f =3D new =
File("Rootie.xml");<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> &=
nbsp; RootieDocument rd =3D =
RootieDocument.Factory.parse(f);<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> &=
nbsp; System.out.println(rd.xmlText());<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> &=
nbsp; Rootie r =3D rd.getRootie();<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> &=
nbsp; r.setDoorNail("modified");<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> &=
nbsp; System.out.println(rd.xmlText());<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> &=
nbsp; r.unsetDoorNail();<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> &=
nbsp; System.out.println(rd.xmlText());<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> }<o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'>}<o:p></o:p></span></p><p =
class=3DMsoPlainText><o:p> </o:p></p><p =
class=3DMsoPlainText>Output:<o:p></o:p></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier New"'><Rootie =
xsi:schemaLocation=3D"org.eltesto xsd\Rootie.xsd" =
xmlns=3D"org.eltesto" =
xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance"><o:p=
></o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> =
<ItLives>foo</ItLives><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> =
<DoorNail>bar</DoorNail><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'></Rootie><o:p></o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'><o:p> </o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier New"'><Rootie =
xsi:schemaLocation=3D"org.eltesto xsd\Rootie.xsd" =
xmlns=3D"org.eltesto" =
xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance"><o:p=
></o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> =
<ItLives>foo</ItLives><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> =
<DoorNail>modified</DoorNail><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'></Rootie><o:p></o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'><o:p> </o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier New"'><Rootie =
xsi:schemaLocation=3D"org.eltesto xsd\Rootie.xsd" =
xmlns=3D"org.eltesto" =
xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance"><o:p=
></o:p></span></p><p class=3DMsoPlainText =
style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> =
<ItLives>foo</ItLives><o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'> <o:p></o:p></span></p><p =
class=3DMsoPlainText style=3D'margin-left:.5in'><span =
style=3D'font-size:8.0pt;font-family:"Courier =
New"'></Rootie><o:p></o:p></span></p><p =
class=3DMsoPlainText><o:p> </o:p></p><p class=3DMsoPlainText>Note =
that you only get the “unset…” method if the element =
is declared as optional, i.e. ‘minOccurs=3D"0"’ =
which is as it should be.<o:p></o:p></p><p =
class=3DMsoPlainText><o:p> </o:p></p><p =
class=3DMsoPlainText>Cordially,<o:p></o:p></p><p =
class=3DMsoPlainText>Paul Gillen<o:p></o:p></p><p =
class=3DMsoPlainText><o:p> </o:p></p><p =
class=3DMsoPlainText><o:p> </o:p></p><p =
class=3DMsoPlainText>-----Original Message-----<br>From: Lott, =
Christopher M [mailto:[email protected]] <br>Sent: Monday, August 19, =
2013 5:33 PM<br>To: [email protected]<br>Subject: Modifying =
XmlBeans document without XmlValueDisconnectedException?</p><p =
class=3DMsoPlainText><o:p> </o:p></p><p class=3DMsoPlainText>I =
tried asking this on StackOverflow, but then realized that this email =
list is probably the better place, so please forgive if you read both =
places. I hope someone can please find time to =
answer.<o:p></o:p></p><p class=3DMsoPlainText><o:p> </o:p></p><p =
class=3DMsoPlainText>We use XmlBeans, code generated from a schema, to =
read in an XML instance that conforms to the schema and modify it. =
Read and validate work fine; modify is killing me. For example, =
change the value of an element, delete an element from a list, =
etc. I am using Java method calls like removeMyElement(i) to =
remove the element at position (i). I am not using Cursors or any =
low-level DOM access.<o:p></o:p></p><p =
class=3DMsoPlainText><o:p> </o:p></p><p class=3DMsoPlainText>We =
thought it would be convenient to walk thru the document, gather a list =
of elements (i.e., references to XmlBeans objects) that need to be =
modified or nuked, then iterate over the list changing them. But =
use of a cached object reference is where I run into the ditch: I get =
XmlValueDisconnectedException. I read that this means the =
XmlObject has become disconnected from its underlying store. But I have =
not discarded the Document!<o:p></o:p></p><p =
class=3DMsoPlainText><o:p> </o:p></p><p class=3DMsoPlainText>Does =
every change to an XmlObject backed by the XmlBeans XmlStore cause all =
existing references to become invalid? I don't yet have the right =
mental model for what's going on when I call the java =
methods.<o:p></o:p></p><p class=3DMsoPlainText><o:p> </o:p></p><p =
class=3DMsoPlainText>I checked the XmlBeans FAQ and the sample code for =
guidelines. I found many samples at <a =
href=3D"http://xmlbeans.apache.org/samples/"><span =
style=3D'color:windowtext;text-decoration:none'>http://xmlbeans.apache.or=
g/samples/</span></a> that show how to create an object from scratch, =
how to read in XML, how to validate XML, etc. Unfortunately I =
didn't find a sample that does a messy change to the content. The FAQ at =
<a href=3D"http://wiki.apache.org/xmlbeans/XmlBeansFaq"><span =
style=3D'color:windowtext;text-decoration:none'>http://wiki.apache.org/xm=
lbeans/XmlBeansFaq</span></a> doesn't have a good question on this =
either.<o:p></o:p></p><p class=3DMsoPlainText><o:p> </o:p></p><p =
class=3DMsoPlainText>Thanks in advance.<o:p></o:p></p><p =
class=3DMsoPlainText>----------------------------------------------------=
-----------------<o:p></o:p></p><p class=3DMsoPlainText>To unsubscribe, =
e-mail: <a href=3D"mailto:[email protected]"><span =
style=3D'color:windowtext;text-decoration:none'>user-unsubscribe@xmlbeans=
.apache.org</span></a><o:p></o:p></p><p class=3DMsoPlainText>For =
additional commands, e-mail: <a =
href=3D"mailto:[email protected]"><span =
style=3D'color:windowtext;text-decoration:none'>[email protected]=
.org</span></a><o:p></o:p></p><p =
class=3DMsoPlainText><o:p> </o:p></p></div></body></html>
------=_NextPart_000_00AB_01CE9D1F.6913DEE0--