Parse XML special characters?

前端 未结 5 874
时光取名叫无心
时光取名叫无心 2021-01-23 21:36

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 a

相关标签:
5条回答
  • 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.

    0 讨论(0)
  • 2021-01-23 21:52

    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.

    0 讨论(0)
  • 2021-01-23 21:54
    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:

    • Parsing invalid ampersands with Android's XmlPullParsers

    • http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

    0 讨论(0)
  • 2021-01-23 22:00
     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?
    
    0 讨论(0)
  • 2021-01-23 22:04
    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.

    0 讨论(0)
提交回复
热议问题