You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Jakub Kahovec <j....@imperial.ac.uk> on 2005/02/15 15:46:52 UTC

Re: how to exchange XML content - solution and question

I've found the solution how to change the content of the element. It's 
possible to do by following way :
 
      /* templateValueType xml content
          <setTemplateValue>
              <randomInteger min="1" max="5" step="1"/>
          </setTemplateValue>
     */

        XmlCursor cursor = templateValueType.newCursor();

        cursor.removeXmlContents();
       
        XmlObject xmlObject;
        try {
            xmlObject = XmlObject.Factory.parse("<result><randomFloat 
min=\"1.0\" max=\"5.5\" step=\"1\"/></result>");
        } catch (XmlException ex) {
            ex.printStackTrace();
        }
        cursor.toFirstContentToken();
       
        XmlCursor newCursor = xmlObject.newCursor();
        newCursor.toFirstContentToken();
       
        newCursor.copyXmlContents(cursor);

      /* templateValueType xml content
          <setTemplateValue>
              <randomFloat min="1" max="5" step="1"/>
          </setTemplateValue>
     */

The xml content of the variable templateValueType is changed now. 
However, when i call  the method 'getRandomFloat' I get 'null'. 
I suppose that I have to re-initialize the variable templateValueType. 
Is there is some recommendation or an convenient way how to manage it ?

Thanks


Jakub Kahovec wrote:

> I've already tried it but neither moveXml() nor moveXmlContents() 
> works as I need.
> This is a piece of my code :
>
> /*  this is the xml content of  variable templateValueType
>
> <setTemplateValue id="1" >
>       <randonInteger min="2" max="3"/>
>     </setTemplateValue>
>
> */
>
> XmlCursor cursor = templateValueType.newCursor();
>
> cursor.removeXmlContents();
>       XmlObject xmlObject = null;
> try {
>     xmlObject = XmlObject.Factory.parse("<result><randomInteger 
> min=\"2\" max=\"4\" step=\"1\"/></result>");
> } catch (XmlException ex) {
>    ex.printStackTrace();
> }
>
> // xmlObject.newCursor().moveXml(cursor);    // doesn't work , throw 
> java.lang.IllegalArgumentException: Can't move/copy/insert a whole 
> document.
> xmlObject.newCursor().moveXmlContents(cursor); //works, but insert the 
> element <result> as previous sibling of setTemplateValue resp. child 
> of its parent element instead
>
> after that the xml document look like this
>
> <item>    <result>
>       <randomInteger min="2" max="4" step="1"/>
>   </result>
>   <setTemplateValue id="1" />
> </item>
>
> but i'd like to get following
>
> <item>       <setTemplateValue id="1" />
>          <randomInteger min="2" max="4" step="1"/>
>       </setTemplateValue>
> </item>
>
>
> So, what i've been doing wrong ?
>
>
>
>
> Radu Preotiuc-Pietro wrote:
>
>> Take a look at XmlCursor.moveXml() too, which is faster.
>> Depending on your Schema, XmlObject.set() may also work.
>>
>> Radu
>> -----Original Message-----
>> From: Jacob Danner Sent: Monday, February 14, 2005 10:26 AM
>> To: user@xmlbeans.apache.org
>> Subject: RE: how to exchange XML content
>>
>>
>> You should give substitution groups. There's a new API in v2 
>> specifically for substitution, give it a try. I contributed some 
>> tests for the API, you can see the code for example usages.
>> The test src lives off trunk at:
>> test/src/xmlobject/detailed/SubstGroupTests.java
>> it uses schema types from
>> test/cases/xbean/xmlobject/substgroup2/xsd
>> Let us know what you think of the new API,
>> -Jacobd
>>
>>     -----Original Message-----     From: Jakub Kahovec 
>> [mailto:j.kahovec@imperial.ac.uk]     Sent: Mon 2/14/2005 8:26 AM 
>>     To: user@xmlbeans.apache.org     Cc:     Subject: how to exchange 
>> XML content
>>     
>>     
>>
>>     Hello,
>>     i'm using XmlBeans v.2. i'd like to ask if there does exist some 
>> easy
>>     way how to exchange the xml content (without root) of some 
>> element for
>>     another one.
>>     
>>     For example i've got the following element
>>     
>>     <setTemplateValue id="1" >
>>       <randonInteger min="2" max="3"/>
>>     </setTemplateValue>
>>     
>>     and new one
>>     
>>     <newTemplateValue>
>>        <randonFloat min="1" max="6"/>
>>       <required/>
>>     </newTemplateValue
>>     
>>     and I'd like to exchange the content of first element for second 
>> one, so
>>     that first element look like
>>     
>>     <setTemplateValue id="1" >
>>        <randonFloat min="1" max="6"/>
>>       <required/>
>>     </setTemplateValue>
>>     
>>     
>>     Thanks so much
>>     
>>     Jakub
>>     
>>     
>>     
>>     
>>     
>>     
>>     --------------------------------------------------------------------- 
>>
>>     To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
>>     For additional commands, e-mail: user-help@xmlbeans.apache.org
>>     
>>     
>>
>>  
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org


Re: how to exchange XML content - final solution

Posted by Jakub Kahovec <j....@imperial.ac.uk>.
Hi,
 i've found the final solution ;-)  There must be specified the 
