Java 怎么处理XML实体

2019年8月9日22:18:39Java 怎么处理XML实体已关闭评论 374 738字阅读2分27秒

1.简介

Java 怎么处理XML实体?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import java.io.IOException;
import java.io.Reader;

public class Main{
    
    static void processEntity(String entity, StdXMLReader reader,
            XMLEntityResolver entityResolver) throws IOException,
            XMLParseException {
        entity = entity.substring(1, entity.length() - 1);
        Reader entityReader = entityResolver.getEntity(reader, entity);

        if (entityReader == null) {
            XMLUtil.errorInvalidEntity(reader.getSystemID(),
                    reader.getLineNr(), entity);
        }

        boolean externalEntity = entityResolver.isExternalEntity(entity);
        reader.startNewStream(entityReader, !externalEntity);
    }
    
    static void errorInvalidEntity(String systemID, int lineNr,
            String entity) throws XMLParseException {
        throw new XMLParseException(systemID, lineNr, "Invalid entity: `&"
                + entity + ";'");
    }
}

 
编程十万个怎么办