From the last 2 days I am searching that how to parse XML with special chars like !@#$%^&*()':;", but I am not getting anything sufficient that how to implement it.. Can anyone pls suggest me something..how can I do this??
-
did you get the soultion to your problem? – Lalit Poptani Nov 16 '11 at 12:08
5 Answers
You can try something like this, that is giving the reference for that special character.
Character Reference
& - &
< - <
> - >
" - "
' - '
UPDATE:
I had already answered in your question here so have a look at the Answer.

- 1
- 1

- 67,150
- 23
- 161
- 242
-
I am using XML SAX parser and it is parsing all the chars except & and <...now I am confused that how to handle them? – Kanika Nov 15 '11 at 06:13
-
Please check my updated answer, and let me know if you get any problem further. – Lalit Poptani Nov 15 '11 at 14:17
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = documentBuilder.parse(inputSource);
document.getDocumentElement().normalize();
The normalize method can handle special characters and entities for you.
public abstract void normalize ()
Added in API level 1
Puts all Text nodes in the full depth of the sub-tree underneath this Node, including attribute nodes, into a "normal" form where only structure (e.g., elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, i.e., there are neither adjacent Text nodes nor empty Text nodes. This can be used to ensure that the DOM view of a document is the same as if it were saved and re-loaded, and is useful when operations (such as XPointer [XPointer] lookups) that depend on a particular document tree structure are to be used. If the parameter "normalize-characters" of the DOMConfiguration object attached to the Node.ownerDocument is true, this method will also fully normalize the characters of the Text nodes.
Note: In cases where the document contains CDATASections, the normalize operation alone may not be sufficient, since XPointers do not differentiate between Text nodes and CDATASection nodes.

- 4,458
- 3
- 24
- 37
There is a free command line tool for transforming files with special characters in text to valid XML. It also assures that the file encoding matches what is specified in the declaration.
There is also a Java developer suite that allows you to use the parser to parse such files (called XPL) as an alternative to XML or a pre-process into XML. It uses a StAX-like process called StAX-PL.

- 1,830
- 2
- 20
- 25
- A valid XML file should not have any of these characters "&", "<", ">" or "@". It should instead have the named entities "&", "<", ">", "®", """, "&apos", etc:
xml parsing with "&", "®", but still getting errors
- If the XML file is valid then parsing (using "android.util.XML", I presume), should "just work".
These links might also help:
-
I am using XML SAX parser and it is parsing all the chars except & and <...now I ma confused that how to handle them? – Kanika Nov 15 '11 at 06:04
public void XmlParsing(String questions_xml)
{
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXmlHandler myXMLHandler = new MyXmlHandler();
xr.setContentHandler(myXMLHandler);
xr.parse( new InputSource(new StringReader(questions_xml)));
} catch (Exception e) {
String err = (e.getMessage()==null)?"XMLParsing exception":e.getMessage();
Log.e("XMLParsing Exception",err);
}
}
now when I am trying to parse & then my app crashes?

- 10,648
- 18
- 61
- 81