Changes for page XML-RPC Java Examples

Last modified by Thomas Mortagne on 2023/10/10 14:31

From version 10.1
edited by mawoki
on 2010/08/09 12:04
Change comment: There is no comment for this version
To version 11.1
edited by mawoki
on 2010/08/09 12:14
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -489,8 +489,11 @@
489 489  
490 490  = Page: List And Add Tags =
491 491  
492 +Pages are tagged via objects of type XWiki.TagClass. Thus you can switch to object edit mode on any page and edit tags this way. This is also the way, done by XMLRPC-API.
493 +
492 492  {{code language="java"}}
493 493  import java.net.MalformedURLException;
496 +import java.util.ArrayList;
494 494  import java.util.List;
495 495  
496 496  import org.apache.xmlrpc.XmlRpcException;
... ... @@ -507,15 +507,16 @@
507 507   String pass = "admin";
508 508  
509 509   XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
513 + XWikiObjectSummary xosum = new XWikiObjectSummary();
514 + xosum.setClassName("XWiki.TagClass");
515 + // your page id
516 + final String pageId = "Sandbox.Wdscfaq";
517 + xosum.setPageId(pageId);
510 510   try {
511 511   // Perform Login & Authentication
512 512   rpc.login(user, pass);
513 513  
514 - XWikiObjectSummary xosum = new XWikiObjectSummary();
515 - xosum.setClassName("XWiki.TagClass");
516 - // your page id
517 - xosum.setPageId("Sandbox.WebHome");
518 - // retrieve current page object informations
522 + // retrieve current page object informations
519 519   XWikiObject xwo = rpc.getObject(xosum);
520 520   // The tags are one String, separated by | or , or 'SPACE'
521 521   List<String> tags = (List<String>) xwo.getProperty("tags");
... ... @@ -523,11 +523,26 @@
523 523   for (String tag : tags) {
524 524   System.out.println(tag);
525 525   }
530 + } catch (Exception e) {
531 + // may be it's
532 + // "Failed to invoke method getObject in class
533 + // com.xpn.xwiki.xmlrpc.XWikiXmlRpcApiImpl: [Unable to find
534 + // object XWiki.TagClass[0] on page 'Sandbox.Wdscfaq']"
535 + // This means: no tags already given.
536 + System.out.println(e.getMessage());
537 + }
538 + // try to place a new tag.
539 + try {
526 526   System.out.println("--- NEW TAG -----------------------");
527 527   // add a new random tag
528 528   String tag = "NOW"+System.currentTimeMillis();
529 529   System.out.println(tag);
544 + List<String> tags = new ArrayList<String>();
530 530   tags.add(tag);
546 + XWikiObject xwo = new XWikiObject();
547 + xwo.setProperty("tags", tags);
548 + xwo.setPageId(pageId);
549 + xwo.setClassName("XWiki.TagClass");
531 531   rpc.storeObject(xwo);
532 532   } catch (XmlRpcException e) {
533 533   System.out.println(e.getMessage());

Get Connected