在Salesforce中处理Xml的生成与解析

1): Generate Xml

    private String ConvertAccountToXmlInfo(Account acc){

        Dom.Document doc = new Dom.Document();
Dom.Xmlnode rootNode = doc.createRootElement('Accounts', null, null); Dom.Xmlnode accountNode = rootNode.addChildElement('Account', null, null);
accountNode.addChildElement('AccountName', null, null).addTextNode(acc.Name);
accountNode.addChildElement('AccountNumber', null, null).addTextNode(acc.AccountNumber);
accountNode.addChildElement('ABN', null, null).addTextNode(acc.ABN__c);
accountNode.addChildElement('AutoEmailInvoice', null, null).addTextNode(String.valueOf(acc.Auto_Email_Invoice__c));
accountNode.addChildElement('CreditType', null, null).addTextNode(acc.Credit_Type__c);
accountNode.addChildElement('GPID', null, null).addTextNode(acc.GP_ID__c);
accountNode.addChildElement('PPSR', null, null).addTextNode(acc.PPSR__c);
accountNode.addChildElement('VIPNumber', null, null).addTextNode(acc.VIP_Number__c);
accountNode.addChildElement('AccountContact', null, null).addTextNode(acc.Account_Contact__c);
accountNode.addChildElement('Phone', null, null).addTextNode(acc.Phone);
accountNode.addChildElement('Fax', null, null).addTextNode(acc.Fax);
accountNode.addChildElement('Email', null, null).addTextNode(acc.Email__c); Dom.Xmlnode baNode = accountNode.addChildElement('BillingAddress', null, null);
baNode.addChildElement('Address', null, null).addTextNode(acc.BillingStreet);
baNode.addChildElement('Suburb', null, null).addTextNode(acc.BillingCity);
baNode.addChildElement('Postcode', null, null).addTextNode(acc.BillingPostalCode);
baNode.addChildElement('State', null, null).addTextNode(acc.BillingState);
baNode.addChildElement('Country', null, null).addTextNode(acc.BillingCountry); Dom.Xmlnode saNode = accountNode.addChildElement('ShippingAddress', null, null);
saNode.addChildElement('Address', null, null).addTextNode(acc.ShippingStreet);
saNode.addChildElement('Suburb', null, null).addTextNode(acc.ShippingCity);
saNode.addChildElement('Postcode', null, null).addTextNode(acc.ShippingPostalCode);
saNode.addChildElement('State', null, null).addTextNode(acc.ShippingState);
saNode.addChildElement('Country', null, null).addTextNode(acc.ShippingCountry); return doc.toXmlString();
}

2): Parse Xml

private static Account GenerateAccountFromXmlInfo(String accountXmlInfo){
Account currentAcc = new Account();
system.debug('00001 ---- ' + accountXmlInfo);
Dom.Document doc = new Dom.Document();
doc.load(accountXmlInfo);
Dom.Xmlnode rootNode = doc.getRootElement();
Dom.Xmlnode accountNode = rootNode.getChildElement('Account', null);
currentAcc.Name = accountNode.getChildElement('AccountName', null).getText();
currentAcc.AccountNumber = accountNode.getChildElement('AccountNumber', null).getText();
currentAcc.ABN__c = accountNode.getChildElement('ABN', null).getText();
currentAcc.Auto_Email_Invoice__c = Boolean.valueOf(accountNode.getChildElement('AutoEmailInvoice', null).getText());
currentAcc.Credit_Type__c = accountNode.getChildElement('CreditType', null).getText();
currentAcc.GP_ID__c = accountNode.getChildElement('GPID', null).getText();
currentAcc.PPSR__c = accountNode.getChildElement('PPSR', null).getText();
currentAcc.VIP_Number__c = accountNode.getChildElement('VIPNumber', null).getText();
currentAcc.Account_Contact__c = accountNode.getChildElement('AccountContact', null).getText();
currentAcc.Phone = accountNode.getChildElement('Phone', null).getText();
currentAcc.Fax = accountNode.getChildElement('Fax', null).getText();
currentAcc.Email__c = accountNode.getChildElement('Email', null).getText(); Dom.Xmlnode baNode = accountNode.getChildElement('BillingAddress', null);
currentAcc.BillingStreet = baNode.getChildElement('Address', null).getText();
currentAcc.BillingCity = baNode.getChildElement('Suburb', null).getText();
currentAcc.BillingPostalCode = baNode.getChildElement('Postcode', null).getText();
currentAcc.BillingState = baNode.getChildElement('State', null).getText();
currentAcc.BillingCountry = baNode.getChildElement('Country', null).getText(); Dom.Xmlnode saNode = accountNode.getChildElement('ShippingAddress', null);
currentAcc.ShippingStreet = saNode.getChildElement('Address', null).getText();
currentAcc.ShippingCity = saNode.getChildElement('Suburb', null).getText();
currentAcc.ShippingPostalCode = saNode.getChildElement('Postcode', null).getText();
currentAcc.ShippingState = saNode.getChildElement('State', null).getText();
currentAcc.ShippingCountry = saNode.getChildElement('Country', null).getText(); return currentAcc;
}

