function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SurjenduSurjendu 

XmlStreamReader parsing fails for escaped &

The following code gives me an XML parsing error. If the value of the node contains & then the parsing fails.

reader.getText() returns the value of the node till & which is wrong. It should
return everything as & is escaped with &

Code:
public class myClass 
{

static testMethod void myTest() 
{
String xmlString = '<—xml version="1.0" encoding="ISO-8859-1"–><project-entities><response><users><user><user-id>surone@surl &amp; t.com</user-id><code>Success</code><desc>User Created</desc></user></users></response></project-entities>';
XmlStreamReader xsr = new XmlStreamReader(xmlString);
parseResponse(xsr);
}

static void parseResponse(XmlStreamReader reader)
 {

  while(reader.hasNext()) 
  {
   
   if(reader.getEventType() == XmlTag.START_ELEMENT)
   {
    if(reader.getLocalName() == 'user-id')
    {
     System.debug('I am here');
     reader.next(); 
     if(reader.getEventType() == XmlTag.CHARACTERS)
     {
      System.debug(reader.getText());
     }
     reader.nextTag();
     reader.nextTag();
     
     if(reader.getEventType() == XmlTag.START_ELEMENT)
     {
      if(reader.getLocalName() == 'code')
      {
       reader.next();
       if(reader.getEventType() == XmlTag.CHARACTERS)
       {
        System.debug(reader.getText());
       }
      }
     }
     reader.nextTag();
     reader.nextTag(); 
     if(reader.getEventType() == XmlTag.START_ELEMENT)
     {
      if(reader.getLocalName() == 'desc')
      {
       reader.next();
       if(reader.getEventType() == XmlTag.CHARACTERS)
       {
        System.debug(reader.getText());
       }
      }
     }    
    }
   }
  reader.next();
  }
 }
}

 
werewolfwerewolf
You may want to log this as a case with Salesforce support.
MJozefiakMJozefiak
Hi,

same problem here.

I need to parse some XML with company names containing an ampersand '&'. XmlStreamReaders getText only returns the string before the '&'.

Did you try to contact SalesForce Support or does anyone got a solution to solve this problem?

Greets

Mark
SuperfellSuperfell
The parser can return multiple text nodes, you need to either collect them all into a single string yourself, or turn on the coalescing feature, e.g. rdr.setCoalescing(true); see the XmlStreamReader docs http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_classes_XmlStream_reader.htm
MJozefiakMJozefiak
Thank you for your answer.
Unfortunately your hint didn't solve my problem.
It seems the problem is that the XMLStreamReader always assumes that an entity definition will follow after an '&'.
If the xml-string contains the '&' as '&amp;' the StreamReader converts it back to '&' when loading the xml-string.

Any solution?

Greets

Mark
MJozefiakMJozefiak
ok, the combination of '&amp;' and setCoalescing(true) works.

Thank you for help.

Greets

Mark

SuperfellSuperfell
Yes, it does, as required by the XML specification, & on its own is not legal xml.