4

同様の問題

xml現在保持している事前定義されたものはほとんどなくres>raw>first.xml、実行時にフェッチし、以下のようなデータを表示しています。

NodeList nodes = MainActivity.commonmethod.GetDocumentFile(ProductActivity.this,_intRowID).getElementsByTagName("string");


for (int i = 0; i < nodes.getLength(); i++) {                           

            Element e = (Element)nodes.item(i);
            e.normalize();

                    _ArrProductName.add( MainActivity.commonmethod.getValue(e, "string"));              
            }

ドキュメントを使用して XML ファイル (Plist ファイル) を取得する方法

public Document GetDocumentFile(Context context, int rawID) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    DocumentBuilder builder = null;
    try {
        builder = builderFactory.newDocumentBuilder();

        document = builder.parse(context.getResources().openRawResource(
                rawID));
        document.getDocumentElement().normalize();
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return document;
}

getValue メソッド

public String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);

        try {
            StringWriter sw = new StringWriter(); 
            Transformer serializer = TransformerFactory.newInstance().newTransformer(); 
            serializer.transform(new DOMSource(n.item(0)), new StreamResult(sw));   
            String result = sw.toString(); 

            System.out.println("result="+result);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return CommonMethod.getElementValue(n.item(0));
    }

    public final static String getElementValue(Node elem) {
        Node kid;
        if (elem != null) {
            if (elem.hasChildNodes()) {
                for (kid = elem.getFirstChild(); kid != null; kid = kid
                        .getNextSibling()) {
                    if (kid.getNodeType() == Node.TEXT_NODE) {
                        return kid.getNodeValue();
                    }
                }
            }
        }
        return "";
    }

test.xml

 <?xml version="1.0" encoding="UTF-8"?>

    <array>
       <!-- PrdocuName -->
       <string>Android ICS 4.0™&lt;/string>

     <!-- PrdocutDescription -->
       <string>Mobile</string>

       <!-- PrdocuImage -->
       <string>Mobile.png</string>

      <!-- PrdocuAddress -->
       <string>url</string>

        <!-- Conversion -->
       <integer>400</integer>

      <!-- ThicknessNames -->
       <string>skim</string>

       <!-- ThicknessValues -->
       <string>1</string>

      <!-- LongDescription -->
       <string>Android is the market leader in terms of total number of device sold and soon it will be leader in terms of total number of application available in the market.</string>

    </array>

上記のコード全体は 4.0 より下では十分に機能しますが、4.0 より上では動作しません。getElementsByTagName` は 4.0 より上では null の結果を返します。

4.0未満の結果

<?xml version="1.0" encoding="UTF-8"?><string>Android ICS 4.0™&lt;/string>

4.0以上の結果

<?xml version="1.0" encoding="UTF-8"?>

String tag4.0 以上でのテスト中に欠落している、

4

1 に答える 1

0

最後に、getvalue関数内で変更を行いました。

変更コード:

public String getValue(Element item, String str) {


        String strResponse="";
        Node kid;
        if(item!=null)
        {
            if(item.hasChildNodes())
            {
                for(kid=item.getFirstChild(); kid!=null; kid =kid.getNextSibling())
                {
                    if (kid.getNodeType() == Node.TEXT_NODE) {
                        strResponse =kid.getNodeValue();
                        return strResponse;
                    }
                }
            }else
            {
                NodeList n = item.getElementsByTagName(str);
                n = item.getChildNodes();

                if(((Node) n.item(0))!=null)
                {
                    if(((Node) n.item(0)).getNodeValue() !=null)
                    {
                        strResponse =((Node) n.item(0)).getNodeValue();
                        return strResponse;
                    }else
                    {
                        strResponse ="";
                    }
                }


            }
        }


        return strResponse;

    }

その作品は完璧です!

于 2013-02-20T10:09:53.497 に答える