【问题标题】:getting a single xml node - android dev获取单个 xml 节点 - android dev
【发布时间】:2010-07-12 10:05:36
【问题描述】:

我想制作一个使用 xml 数据库中的数据的应用程序,有两点需要帮助,因为我发现带有 xml 的 Java 太复杂了。

例如

<items>
<1><name>box</name><price>2.00</price></1>
<2><name>pencil</name><price>1.00</price></1>
</items>
  1. 从子节点获取单个 xml 值(例如,第一项的名称 - “box”)
  2. 从子节点获取一个 xml 数据库(将其用作列表框的数据源)——这不是必需的。

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    您的问题是 XML 节点必须以字母开头命名 - 就像 Java 中的变量一样。尝试以下方法:

    <items>
        <item name="box" price="2.00" />
        <item name="pencil" price="1.00" />
    </items>
    

    所以你看,每个item 都表示为&lt;item&gt;。在 XML 中,标签具有语义含义。你可以让他们做类似的事情(取决于你的解析器——这实际上更接近 JavaScript,但我希望你能明白):

    myXmlDocument = parseThatXMLForMePlease(); // Load the XMl from wherever
    itemList = myXmlDocument.getChildren()[0]; // Get the first node in the document
    firstItem = itemList.getChildren()[0];     // Get the first child of the item list
    itemName = firstItem.getAttr("name");
    itemPrice = firstItem.getAttr("price");
    

    如果您想将数据用作某些列表框的基础,您可能需要使用Adapter。这基本上是一种数据结构,旨在为某些 UI 小部件提供数据(链接指向 ListAdapter - 这听起来正是您想要的)。

    【讨论】:

      猜你喜欢
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 2012-12-13
      • 1970-01-01
      • 2013-03-19
      • 1970-01-01
      相关资源
      最近更新 更多