File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to declare an attribute of xml element as CDATA type

 
zb cong
Ranch Hand
Posts: 416
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 416
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 416
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you not escape the character? Try to escape it like this: &lt;
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic