You need to sign in to do that
Don't have an account?
Anu-cnp
XML Parser values are not looping
Hi,
I want to read xml from a custom object. My code is:
trigger dmldata on CnPData__c (before insert,after insert) {
String dataxml;
for(CnP__CnPData__c cpdata:Trigger.new){
dataxml=cpdata.CnP__DataXML__c;
}
XmlStreamReader reader= new XmlStreamReader(dataxml);
Contact[] contacts = new Contact[0];
while(reader.hasNext()) {
if (reader.getEventType() == XmlTag.START_ELEMENT) {
System.debug('local nameee'+reader.getLocalName());
if ('Contact' == reader.getLocalName()) {
Contact con= parseContact(reader);
contacts.add(con);
System.debug('Contacts after parsinggg'+contacts);
}
}
reader.next();
}
Contact parseContact(XmlStreamReader reader1){
Contact contact = new Contact();
while(reader1.hasNext()) {
System.debug('event typpeeee'+reader1.getEventType());
if (reader1.getEventType() == XmlTag.END_ELEMENT){
break;
}
else if (reader1.getEventType() == XmlTag.CHARACTERS) {
System.debug('Contacts getTextttt'+reader1.getText());
contact.FirstName= reader1.getText();
contact.LastName= reader1.getText();
System.debug('Last Nameeeeee'+contact.LastName);
}
reader1.next();
}
return contact;
}
}
My XML is:
<?xml version="1.0" encoding="UTF-8"?>
<Contact>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Contact>
<Contact>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Contact>
I can able to get First name as John. But the loop is not going to 'Doe'.I want to pass that value into last name.
Can any one of you throw some help on this?
hi,
I modified the code to following:
But it is creating two contacts.
How can I solve this.?
Hi,
In this if block -
if (reader1.getEventType() == XmlTag.END_ELEMENT){
Hi,
Thanks for the Reply.
I already try "Continue" there. But when I use continue, it is showing the Too many script statements 200001 runtime error.