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

在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

原文地址:https://www.cnblogs.com/mingmingruyuedlut/p/3497646.html