在SAX中解析特殊字符(<,> .. etc)

时间:2015-06-11 04:16:17

标签: java xml sax saxparser

我正在尝试解析包含特殊字符的xml文档,例如" // gpa [。< 2.0]"在一些节点中。但是,解析器只读取节点内容的一部分,如" // gpa [。"特殊字符所在的位置。我尝试了在线发现的不同方法,但仍然得到相同的结果。如何解析所有节点的内容。

以下是我的部分代码,并提前感谢您:

package temp;

import java.util.*;
import java.lang.Object.*;
import org.xml.sax.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.apache.commons.lang.StringEscapeUtils;

public class Login implements LoginInterface{

public static String elementName="";    
public static StringBuilder chars = new StringBuilder();

public static void startDocument ()
throws SAXException, IOException
{
}

public static void endDocument ()
throws SAXException
{
}

public static void characters (char buf [], int offset, int len)
throws SAXException
{
    chars.append(new String(buf , offset, len));                
        if(elementName.equals("object"))
            {                    
            //tempObjectStr += org.apache.commons.lang.StringEscapeUtils.escapeXml( new String(buf, offset, len));
            //characters(s.toCharArray(),0,s.length());                                            
            System.out.println( "objectNodeContent: " + chars.toString());            
            }
}

public static void startElement (String uri, String localname, String name, Attributes attrs)
throws SAXException
 {
    chars.setLength(0);
    //nodeContent = "";
    elementName=name;       
 }

public static void endElement (String uri, String localName, String name)
throws SAXException
{        
}    

public static void main(String args[]) {    
    try
    {            
         SAXParser saxParser2 = factory.newSAXParser();
         saxParser2.parse( "authorization.xml",new LoginHandlerBase());             
    }
    catch(Exception e)
    {
        System.out.println("Error:"+e);
    }
}
}

这是Authorization.xml的一部分:

<rules>
    <rule>
        <role>staff</role>
        <object>/department/gradstudent/address</object>        
        <action>Read</action>
        <type>R</type>
    </rule>
    <rule>
        <role>staff</role>
        <object>//gpa[.&lt;2.0]</object>        
        <action>Read</action>
        <type>L</type>
    </rule>
</rules>

输出看起来像:

objectNodeContent: /department/gradstudent/address
objectNodeContent: //gpa[.

1 个答案:

答案 0 :(得分:0)

请参阅javadoc

正如迈克尔凯所说,角色数据可能有几个块。你的contenthandler应该收集它们。下面的代码显示了如何执行此操作:

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParserFactory;
import java.io.File;

public class MySAXHandler extends DefaultHandler{
    private StringBuilder content = new StringBuilder();

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
        content.setLength(0);
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException{
        content.append(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException{
        if(localName.equals("object"))
            System.out.println("objectNodeContent: " + content);
        content.setLength(0);
    }

    public static void main(String[] args) throws Exception{
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.newSAXParser().parse(new File("Authorization.xml"), new MySAXHandler());
    }
}

上述代码的输出是:

objectNodeContent: /department/gradstudent/address
objectNodeContent: //gpa[.<2.0]