Click following links to learn more technology:

http://it.toolbox.com/blogs/anything-worth-doing/a-better-way-to-generate-xml-on-salesforce-using-visualforce-55433

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_xml_dom.htm

在Salesforce中处理Xml的生成与解析的更多相关文章

  1. 6.2 dubbo在spring中自定义xml标签源码解析

    在6.1 如何在spring中自定义xml标签中我们看到了在spring中自定义xml标签的方式.dubbo也是这样来实现的. 一 META_INF/dubbo.xsd 比较长,只列出<dubb ...

  2. 在请求中使用XML Publisher生成文件报错

    在页面上使用按钮生成该文件不报错,但是使用请求就报错. 错误内容如下 Error : No corresponding LOB data found :SELECT L.FILE_DATA FILE_ ...

  3. xml的生成与解析_老师笔记

    使用序列化器生成一个xml文件 //1,初始化一个xml文件的序列化器 XmlSerializer serializer = Xml.newSerializer(); //2.初始化序列器参数 Fil ...

  4. Java读取数据库中的xml格式内容,解析后修改属性节点内容并写回数据库

    直接附代码: 1.测试用的xml内容 <mxGraphModel> <root> <mxCell id="-1" /> <mxCell i ...

  5. android xml的生成与解析

    Main java package com.itheima.xml; import android.app.Activity; import android.content.Context; impo ...

  6. Qt之JSON生成与解析

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - December ...

  7. ZXing 生成、解析二维码图片的小示例

    概述 ZXing 是一个开源 Java 类库用于解析多种格式的 1D/2D 条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME ...

  8. 【转载】Qt之JSON生成与解析

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - December ...

  9. Android中XML文件的序列化生成与解析

    xml文件是非常常用的,在android中json和xml是非常常用的两种封装数据的形式,从服务器中获取数据也经常是这两种形式的,所以学会生成和解析xml和json是非常有用的,json相对来说是比较 ...

随机推荐

  1. Binary Tree Upside Down

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  2. 19. javacript高级程序设计-E4X

    1. E4X E4X是对ECMAScript的一个扩展, l 与DOM不同,E4X只用一个类型节点来表示XML中的各个节点 l XML对象中封装了对所有节点都有用的数据和行为.为了表示多个节点的集合, ...

  3. FFmpeg-20160413-snapshot-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 F ...

  4. js 一搬问题汇总

    --有时无法进行js调试,在浏览器中设置启用脚本调试就可以了

  5. 允许FTP用户登录并禁止Shell登录的方法

    最近安装了vsftpd做FTP服务,发现系统用户的登录shell设置为/sbin/nologin,就无法使用FTP服务.网上资料说,vsftpd会为每个FTP登录用户去在/etc/shells中检查对 ...

  6. Effective C++ -----条款33:避免遮掩继承而来的名称

    derived classes内的名称会遮掩base classes内的名称.在public继承下从来没有人希望如此. 为了让被遮掩的名称再见天日,可使用using声明式或转交函数(forwardin ...

  7. ajax加载模块实时刷新的原理

    var loadMenu = function(data) {        var trs = template.render('menu-list-temp', {'list': data});  ...

  8. javascript void运算符

    参考链接:http://www.cnblogs.com/ziyunfei/archive/2012/09/23/2698607.html语法: void expr 作用:计算表达式expr,并返回un ...

  9. Redis 数据持久化(一)

    Redis的模块化开发设计的还是相当不错的,在Bio.h和Bio.c文件中定义了一个多线程的文件任务处理模块,在添加和处理任务的时候使用互斥锁和条件变量进行的同步,而且本身也支持多线程,这个模块的支持 ...

  10. Git命令之上传与同步

    操作步骤,可参考:http://blog.csdn.net/chenyufeng1991/article/details/47299461. 1.在本地仓库中,即项目目录下创建文件,如: 2.查看当前 ...