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:cl...@appcomsci.com] 
Sent: Monday, August 19, 2013 5:33 PM
To: user@xmlbeans.apache.org
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: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