【问题标题】:Removing tags from xml file for BlackBerry从 BlackBerry 的 xml 文件中删除标签
【发布时间】:2011-07-21 03:23:06
【问题描述】:

我正在开发一个需要读取 xml 文件的 BlackBerry 应用程序。我可以阅读它,但我想删除诸如<?xml version='1.0' encoding='UTF-8'?>、<feed>、<author> 等标签...

我使用的是 BlackBerry 9800 Simulator。

已检查我的文件是 Atom 1.0 文件。那么我应该如何删除这些标签呢?

目前我的代码是从我从互联网上获得的。但它只显示文件的“id”和“更新”日期。如何显示/过滤某些数据并删除标签?

谢谢, 亨德

 class MyApp extends UiApplication{
//creating a member variable for the MainScreen
MainScreen _screen= new MainScreen();
//string variables to store the values of the XML document
String _node,_element;
Connection _connectionthread;

public static void main(String arg[]){
    MyApp application = new MyApp();
    //create a new instance of the application
    //and start the application on the event thread
    application.enterEventDispatcher();
}

public MyApp() {
    _screen.setTitle("XML Parsing");//setting title
    _screen.add(new RichTextField("Requesting....."));
    _screen.add(new SeparatorField());
    pushScreen(_screen); // creating a screen
    //creating a connection thread to run in the background
    _connectionthread = new Connection();
    _connectionthread.start();//starting the thread operation
}

public void updateField(String node, String element) 
{    
    synchronized (UiApplication.getEventLock()) 
    {
        String title = "My App";
        _screen.add(new RichTextField(node + " : " + element));
        if (node.equals(title))
        {
            _screen.add(new SeparatorField());
            }
        }
    } 


private class Connection extends Thread{
    public Connection(){
        super();
    }

    public void run(){
        // define variables later used for parsing

        Document doc;
        StreamConnection conn;

        try{
            //providing the location of the XML file,
            //your address might be different
            conn=(StreamConnection)Connector.open
              ("http://www.google.com");
            //next few lines creates variables to open a
            //stream, parse it, collect XML data and
            //extract the data which is required.
            //In this case they are elements,
            //node and the values of an element
            DocumentBuilderFactory docBuilderFactory
              = DocumentBuilderFactory. newInstance(); 
            DocumentBuilder docBuilder
              = docBuilderFactory.newDocumentBuilder();

            docBuilder.isValidating();
            doc = docBuilder.parse(conn.openInputStream());
            doc.getDocumentElement ().normalize ();
            NodeList list=doc.getElementsByTagName("*");
            _node=new String();
            _element = new String();
            //this "for" loop is used to parse through the
            //XML document and extract all elements and their
            //value, so they can be displayed on the device

            for (int i=0;i<list.getLength();i++){
                Node value=list.item(i).
                  getChildNodes().item(0);
                _node=list.item(i).getNodeName();
                _element=value.getNodeValue();
                updateField(_node,_element);
            }//end for
        }//end try
        //will catch any exception thrown by the XML parser
        catch (Exception e){
            System.out.println(e.toString());

        }
      }//end connection function
  }// end connection class
}//end XML_Parsing_Sample

【问题讨论】:

  • 删除标签?你的意思是要忽略某些标签的内容,而去抓取其他标签的内容?
  • 嗨,雷,没错。我想删除那些 xml 标签,然后从中检索某些数据。例如。 2011-07-18T02:35:41.000ZHendhendyWve@gmail.com,我想检索日期和名称。谢谢
  • 好的,您可以将 getElementsByTagName 请求更改为外部标签而不是通配符,然后有选择地将内部标签读入自定义对象并将它们填充到向量中。这个网站上已经有很多类似的问题,而且答案非常好。

标签: xml parsing blackberry sax atom-feed


【解决方案1】:

创建另一个执行相同 run() 但搜索另一个 tagName 的方法。这允许读取和显示多个标签的信息。

【讨论】:

    猜你喜欢
    • 2021-04-26
    • 2021-01-24
    • 2020-09-02
    • 1970-01-01
    • 2019-10-03
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    相关资源
    最近更新 更多