在 BlackBerry 中使用 SAX 解析器解析 XML

发布于 2024-10-09 09:26:13 字数 283 浏览 0 评论 0原文

我正在黑莓中寻找 SAX 解析器代码来解析 XML 文档, 检索 XML 文档并更新 XML 文档。我是 SAX 解析器的新手。

我的 XML 字符串具有以下类型:

<users>
   <user  uid = "1"  dispname="Yogesh C" statid="1" statmsg="1">Yogesh Chaudhari</user>
</users>

我必须使用 SAX 解析器解析上面的字符串。

I am looking for a SAX parser code in blackberry for parsing a XML document,
retrieve the XML document and update the XML document. I am new to the SAX parser.

My XML string is of following type:

<users>
   <user  uid = "1"  dispname="Yogesh C" statid="1" statmsg="1">Yogesh Chaudhari</user>
</users>

I have to parse above string with SAX parser.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情换悲伤 2024-10-16 09:26:13

你关于这个主题的一系列问题似乎加起来就是:我可以有一个可读/可写数据库的代码,该数据库使用SDCard上的XML格式文件进行存储吗?

这超出了我的回答资格,但这里是我在 BB 上测试 XML 时使用的工作示例代码。希望这能让您开始,当您完成后我可以得到数据库代码吗? ;)

public class XmlManager {

    protected Document document;

    //protected Log log = new Log("XmlManager");

    public XmlManager(String file) {
       FileConnection fc = null;
       InputStream inputStream = null;
       try {
           DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
           DocumentBuilder builder = factory.newDocumentBuilder();
           fc = (FileConnection)Connector.open(file, Connector.READ);
           inputStream = fc.openInputStream();  
           document = builder.parse( inputStream );
       } catch (Exception e) {
           //log.error("builder.parse", e);   
       } finally {
           try {
               if (fc!=null) fc.close();
               if (inputStream!=null) inputStream.close();
           } catch (Exception e) {
               //log.error("close", e);
           } 
       }
    }

    public String readApiString(String tag) {       
        Element root=document.getDocumentElement();
        NodeList list = root.getElementsByTagName(tag);
        return(list.item(0).getFirstChild().getNodeValue());
    }

示例 xml:

<myuniquetagname>foo</myuniquetagname>

示例用法:

XmlManager xmlManager = new XmlManager("file:///SDCard/BlackBerry/documents/myfile.xml");
String foo = xmlManager.readApiString("myuniquetagname");

Your series of questions on this subject seem to add up to: Can I have code for a readable/writable database that uses XML formatted files on the SDCard for storage?

That's more than I'm qualified to answer, but here is working sample code that I used when I was testing XML on BB. Hopefully this will get you started, and can I have the database code when you're finished? ;)

public class XmlManager {

    protected Document document;

    //protected Log log = new Log("XmlManager");

    public XmlManager(String file) {
       FileConnection fc = null;
       InputStream inputStream = null;
       try {
           DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
           DocumentBuilder builder = factory.newDocumentBuilder();
           fc = (FileConnection)Connector.open(file, Connector.READ);
           inputStream = fc.openInputStream();  
           document = builder.parse( inputStream );
       } catch (Exception e) {
           //log.error("builder.parse", e);   
       } finally {
           try {
               if (fc!=null) fc.close();
               if (inputStream!=null) inputStream.close();
           } catch (Exception e) {
               //log.error("close", e);
           } 
       }
    }

    public String readApiString(String tag) {       
        Element root=document.getDocumentElement();
        NodeList list = root.getElementsByTagName(tag);
        return(list.item(0).getFirstChild().getNodeValue());
    }

example xml:

<myuniquetagname>foo</myuniquetagname>

example usage:

XmlManager xmlManager = new XmlManager("file:///SDCard/BlackBerry/documents/myfile.xml");
String foo = xmlManager.readApiString("myuniquetagname");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文