You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Pa...@b-source.ch on 2007/04/06 17:41:22 UTC
XMLBeans and XmlCursor
Hi all,
I've just started to use XMLBeans and I had some troubles to use
XmlCursor to navigate through a document, update it and get back the
updated version.
This is my xml document:
<?xml version="1.0" encoding="UTF-8"?>
<eda:quoteEvent xmlns:eda="http://www.bsource.ch/EDA">
<eda:security>CS GROUP N</eda:security>
<eda:date>09-03-2007</eda:date>
<eda:time/>
<eda:national-nr>1213853</eda:national-nr>
<eda:change>-0.2</eda:change>
<eda:volume>5821192</eda:volume>
<eda:currency transcoder="CURRENCY">CHF</eda:currency>
<eda:prev-close>87.9</eda:prev-close>
<eda:open>87.75</eda:open>
<eda:last>87.7</eda:last>
</eda:quoteEvent>
I would like to retrieve all nodes with transcoder attribute (I've used
the //@transcoder/.. expression), call another component (for
transcodification) and get back the below new one:
<?xml version="1.0" encoding="UTF-8"?>
<eda:quoteEvent xmlns:eda="http://www.bsource.ch/EDA">
<eda:security>CS GROUP N</eda:security>
<eda:date>09-03-2007</eda:date>
<eda:time/>
<eda:national-nr>1213853</eda:national-nr>
<eda:change>-0.2</eda:change>
<eda:volume>5821192</eda:volume>
<eda:currency transcoder="CURRENCY">001</eda:currency>
<eda:prev-close>87.9</eda:prev-close>
<eda:open>87.75</eda:open>
<eda:last>87.7</eda:last>
</eda:quoteEvent>
The code is:
XmlObject xobj = XmlObject.Factory.parse(new
ByteArrayInputStream(bodyEvent.getBytes()));
XmlObject response = EDATool.transcodification (xobj, "SMI-EDA");
where
public static XmlObject transcodification (XmlObject bodyEvent, String
encoder) throws Exception
{
if ((bodyEvent == null) || (encoder == null))
throw new Exception("unexpected null object");
long start = System.currentTimeMillis();
XmlObject[] elems = bodyEvent.execQuery("//@transcoder/..");
System.out.println ("execQuery elapsed time msec: " +
(System.currentTimeMillis() - start));
for (int i = 0; i < elems.length; i++)
{
XmlCursor cursor = elems[i].newCursor();
TokenType token = null;
String value = cursor.getTextValue();
String table = null;
while (cursor.hasNextToken())
{
token = cursor.toNextToken();
if (token.isAttr() &&
"transcoder".equals(cursor.getName().toString()))
{
table = cursor.getTextValue();
break;
}
}
if (table != null)
{
cursor.pop();
value = transcode(table, encoder, value);
cursor.setTextValue(value);
}
cursor.dispose();
}
return bodyEvent;
}
My doubts:
1) I noticed that the time for executing execQuery is not neglectable
(msec: 3688 for a document of only 471 bytes).
What is wrong in my code ?
2) after disposing the cursor the document is not updated, how could I
write back my changes to the document ?
Regards
Patrizio
IMPORTANT:
This e-mail transmission is intended for the named
addressee(s)only.
Its contents are private, confidential and protected
from disclosure and should not be read, copied or
disclosed by any other person.
If you are not the intended recipient, we kindly ask
you to notify the sender immediately by telephone
(+41 (0)58 806 50 00), to redirect the message to the
account "info@b-source.ch" and to delete this e-mail.
E-mail transmissions may be intercepted, altered or
read by unauthorized persons and may contain viruses.
Therefore, it is recommended that you use regular mail
or courier services for any information intended to be
confidential. However, by sending us messages through
e-mail, you authorize and instruct us to correspond by
e-mail in the relevant matter.
Thank you.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org
RE: XMLBeans and XmlCursor
Posted by Cezar Andrei <ce...@bea.com>.
Hi Patrizio,
execQuery() methods return new documents (there are queries that
construct new documents), so the modifications on results will not
affect the original.
In your case, you should be better off using XMLCursor.selectPath().
This method will set a selection on the original document, than use
hasNext/toNextSelection methods to navigate.
Avoid creation of many cursors, if you want to improve performance, in
your case one is probably enough, moving a cursor anywhere in a document
is much faster than managing many cursors. After you're done with it
don't forget to call dispose().
Cezar
> -----Original Message-----
> From: Patrizio.Ferlito@b-source.ch
[mailto:Patrizio.Ferlito@b-source.ch]
> Sent: Friday, April 06, 2007 10:41 AM
> To: user@xmlbeans.apache.org
> Subject: XMLBeans and XmlCursor
>
>
> Hi all,
>
> I've just started to use XMLBeans and I had some troubles to use
> XmlCursor to navigate through a document, update it and get back the
> updated version.
>
> This is my xml document:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <eda:quoteEvent xmlns:eda="http://www.bsource.ch/EDA">
> <eda:security>CS GROUP N</eda:security>
> <eda:date>09-03-2007</eda:date>
> <eda:time/>
> <eda:national-nr>1213853</eda:national-nr>
> <eda:change>-0.2</eda:change>
> <eda:volume>5821192</eda:volume>
> <eda:currency transcoder="CURRENCY">CHF</eda:currency>
> <eda:prev-close>87.9</eda:prev-close>
> <eda:open>87.75</eda:open>
> <eda:last>87.7</eda:last>
> </eda:quoteEvent>
>
> I would like to retrieve all nodes with transcoder attribute (I've
used
> the //@transcoder/.. expression), call another component (for
> transcodification) and get back the below new one:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <eda:quoteEvent xmlns:eda="http://www.bsource.ch/EDA">
> <eda:security>CS GROUP N</eda:security>
> <eda:date>09-03-2007</eda:date>
> <eda:time/>
> <eda:national-nr>1213853</eda:national-nr>
> <eda:change>-0.2</eda:change>
> <eda:volume>5821192</eda:volume>
> <eda:currency transcoder="CURRENCY">001</eda:currency>
> <eda:prev-close>87.9</eda:prev-close>
> <eda:open>87.75</eda:open>
> <eda:last>87.7</eda:last>
> </eda:quoteEvent>
>
> The code is:
>
> XmlObject xobj = XmlObject.Factory.parse(new
> ByteArrayInputStream(bodyEvent.getBytes()));
> XmlObject response = EDATool.transcodification (xobj, "SMI-EDA");
>
> where
>
> public static XmlObject transcodification (XmlObject bodyEvent, String
> encoder) throws Exception
> {
> if ((bodyEvent == null) || (encoder == null))
> throw new Exception("unexpected null object");
>
> long start = System.currentTimeMillis();
> XmlObject[] elems = bodyEvent.execQuery("//@transcoder/..");
> System.out.println ("execQuery elapsed time msec: " +
> (System.currentTimeMillis() - start));
>
> for (int i = 0; i < elems.length; i++)
> {
> XmlCursor cursor = elems[i].newCursor();
> TokenType token = null;
>
> String value = cursor.getTextValue();
> String table = null;
>
> while (cursor.hasNextToken())
> {
> token = cursor.toNextToken();
> if (token.isAttr() &&
> "transcoder".equals(cursor.getName().toString()))
> {
> table = cursor.getTextValue();
> break;
> }
> }
>
> if (table != null)
> {
> cursor.pop();
> value = transcode(table, encoder, value);
> cursor.setTextValue(value);
> }
> cursor.dispose();
> }
> return bodyEvent;
> }
>
> My doubts:
>
> 1) I noticed that the time for executing execQuery is not neglectable
> (msec: 3688 for a document of only 471 bytes).
> What is wrong in my code ?
> 2) after disposing the cursor the document is not updated, how could I
> write back my changes to the document ?
>
> Regards
> Patrizio
>
Notice: This email message, together with any attachments, may contain information of BEA Systems, Inc., its subsidiaries and affiliated entities, that may be confidential, proprietary, copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named in this message. If you are not the intended recipient, and have received this message in error, please immediately return this by email and then delete it.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org