Parse XML special characters?

2019-02-28 19:28发布

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??

5条回答
迷人小祖宗
2楼-- · 2019-02-28 19:47

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.

查看更多
地球回转人心会变
3楼-- · 2019-02-28 19:51
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楼-- · 2019-02-28 19:56

You can try something like this, that is giving the reference for that special character.

Character Reference

&   -   &
<   -   &lt;
>   -   &gt;
"   -   &quot;
'   -   &apos;

UPDATE:

I had already answered in your question here so have a look at the Answer.

查看更多
你好瞎i
5楼-- · 2019-02-28 20:06
  1. A valid XML file should not have any of these characters "&", "<", ">" or "@". It should instead have the named entities "&amp", "&lt", "&gt", "&reg", "&quot", "&apos", etc:

xml parsing with "&amp;", "&reg;", but still getting errors

  1. If the XML file is valid then parsing (using "android.util.XML", I presume), should "just work".

These links might also help:

查看更多
不美不萌又怎样
6楼-- · 2019-02-28 20:09
 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?
查看更多
登录 后发表回答