namespace when the element is being parsed.
(something like that    xmlObject = XmlObject.Factory.parse("<result 
xmlns="http://math.org/MathNS"><randomFloat min=\"1.0\" max=\"5.5\" 
step=\"1\"/></result>").

then everything works smoothly and there is no need to reinitialize a 
variable.


Jakub Kahovec wrote:

> I've found the solution how to change the content of the element. It's 
> possible to do by following way :
>
>      /* templateValueType xml content
>          <setTemplateValue>
>              <randomInteger min="1" max="5" step="1"/>
>          </setTemplateValue>
>     */
>
>        XmlCursor cursor = templateValueType.newCursor();
>
>        cursor.removeXmlContents();
>              XmlObject xmlObject;
>        try {
>            xmlObject = XmlObject.Factory.parse("<result><randomFloat 
> min=\"1.0\" max=\"5.5\" step=\"1\"/></result>");
>        } catch (XmlException ex) {
>            ex.printStackTrace();
>        }
>        cursor.toFirstContentToken();
>              XmlCursor newCursor = xmlObject.newCursor();
>        newCursor.toFirstContentToken();
>              newCursor.copyXmlContents(cursor);
>
>      /* templateValueType xml content
>          <setTemplateValue>
>              <randomFloat min="1" max="5" step="1"/>
>          </setTemplateValue>
>     */
>
> The xml content of the variable templateValueType is changed now. 
> However, when i call  the method 'getRandomFloat' I get 'null'. I 
> suppose that I have to re-initialize the variable templateValueType. 
> Is there is some recommendation or an convenient way how to manage it ?
>
> Thanks
>
>
> Jakub Kahovec wrote:
>
>> I've already tried it but neither moveXml() nor moveXmlContents() 
>> works as I need.
>> This is a piece of my code :
>>
>> /*  this is the xml content of  variable templateValueType
>>
>> <setTemplateValue id="1" >
>>       <randonInteger min="2" max="3"/>
>>     </setTemplateValue>
>>
>> */
>>
>> XmlCursor cursor = templateValueType.newCursor();
>>
>> cursor.removeXmlContents();
>>       XmlObject xmlObject = null;
>> try {
>>     xmlObject = XmlObject.Factory.parse("<result><randomInteger 
>> min=\"2\" max=\"4\" step=\"1\"/></result>");
>> } catch (XmlException ex) {
>>    ex.printStackTrace();
>> }
>>
>> // xmlObject.newCursor().moveXml(cursor);    // doesn't work , throw 
>> java.lang.IllegalArgumentException: Can't move/copy/insert a whole 
>> document.
>> xmlObject.newCursor().moveXmlContents(cursor); //works, but insert 
>> the element <result> as previous sibling of setTemplateValue resp. 
>> child of its parent element instead
>>
>> after that the xml document look like this
>>
>> <item>    <result>
>>       <randomInteger min="2" max="4" step="1"/>
>>   </result>
>>   <setTemplateValue id="1" />
>> </item>
>>
>> but i'd like to get following
>>
>> <item>       <setTemplateValue id="1" />
>>          <randomInteger min="2" max="4" step="1"/>
>>       </setTemplateValue>
>> </item>
>>
>>
>> So, what i've been doing wrong ?
>>
>>
>>
>>
>> Radu Preotiuc-Pietro wrote:
>>
>>> Take a look at XmlCursor.moveXml() too, which is faster.
>>> Depending on your Schema, XmlObject.set() may also work.
>>>
>>> Radu
>>> -----Original Message-----
>>> From: Jacob Danner Sent: Monday, February 14, 2005 10:26 AM
>>> To: user@xmlbeans.apache.org
>>> Subject: RE: how to exchange XML content
>>>
>>>
>>> You should give substitution groups. There's a new API in v2 
>>> specifically for substitution, give it a try. I contributed some 
>>> tests for the API, you can see the code for example usages.
>>> The test src lives off trunk at:
>>> test/src/xmlobject/detailed/SubstGroupTests.java
>>> it uses schema types from
>>> test/cases/xbean/xmlobject/substgroup2/xsd
>>> Let us know what you think of the new API,
>>> -Jacobd
>>>
>>>     -----Original Message-----     From: Jakub Kahovec 
>>> [mailto:j.kahovec@imperial.ac.uk]     Sent: Mon 2/14/2005 8:26 AM 
>>>     To: user@xmlbeans.apache.org     Cc:     Subject: how to 
>>> exchange XML content
>>>        
>>>     Hello,
>>>     i'm using XmlBeans v.2. i'd like to ask if there does exist some 
>>> easy
>>>     way how to exchange the xml content (without root) of some 
>>> element for
>>>     another one.
>>>         For example i've got the following element
>>>         <setTemplateValue id="1" >
>>>       <randonInteger min="2" max="3"/>
>>>     </setTemplateValue>
>>>         and new one
>>>         <newTemplateValue>
>>>        <randonFloat min="1" max="6"/>
>>>       <required/>
>>>     </newTemplateValue
>>>         and I'd like to exchange the content of first element for 
>>> second one, so
>>>     that first element look like
>>>         <setTemplateValue id="1" >
>>>        <randonFloat min="1" max="6"/>
>>>       <required/>
>>>     </setTemplateValue>
>>>             Thanks so much
>>>         Jakub
>>>                             
>>> ---------------------------------------------------------------------
>>>     To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
>>>     For additional commands, e-mail: user-help@xmlbeans.apache.org
>>>        
>>>  
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
>> For additional commands, e-mail: user-help@xmlbeans.apache.org
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org