Question

I'm creating a DOMDocument.

The question is simple, I have an XML that has one node name <productName>.

If I want create an xml and the value contains an especial chars like çøðé&, I can not create the xml because the application throws an exception

"unterminated entity reference çøðé"

But I know that the problem is the char "&", what I should do, encode the char to &amp; and decode it if I want to paint the value or I should set the value inside a <! [CDATA []]

Thank you.

Was it helpful?

Solution

&amp; would be the proper way to do it and then you would have to manually edit it when you import the xml document or automatically decode it in your code. The problem with CData is that this will not be parsed if you are using an XML parser library (which I would strongly suggest using especially if you have large files).

Source: I worked at a publishing company. They would receive XML files with improper characters and I would have to go through the file and remove the invalid characters in the XML and replace them with other characters. Occasionally, this was a long and tedious task unfortunately. You have to make sure that the people sending you the XML files are not including invalid characters and if they are, you may have to have the unfortunate task of going through the files and removing them yourself. You could do this by writing a java program to remove the characters for you but the problem is, it may not catch all the invalid characters. If you catch exceptions, most of the time you should be able to look at the exceptions and see where the invalid characters are with the parser you are using and it may include the byte code for that invalid character. I suggest you use TextPad for finding invalid characters as you can search by bytes and you can find "hidden" characters that you would otherwise not see in another text editor.

You may also have cases where you have very large files that are too large to open. In this case, you will have to split the files in order to view them (if you are creating your own XML structure, you will most likely need to create your own XML splitter).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top