I am trying to write this below xml from Contact object.Problem is compostion of the xml with nested childs is really complex.Can you help understand to demistfy this problem.
"<request version='1.0'> <header> <office>SDC</office> <user>Userid</user> <password>*****</password> </header> <insert> <Contact> <LastName>Test1</LastName> <FirstName>Test</FirstName> <Title>Dr.</Title> <TaxID>123456789</TaxID> <Person> <Dob>1959-03-30</Dob> <Gender>1</Gender> <Occupation>Baker</Occupation> <JobTitle>Dough Boy</JobTitle> <Marital>1</Marital> <Salary>15000</Salary> </Person> </Contact> </insert> </request>"
Indented version:
<request version="1.0">
<header>
<office>SDC</office>
<user>Userid</user>
<password>*****</password>
</header>
<insert>
<Contact>
<LastName>Test1</LastName>
<FirstName>Test</FirstName>
<Title>Dr.</Title>
<TaxID>123456789</TaxID>
<Person>
<Dob>1959-03-30</Dob>
<Gender>1</Gender>
<Occupation>Baker</Occupation>
<JobTitle>Dough Boy</JobTitle>
<Marital>1</Marital>
<Salary>15000</Salary>
</Person>
</Contact>
</insert>
</request>
Here is the code:
public class RestDemoController {
public String policyid { get; set; }
public String CName { get; set; }
public String LName { get; set; }
public String FName { get; set; }
public String address { get; set; }
public PageReference submit() {
sendRequest();
return null;
}
public void sendRequest(){
XmlStreamWriter w = new XmlStreamWriter();
w.writeStartDocument('utf-8','1.0');
w.writeStartElement('null','request', '1.0');
w.writeStartElement(null, 'Header', null); //this will start with <Header> in XML
w.writeStartElement(null, 'office', '****');
w.writeEndElement();
w.writeStartElement(null, 'user','*****');
w.writeEndElement();
w.writeStartElement(null, 'password','*****');
w.writeEndElement();
// w.writeCharacters(a.AccountExternalID__c);
w.writeEndElement();
w.writeEndElement();
String xmlOutput = w.getXmlString();
w.close();
System.debug('XML Output**********'+xmlOutput);
String endpoint='www.google.com';
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint(endpoint);
req.setHeader('Content-Type', 'text/xml');
req.setBody(xmlOutput);
Http http = new Http();
HttpResponse res = http.send(req);
}
}
Attribution to: Kumara Ravuri
Possible Suggestion/Solution #1
Your question appears to be how to create the XML. The simplest Apex API for this is the DOM API. You create the nesting yourself by creating a root document and adding child nodes. When you have built the tree in this form you can invoke the toXmlString()
method to produce the XML string.
See the "XmlNode Example" in the above link and/or Google for many examples posted in blogs etc. Generally outputting XML is easier than parsing XML.
PS
The sample code added to the question is using XmlStreamWriter
. When not using namespaces lots of nulls need passing (null not 'null'); to add content inside elements the writeCharacters
method must be used. This API requires more lines of code than the DOM approach and is harder to get right: it is only necessary to resort to this API for very large documents. But having started with it I guess you may want to continue - see e.g. Creating an XML with Xmlstreamwriter for a working example of how to use the API.
Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32572