aspose file tools*
The moose likes Product and Other Certifications and the fly likes How to declare an attribute of xml element as CDATA type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Product and Other Certifications
Bookmark "How to declare an attribute of xml element as CDATA type" Watch "How to declare an attribute of xml element as CDATA type" New topic
Author

How to declare an attribute of xml element as CDATA type

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
hello,

see my code:
<xsd:attribute name="password" type="xsd:CDATA"/>
the jdeveloper complain the "reference xsd:CDATA not found", could you help? how to declare a CDATA attribute in the xml schema?
Xin Zoo
Greenhorn

Joined: Oct 03, 2009
Posts: 9
There is no datatype called xsd:CDATA. A CDATA section is a means to escape the String from parsing.

If you need more info about the available datatypes look here: XML Schema Built-In Datatypes

You should declare the field as xsd:string. If you are using a reasonable xml serializer it will automatically escape the characters if there is a need to do so. If you serialize your xml document yourself you just have to embed the password in a CDATA section.

Anyway you have to make sure the password does not contain ]]>.
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
Thanks for helps, but i could not embed "<!CDATA[[.....]]" in the attribute, the "><" in attribute is not allowed, i think it should be legal to use the "<!CDATA[[.....]]" in the element value. is that correct? can you use it in the attribute? let me know
>
Xin Zoo
Greenhorn

Joined: Oct 03, 2009
Posts: 9
Sorry, i overlooked this. But still i think you should declare the attribute as xsd:string and let the xml serializer do the escaping. I think the serializer will escape a " and a & with a character reference. I believe these are the only harmful characters in an attribute. Try it and tell us if it works. Again, if you do the serializing yourself then replace any occurence of " by &quot; a & by &amp; and revert the operation when you deserialize the document. And please report if it worked out.
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
In fact, i am designing a xml configuration file for a java application, which handle the database operation, so i need to put the password somewhere in the config file. to xml structure point of view, it is better to define the password as an attribute, but it is possible the "<" is included in the password string, and i could not find a good way to handle this case, because the "<" is not allowed in the attribute, i am not sure if the serializer you mentioned and handle it well.
so if i could not find the good way, i have to put the password as an element in the file, then wrap it by "<![CDATA[....]>", that maybe works, but the file structure looks awful.
Xin Zoo
Greenhorn

Joined: Oct 03, 2009
Posts: 9
Can you not escape the character? Try to escape it like this: &lt;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to declare an attribute of xml element as CDATA type
 
Similar Threads
mock exam question
Converting an object into an XML
PCDATA and CDATA
CDATA and jdom
what is CDATA and how it is used