File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes parsing CDATA error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "parsing CDATA error" Watch "parsing CDATA error" New topic
Author

parsing CDATA error

jeff zhang
Greenhorn

Joined: Sep 25, 2000
Posts: 9
Hi All,
I tried to parse the following xml data with sun's dom validating parwer. But I got the error like:
Element "orders" does not allow text.
The xml data is:
<?xml version ="1.0"?><!DOCTYPE orders [
<!ELEMENT orders (order+)>
<!ELEMENT order EMPTY>
<!ATTLIST order
customerName CDATA #REQUIRED
customerAddr CDATA #IMPLIED >
]>
<orders><![CDATA[<[order customerName= " mark & john " customerAddr= "route 15">
</order>]]></orders>
I have no ideas what's wrong there.
Can anybody help?
Thanks in advance
Jeff
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
...The ampersand character (&) may only appear in it's literal form when used as a markup character... If it is needed anywhere else it must be escaped...
This is a quote from A book on XML your data
" mark & john "
must be " mark & amp john " or clearer yet
//please note I had to put a space between the & and amp for it to show it's not really sopposed to be a space there
" mark and john "
This is beause the & is used as the escape character and for defining entity references. There are several predefined entity references and they are:
& - & amp
' - &apos
> - & gt
< - & lt
" - & quot
//please note I had to put a space between the & and the word that follows for it to show there's not really sopposed to be a space there

I hope this helps

[This message has been edited by Carl Trusiak (edited October 03, 2000).]
[This message has been edited by Carl Trusiak (edited October 03, 2000).]
[This message has been edited by Carl Trusiak (edited October 03, 2000).]


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
If the above doesn't seem to make sense, please click on the edit button (the "paper and pen" icon) to view the text as entered. It seems impossible to prevent this bulletin board from processing these character sequences.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Jayakumar Thirumalai
Greenhorn

Joined: Jul 08, 2000
Posts: 15
As suggested by Carl Trusiak, you may use either 'and' or '&' in the attributes.
Suggestion 1: (use 'and' instead of & )
<?xml version ="1.0"?>
<!DOCTYPE orders [
<!ELEMENT orders (order+)>
<!ELEMENT order EMPTY>
<!ATTLIST order
customerName CDATA #REQUIRED
customerAddr CDATA #IMPLIED >
]>
<orders>
<order customerName= " mark and john " customerAddr= "route15">
</order>
</orders>
Suggestion II: ( use '& amp;' instead of & )
<?xml version ="1.0"?>
<!DOCTYPE orders [
<!ELEMENT orders (order+)>
<!ELEMENT order EMPTY>
<!ATTLIST order
customerName CDATA #REQUIRED
customerAddr CDATA #IMPLIED >
]>
<orders>
<order customerName= " mark & amp; john " customerAddr= "route15">
</order>
</orders>
I tried to use CDATA section to give value to an element in the following way.
<?xml version ="1.0"?>
<!DOCTYPE orders [
<!ELEMENT orders (order+)>
<!ELEMENT order (#PCDATA)>
<!ATTLIST order
customerName CDATA #REQUIRED
customerAddr CDATA #IMPLIED >
]>
<orders>
<order
customerName="mark & amp; john"
customerAddr= "route15">
<![CDATA[
: & order string & : < >
]]>
</order>
</orders>
CDATA section can not be used to assign value to attributes. The following text is from XML Tutorial from Sun.
"One other area to watch for is attributes. The text of an attribute value could also contain angle brackets and semicolons that need to be replaced by entity references. (Attribute text can never be in a CDATA section, though, so there is never any question about doing that substitution.)"
You have used <![CDATA[ before order tag. is that right way to do it?
All the above xml documents are parsed without any error.
Jay

[This message has been edited by Jayakumar Thirumalai (edited October 03, 2000).]


Jayakumar Thirumalai
Amala Amperayani
Greenhorn

Joined: Sep 27, 2000
Posts: 6
The '&' character is the one which is giving trouble.
I guess in XML, unlike HTML entity references must end with a semicolon.
Try using a semicolon after the entity reference you are using. Post your result so that I can correct if I am wrong.
[This message has been edited by Amala Amperayani (edited October 03, 2000).]
jeff zhang
Greenhorn

Joined: Sep 25, 2000
Posts: 9
Thanks for responses. All suggestions can work around '&' problem. Especially, Jay's CDATA solution is the one I am looking for since I may have many special chars like '&' or '<' needed to handle. After I posted the question, I realized what's wrong in my previous xml stream. I should not put <![CDATA[ after <orders> since it will violate the rule defined in dtd file(get error like Element "orders" does not allow text.).
Thanks again for all your help.
Jeff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parsing CDATA error
 
Similar Threads
Encoding Special case characters
validating xml problem
#CDATA and #PCDATA
CDATA and PCDATA
xml parsing with dom