简体   繁体   English

XML无法解析为转义字符

[英]XML not parsing with Escape character

I am trying to write a simple SAX parser, I am receiving the inputs from a Web service response, and it includes escape characters < and > When I am trying to parse it using my code, I am getting Reference is not allowed in prolog. 我正在尝试编写一个简单的SAX解析器,我从Web服务响应中接收输入,并且它包含转义符<和>当我尝试使用我的代码对其进行解析时,我在序言中获得了Reference不允许。 Error, where as if I change the escape characters to normal < and > character it is parsing without any issues, I guess I am just missing something very simple here... can somebody please help? 错误,好像我将转义字符更改为普通的<和>字符一样,没有任何问题,我想我只是在这里缺少了一些非常简单的东西……有人可以帮忙吗?

import java.io.ByteArrayInputStream;
import java.io.FileReader;
import java.io.InputStream;

import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;

public class Test extends DefaultHandler {

    public static void main(String args[]) throws Exception {
        XMLReader xr = XMLReaderFactory.createXMLReader();
        Test handler = new Test();
        xr.setContentHandler(handler);
        xr.setErrorHandler(handler);

        String xml_string = "&lt;rootnode&gt;&lt;a&gt;hello&lt;/a&gt;&lt;b&gt;world&lt;/b&gt;&lt;/rootnode&gt;";
        InputStream xmlStream = new ByteArrayInputStream(xml_string.getBytes("UTF-8"));
        xr.parse(new InputSource(xmlStream));
    }

    public Test() {
        super();
    }

    ////////////////////////////////////////////////////////////////////
    // Event handlers.
    ////////////////////////////////////////////////////////////////////

    public void startDocument() {
        System.out.println("Start document");
    }

    public void endDocument() {
        System.out.println("End document");
    }

    public void startElement(String uri, String name, String qName, Attributes atts) {
        if ("".equals(uri))
            System.out.println("Start element: " + qName);
        else
            System.out.println("Start element: {" + uri + "}" + name);
    }

    public void endElement(String uri, String name, String qName) {
        if ("".equals(uri))
            System.out.println("End element: " + qName);
        else
            System.out.println("End element:   {" + uri + "}" + name);
    }

    public void characters(char ch[], int start, int length) {
        System.out.print("Characters:    \"");
        for (int i = start; i < start + length; i++) {
            switch (ch[i]) {
            case '\\':
                System.out.print("\\\\");
                break;
            case '"':
                System.out.print("\\\"");
                break;
            case '\n':
                System.out.print("\\n");
                break;
            case '\r':
                System.out.print("\\r");
                break;
            case '\t':
                System.out.print("\\t");
                break;
            default:
                System.out.print(ch[i]);
                break;
            }
        }
        System.out.print("\"\n");
    }
}

You shouldn't be using escape characters in your xmlstring . 您不应该在xmlstring使用转义字符。 You need to use < and > for the xml tags. 您需要对xml标签使用<> Only escape them when you need to include < or > as part of the content of an attribute/element not the element tag itself. 仅在需要将<或>作为属性/元素内容的一部分而不是元素标签本身的一部分时才转义它们。

For normal tags one should use < and > like < root >...< / root >. 对于普通标签,应该使用<和>,例如<root> ... </ root>。 Only in real text < and > should be escaped to & lt ; 仅在实文本中<和>应该转义为&lt; and & gt ;. &gt;。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM