This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes trouble parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "trouble parsing "&" from xml file" Watch "trouble parsing "&" from xml file" New topic
Author

trouble parsing "&" from xml file

swapna Kadali
Greenhorn

Joined: Oct 06, 2003
Posts: 22
Hi,
I have data which has "&" operator in my xml file. When I was trying to populate data from xml file into a dropdownlistbox,
I have an error : "Error occured while parsing Entity Name from xml file"
Did anybody had this problem, If so how did you solve that?
Ofcourse I am doing this in ASP.NET web application, but I think parsing xml file is same in any language.
Thanks,
Swapna
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Could you post the part of your XML where the ampersand character is found?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
When you are storing the information it is best to run a piece of code that changes " and & to the escaped state. That is what i do and I program in .NET, but it is the same all around. The error comes when the complier sees the & and expects to do something else with it.
Eric
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Or you could just make the XML file wellformed.
swapna Kadali
Greenhorn

Joined: Oct 06, 2003
Posts: 22
Hi Lasee and Eric,
Thanks to both of you for the reply.
Eric, Yes you are right, I should handle that error in my code.
Lasse, here is my xml file, can u see "&"(MN&O)in the last name element..?
right there..........
<?xml version="1.0"?>
<root>
<xsd:schema id="OrganizationType" targetNamespace="" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="OrganizationType">
<xsd:complexType>
<xsd:all>
<xsd:element name="Name" minOccurs="0" type="xsd:string" />
<xsd:element name="Abbreviation" minOccurs="0" type="xsd:string" />
</xsd:all>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<OrganizationTypes>
<OrganizationType>
<Name>ABC</Name>
<Abbreviation>PT</Abbreviation>
</OrganizationType>
<OrganizationType>
<Name>DEF</Name>
<Abbreviation>CON</Abbreviation>
</OrganizationType>
<OrganizationType>
<Name>GHI</Name>
<Abbreviation>PT</Abbreviation>
</OrganizationType>
<OrganizationType>
<Name>JKL</Name>
<Abbreviation>PT</Abbreviation>
</OrganizationType>
<OrganizationType>
<Name>MN&O</Name>
<Abbreviation>PT</Abbreviation>
</OrganizationType>
</OrganizationTypes>
</root>
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Yep. The XML is not valid. Your choices are basically A) fixing the XML by encoding "&" as "&amp;", or B) running the XML through a custom "encoder" which replaces occurrences of "&" with "&amp;".
swapna Kadali
Greenhorn

Joined: Oct 06, 2003
Posts: 22
Thanks !
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: trouble parsing "&" from xml file