gpt4 book ai didi

android - 在 Android 中将已解析的 Xml 数据显示到 ListView

转载 作者:行者123 更新时间:2023-11-29 21:16:36 27 4
gpt4 key购买 nike

    public class MainActivity extends Activity {

ListView list;
EditText name,number;
Button store,display,update;
ArrayList<String> fetchData = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.pname);
number=(EditText)findViewById(R.id.pnumber);
store=(Button)findViewById(R.id.bSignup);
display=(Button)findViewById(R.id.bShow);
update=(Button)findViewById(R.id.bUpdate);

list=(ListView)findViewById(R.id.list);
try{
Time now = new Time();
now.setToNow();
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("user");
doc.appendChild(rootElement);

// staff elements
Element staff = doc.createElement("details");
rootElement.appendChild(staff);

// set attribute to staff element
Attr attr = doc.createAttribute("id");
attr.setValue("1");
staff.setAttributeNode(attr);

// shorten way
// staff.setAttribute("id", "1");

// firstname elements
Element firstname = doc.createElement("firstname");
firstname.appendChild(doc.createTextNode("manjeet"));
staff.appendChild(firstname);

// lastname elements
Element lastname = doc.createElement("lastname");
lastname.appendChild(doc.createTextNode("singh"));
staff.appendChild(lastname);

// nickname elements
Element nickname = doc.createElement("nickname");
nickname.appendChild(doc.createTextNode("mr. singh"));
staff.appendChild(nickname);

// salary elements
Element salary = doc.createElement("salary");
salary.appendChild(doc.createTextNode("100000"));
staff.appendChild(salary);

// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/data/data/com.example.xmlparsing/files/Register.xml"));

transformer.transform(source, result);

System.out.println("File saved!");

} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
Intent i = new Intent(MainActivity.this,XMLConcat.class);
startActivity(i);

display.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this,XMLConcat.class);
startActivity(i);
}
});
}


public class XMLConcat extends Activity{
ListView list;
ArrayList<String> fetchData = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.display_list);
list=(ListView)findViewById(R.id.list);
try {
File fXmlFile = new File("/data/data/com.example.xmlparsing/files/Register.xml");

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();

System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

NodeList nList = doc.getElementsByTagName("details");

System.out.println("----------------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

System.out.println("\nCurrent Element :" + nNode.getNodeName());

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;
//at console
System.out.println("Staff id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());


fetchData.add(eElement.getAttribute("id"));
fetchData.add(eElement.getAttribute("firstname"));
fetchData.add(eElement.getAttribute("lastname"));
fetchData.add(eElement.getAttribute("nickname"));
fetchData.add(eElement.getAttribute("salary"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
setListAdapter();
}
private void setListAdapter(){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item,fetchData);
list.setAdapter(adapter);
}

}

我想在 listview 中检索 xml 数据。该程序仅在控制台中显示输出,但我想在 listview 中显示输出。我正在学习如何在 Android 中解析 xml 文档。所以对此了解不多.请指导我。

最佳答案

关于android - 在 Android 中将已解析的 Xml 数据显示到 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21281601/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com