Newsletters Subscriptions Forums Media Kit About Us Contact Search Home

mgo
OS/400 Edition
Volume 3, Number 72 -- October 24, 2003

Embedding Markup in XML


Hey, David:

How do you store data that may contain special characters like ampersands (&) in an XML document?

I built an RPG program that reads through a database file, then creates an XML file. One of the salespeople keyed in a customer name with an ampersand, which eventually made it to our order inquiry extraction program. We have a Web program that lets customers review their invoices. The program reads the customer order XML file and fails whenever a customer name contains an ampersand.

--Drew


The program failed because the parser that is processing your customer order XML file is expecting an ampersand to be followed by an entity reference. In addition to ampersands, you need to be careful about "less-than" (<) signs that may be embedded in your content.

There are two ways to address your problem. The first way is to escape any ampersand or less-than sign you find in the input and replace them with an entity reference of &amp; or &lt; respectively. You can use RPG's scan and replace built-in functions to accomplish this. See "SQL Replace Function, Take Two," which describes a replace procedure that you could adapt to your needs.

The second way you can prevent problems with embedded characters is to surround your content with CDATA sections.

Here is an example of a CDATA section:

<customer>
 <name><![CDATA[Tom & Jerry's Custom Rod Shop]]></name>
</customer>

Either of these techniques will solve your problem. The simpler of the two to implement is probably the CDATA section; however, the first option of escaping ampersands and less-than signs will likely result in smaller documents.

--David


Sponsored By
ADVANCED SYSTEMS CONCEPTS

E-Mail Report and File Distribution
for the iSeries & AS/400

- ESEND -

- Saves paper and shipping costs -
- Eliminates report distribution hassles -
- Improves information access -
- Archives important information -
- Saves YOUR time -

Get a Free Trial today!

Read More



THIS ISSUE
SPONSORED BY:

Advanced Systems Concepts
Profound Logic Software


BACK ISSUES

TABLE OF
CONTENTS

Embedding Markup in XML

Query/400 Determines the Day of Week

Reader Feedback and Insights: He Likes Common Table Expressions


Editors
Howard Arner
Joe Hertvik
Ted Holt
David Morris

Managing Editor
Shannon Pastore

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.