私はAndroidが初めてで、ローカルのXMLファイルを取得してデータを取得して表示しようとしています。
これが私のコードです:
public String GetXmlData()
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try
{
builder = builderFactory.newDocumentBuilder();
InputStream is = getAssets().open("words.xml");
Document document = builder.parse(new FileInputStream("C:\\Users\\Ocean\\AndroidStudioProjects\\Deneme1Project\\Deneme1\\build\\res\\assets\\words.xml"));
Element rootElement = document.getDocumentElement();
NodeList nodes = rootElement.getChildNodes();
for(int i=0; i<nodes.getLength(); i++){
Node node = nodes.item(i);
if(node instanceof Element)
{
//a child element to process
Element child = (Element) node;
title = child.getAttribute("title");
String author= child.getAttribute("author");
String year= child.getAttribute("year");
}
}
return title;
}
catch (Exception e)
{
e.printStackTrace();
return e.toString();
}
}
最初の質問は、この XML ファイルをどこに置くべきですか? res 内にフォルダーを作成し、assets という名前を付け、その中に入れます rs フォルダーと r フォルダーがあります:S ファイルが見つかりません 例外 (ファイルはあります:P)