簡體   English   中英

在Android中使用屬性解析XML

[英]Parsing XML with attributes in Android

我正在嘗試(從資產)解析xml文件,但是我無法正確處理。

通常,當我這樣做時,我會像下面這樣格式化我的xml文件:

<channel>
  <item>
    <name>Hello Earth</name>
    <place>Earth</place>
  </item>
  <item>
    <name>Goodbye Mars</name>
    <place>Mars</place>
  </item/>
</channel>

我將使用以下代碼來解析我需要的內容:

AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("words.xml");

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setCoalescing(true);
DocumentBuilder builder = factory.newDocumentBuilder();

Document dom = builder.parse(inputStream);      

org.w3c.dom.Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("item");     

String name="";
String place="";

for (int i = 0; i < items.getLength(); i++) {
Node item = items.item(i);
NodeList properties = item.getChildNodes();

for (int j = 0; j < properties.getLength(); j++) {
    Node property = properties.item(j);
    String propertyName = property.getNodeName();

    if (propertyName.equalsIgnoreCase("name")) {
        String strText = property.getFirstChild()
        .getNodeValue();
        nam = strText;
    }

    if (propertyName.equalsIgnoreCase("place")) {
        String strText = property.getFirstChild()
        .getNodeValue();
        place = strText;
    }

    db.insertWord(name, place);
}

但是,xml來自另一個來源,其格式不同,由於它是mysql導出,因此使用屬性代替。 這是我必須解析的XML,但是我無法弄清楚該怎么做,我嘗試使用getAttribute()方法,但無法從xml中獲取數據:

<table name="item">
  <column name="name">Hello Earth</column>
  <column name="place">Earth</column>
</table>
<table name="item">
  <column name="name">Goodbye Mars</column>
  <column name="place">Mars</column>
</table>

試試這些......

1. DOM PARSER

2. SAX PARSER

3. JAXB AND JAXP

4. CASTOR

5. Pull Parser

您可以按照以下方式使用拉式解析器嘗試此操作。

//定義課程項目

class Item
{
   String name;
   String place;
}



 ArrayList<Item>items=new ArrayList<Item>();




void somemethod()
{

AssetManager assetManager = getAssets();
 InputStream inputStream = assetManager.open("words.xml");

Item item=new Item();
try
        {
             XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
             factory.setNamespaceAware(true);
             XmlPullParser xpp = factory.newPullParser();
             xpp.setInput(inputStream,null);
             int eventType = xpp.getEventType();
             while (eventType != XmlPullParser.END_DOCUMENT) 
             {
                 if(eventType == XmlPullParser.START_DOCUMENT) 
                  { }                 
                 else if(eventType == XmlPullParser.START_TAG)
                 {
                     try 
                     {                          
                             if(xpp.getName()!=null&& xpp.getName().equalsIgnoreCase("name"))
                              {
                                   eventType = xpp.next();
                                   item.name=Integer.parseInt(xpp.getText().toString());
                              }
                             else if(xpp.getName()!=null&& xpp.getName().equalsIgnoreCase("place"))
                              {
                                   eventType = xpp.next();
                                   item.place=xpp.getText().toString();
                              }                     
                        } 
                     catch (Exception e) 
                        {
                            //e.printStackTrace();
                        }
                 }
                 else if(eventType == XmlPullParser.END_TAG) 
                 {
                     if(xpp.getName()!=null&& xpp.getName().equalsIgnoreCase("content"))
                      {
                           eventType = xpp.next();  
                           items.put(item);
                           item=new Item();
                      }
                 }
                 else if(eventType == XmlPullParser.TEXT) 
                 {}
                 eventType = xpp.next();       
         }// end of while                    

        }
        catch (XmlPullParserException e) 
        {
            //e.printStackTrace();
        } 
        catch (IOException e) 
        {
          //e.printStackTrace();
        }
        finally
        {
            try 
            {
                if(inputStream!=null)
                    inputStream.close();
            } catch (IOException e) 
            {               
            }
        }


    }

看看這個問題的答案。 有一個如何處理屬性的示例:

如何在Android中使用xmlpullparser從嵌套xml中提取文本?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM