开发者

Sax Parser Character Array to Integer?

开发者 https://www.devze.com 2022-12-25 09:27 出处:网络
I am trying to get the contents of tags into variables in my java Sax parser. However, the Characte开发者_如何学编程rs method only returns Char arrays. Is there anyway to get the Char array into an In

I am trying to get the contents of tags into variables in my java Sax parser. However, the Characte开发者_如何学编程rs method only returns Char arrays. Is there anyway to get the Char array into an Int???

public void characters(char ch[], int start, int length) {

  if(this.in_total_results) {
      // my INT varialble would be nice here!
  } 

}

Can anyone help at all?

Kind regards,

Andy


To be really correct, you would need to accumulate the value passed in characters, as it may be called more than once per tag.

This can notably happen if you have entities in the text. A tag such as <a>&#49;&#50;&#51;</a> is equivalent to <a>123</a> (49,50,51 are illustrative, but I think these are the right codes for 1,2,3)

So here is an excerpt taken from this question:

private StringBuffer curCharValue = new StringBuffer(1024);

@Override
public void startElement (String uri, String localName,
              String qName, Attributes attributes) throws SAXException {
    if(qName.equals("author")){
        curCharValue.clear();
    }
}

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

@Override
public void endElement (String uri, String localName, String qName)
throws SAXException
{
    if(qName.equals("author")){
        String author = curCharValue.toString();
    }
}

To convert from StringBuffer to int, just use new Integer( curCharValue.toString() ). Note that there are other methods to convert from String to Integer or int, as pointed out in the comment.

0

精彩评论

暂无评论...
验证码 换一张
取 消