BlackBerry中的SAX解析器
我的XML格式如下BlackBerry中的SAX解析器,blackberry,java-me,saxparser,Blackberry,Java Me,Saxparser,我的XML格式如下 <users> <user uid="1" dispname ="Yogesh C" statid="1" statmsg = "Busy">Yogesh Chaudhari</user> <user uid="2" dispname ="Sameer S" statid="2" statmsg = "Available">Yogesh Chaudhari</user> </users> 这是太
<users>
<user uid="1" dispname ="Yogesh C" statid="1" statmsg = "Busy">Yogesh Chaudhari</user>
<user uid="2" dispname ="Sameer S" statid="2" statmsg = "Available">Yogesh Chaudhari</user>
</users>
这是太多的代码,无法获得有用的帮助。将您的代码示例限制为几十行。您好。我将减少代码。但是,您能否告诉我,我们是否可以使用SAX更新XML中属性的值。经过调查,我觉得我无法直接使用SAX更改属性的值。请帮助。您好。我终于使用SAX解析更新了XML中的属性值。我已经使用了setValueAttributesImpl类的方法(org.xml.sax.Attributes).最后我更新了XML,但我感觉的缺点是,即使更新了一条记录的属性,我也必须用n条记录重新构造完整的XML,然后将其重写到设备内存中。我发现,仅仅为了一条记录的更新而重新构造完整的XML并将其重写到设备内存中是不可行的。对此有什么建议吗?如果任何人想要关于我如何更新属性值的代码,请告诉我我将发布代码。您遇到的问题是为什么人们一直警告您不要这样做。您已经设置了必须使用xml文件的条件。因此,我想您可以将xml解析为一个向量,从该向量执行读取并写入该向量,再将事务写入单独的文件中的xml。然后,恢复将读取主xml+您的一系列事务xml。每隔一段时间,您可以获取一个检查点,并将所有检查点前事务xml提交到一个新的主xml,成功后替换旧的主xml。或者您可以只使用持久存储?:)这是太多的代码,无法获得有用的帮助。将您的代码示例限制为几十行。您好。我将减少代码。但是,您能否告诉我,我们是否可以使用SAX更新XML中属性的值。经过调查,我觉得我无法直接使用SAX更改属性的值。请帮助。您好。我终于使用SAX解析更新了XML中的属性值。我已经使用了setValueAttributesImpl类的方法(org.xml.sax.Attributes).最后我更新了XML,但我感觉的缺点是,即使更新了一条记录的属性,我也必须用n条记录重新构造完整的XML,然后将其重写到设备内存中。我发现,仅仅为了一条记录的更新而重新构造完整的XML并将其重写到设备内存中是不可行的。对此有什么建议吗?如果任何人想要关于我如何更新属性值的代码,请告诉我我将发布代码。您遇到的问题是为什么人们一直警告您不要这样做。您已经设置了必须使用xml文件的条件。因此,我想您可以将xml解析为一个向量,从该向量执行读取并写入该向量,再将事务写入单独的文件中的xml。然后,恢复将读取主xml+您的一系列事务xml。每隔一段时间,您可以获取一个检查点,并将所有检查点前事务xml提交到一个新的主xml,成功后替换旧的主xml。或者您可以只使用持久存储?:)
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
public class SaxParseUrl extends UiApplication {
public SaxParseUrl() {
pushScreen(new Pars());
}
public static void main(String[] args) {
SaxParseUrl app = new SaxParseUrl();
app.enterEventDispatcher();
}
}
class Pars extends MainScreen implements ContentHandler
{
boolean name;
Pars() {
try {
XMLReader parser = XMLReaderFactory.createXMLReader();
parser.setContentHandler(this);
FileConnection conn = (FileConnection)Connector.open("file:///store/home/user/employee.xml");
InputStream is = conn.openDataInputStream();
InputSource iss = new InputSource(is);
parser.parse(iss);
} catch (Exception e) {
debug("file:///store/home/user/SAXParser.txt","Exception in pars() :"+e);
}
}
public void startElement(String nsURI, String strippedName, String tagName,
Attributes attributes) throws SAXException {
try {
debug("file:///store/home/user/SAXParser.txt","startElement");
if (tagName.equalsIgnoreCase("user"))
{
debug("file:///store/home/user/SAXParser.txt","Inside startElement");
name = true;
String uid = attributes.getValue("uid");
String dispname = attributes.getValue("dispname");
String statid = attributes.getValue("statid");
String statmsg = attributes.getValue("statmsg");
attributes.
//LabelField lb = new LabelField(uid+"==>"+dispname+"==>"+statid+"==>"+statmsg);
LabelField lb = new LabelField("uid ==>"+uid+"\ndispname==>"+dispname+"\nstatid==>"+statid+"\nstatmsg==>"+statmsg);
add(lb);
}
} catch (Exception e) {
System.out.println(e);
}
}
public void characters(char[] ch, int start, int length) {
debug("file:///store/home/user/SAXParser.txt","characters");
if (name) {
try {
System.out.println("Title: " + new String(ch, start, length));
LabelField lb=new LabelField(""+ new String(ch, start, length));
HorizontalFieldManager sr=new HorizontalFieldManager();
sr.add(lb);
add(sr);
//v_cid.addElement(new String(ch, start, length));
//System.out.println("the elements of vector: " + v_cid);
// name = false;
//Enumeration e = v_cid.elements();
//System.out.println("The elements of vector: " + v_cid);
//while (e.hasMoreElements()) {
//System.out.println("The elements are: " + e.nextElement());
//}*/
} catch (Exception e) {
System.out.println(e);
}
}
}
public void endDocument() throws SAXException {
debug("file:///store/home/user/SAXParser.txt","endDocument");
}
public void endElement(String uri, String localName, String tagName)
throws SAXException {
debug("file:///store/home/user/SAXParser.txt","endElement");
}
public void endPrefixMapping(String prefix) throws SAXException {
debug("file:///store/home/user/SAXParser.txt","endPrefixMapping");
}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
debug("file:///store/home/user/SAXParser.txt","ignorableWhitespace");
}
public void processingInstruction(String target, String data)
throws SAXException {
debug("file:///store/home/user/SAXParser.txt","processingInstruction");
}
public void setDocumentLocator(Locator locator) {
debug("file:///store/home/user/SAXParser.txt","setDocumentLocator");
}
public void skippedEntity(String name) throws SAXException {
}
public void startDocument() throws SAXException {
debug("file:///store/home/user/SAXParser.txt","startDocument");
}
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
debug("file:///store/home/user/SAXParser.txt","startPrefixMapping");
}
public static void debug(String strFileName,String strData)
{
FileConnection fc = null;
StringBuffer strbufData = null;
OutputStream ops = null;
try
{
fc = (FileConnection)Connector.open(strFileName,Connector. READ_WRITE);
//create the file if it doesnt exist
if(!fc.exists())
{
fc.create();
}
InputStream is = fc.openDataInputStream();
strbufData = new StringBuffer();
int intCh;
while((intCh = is.read())!= -1)
{
strbufData.append((char)intCh);
}
strbufData.append("\n\n"+strData);
String strFileContent = new String(strbufData.toString());
byte byteData[] = strFileContent.getBytes();
ops = fc.openOutputStream();
ops.write(byteData);
}
catch(Exception e)
{
Dialog.alert("Exception in writing logs :"+e);
}
finally
{
if(ops != null)
{
try
{
ops.close();
}catch(Exception e)
{
}
}
if(fc != null)
{
try
{
fc.close();
}catch(Exception e)
{
}
}
}
}
}