Changes for page XML-RPC Java Examples
Last modified by Thomas Mortagne on 2023/10/10 14:31
To version 2.1
edited by dilipkumarj
on 2010/01/30 04:42
on 2010/01/30 04:42
Change comment:
Added code to update content of a Page
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Tags
-
... ... @@ -1,0 +1,1 @@ 1 +xmlrpc|java|example|page|space|authentication|create|delete - Content
-
... ... @@ -408,3 +408,68 @@ 408 408 } 409 409 } 410 410 {{/code}} 411 + 412 + 413 += Page: Update A Page = 414 + 415 +We saw code to search a Page, create a Page and get all it's revisions. Now, we move to updating a Page. In our example, we have used the Page called "Update Page" in the "demo code" Space. The basic intent is to get the content first from XWiki. Then add our updated content or "concatenate" new content with the existing content. Then, update the final content back to XWiki. Only the methods of the Page class & the rpc.getPage() & rpc.storePage() would be used here. 416 + 417 +{{warning}} 418 +It is strongly advised that if this is the first time you are using XWiki XMLRPC, go through the examples at the start of this article. Each example tries to build up on the previous one & you would find understanding the API easier. 419 +{{/warning}} 420 + 421 +{{code language="java"}} 422 + 423 +import java.net.MalformedURLException; 424 +import org.apache.xmlrpc.XmlRpcException; 425 +import org.codehaus.swizzle.confluence.Page; 426 +import org.xwiki.xmlrpc.XWikiXmlRpcClient; 427 + 428 + 429 +public class UpdatePage { 430 + 431 + public static void main(String[] args) throws MalformedURLException, XmlRpcException { 432 + 433 + //URL of the xwiki instance 434 + String url = "http://localhost:8080/xwiki/xmlrpc/confluence"; 435 + 436 + //Replace user & pass with desired xwiki username & password 437 + String user = "Admin"; 438 + String pass = "admin"; 439 + 440 + 441 + XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url); 442 + try { 443 + 444 + //Perform Login & Authentication 445 + rpc.login(user, pass); 446 + 447 + //Create a Page object to hold our Document information 448 + Page page = new Page(); 449 + //Fetch the required page. In our example, the page is in Space "demo code" 450 + //and the Page is "Update Page" 451 + page=rpc.getPage("demo code.Update Page"); 452 + //Fetch the content of the page & store it in a string for temporary storage 453 + //This is the present content of the Page 454 + String presentContent=page.getContent(); 455 + //Create a string that will hold the new content that is to be added to the Page 456 + String newContent="\\\\Some new content added"; 457 + //Set the content of the page as: present content + new content 458 + //However, this page is not yet stored to XWiki. It only resides in your application 459 + page.setContent(presentContent+newContent); 460 + //Finally, store the "updated" Page to XWiki 461 + rpc.storePage(page); 462 + 463 + //Just to make sure everything saved all right, fetch the content again for the Page 464 + System.out.println(page.getContent()); 465 + 466 + } catch (XmlRpcException e) { 467 + System.out.println("invalid username/password was specified or communication problem or "); 468 + System.out.println(e); 469 + } finally { 470 + rpc.logout(); 471 + } 472 + } 473 +} 474 + 475 +{{/code}}