在这里我使用DOM解析器解析xml文件,它将dom1作为解析文档。 问题是我想在此之后创建用户界面,我无法做到同样的事情,因为我无法找到相同的逻辑,请帮助我。这也是给我错误的 getLength()值。怎么了???
xml在此链接中:
http://nchc.dl.sourceforge.net/project/trialxml/options.xml
//this is function is called when i click my button
public void next123(View view){
Element root=dom1.getDocumentElement();
NodeList nodes=root.getChildNodes();
create_Menu(nodes);
}
public void create_Menu(NodeList nodes){
for(int i=0;i<nodes.getLength();i++){
Node node=nodes.item(i);
if(node instanceof Element){
Element child = (Element)node;
String name=getTextValue(child,"Name");
String typ=child.getAttribute("type");
if(name!=null){
z++;
Log.i(TAG,"Names are:= " +name+ " -> "+typ +" -> "+ z+ " -> "+ i);
NodeList nod=child.getChildNodes();
Log.i(TAG,"Length : "+nod.getLength());
create_Menu(nod);
Log.i(TAG,"end");
}
}
}
}
我必须在此之后创建一个UI,因为我使用ListView和ArrayList数组来存储我的值。问题是我必须分配一个号码。到各个层面,
例如,如果我的数组是test [],那么
test[0]-> main,
test[1]->1L1,1L2,1L3,
test[2]->2L1,
test[3]->2L2
test[4]->3L1,3L2
请xml参考。
答案 0 :(得分:0)
我只是举个例子,尝试这样做....
DocumentBuilderFactory odbf = DocumentBuilderFactory.newInstance();
DocumentBuilder odb = odbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document odoc = odb.parse(is);
odoc.getDocumentElement().normalize (); // normalize text representation
System.out.println ("Root element of the doc is " + odoc.getDocumentElement().getNodeName());
NodeList LOP = odoc.getElementsByTagName("locations");
int totalPersons =LOP.getLength();
System.out.println("Total nos of locations:"+totalPersons);
for(int s=0; s<LOP.getLength() ; s++)
{
Node FPN =LOP.item(s);
if(FPN.getNodeType() == Node.ELEMENT_NODE)
{
Element latlon = (Element)FPN;
NodeList oNameList1 = latlon.getElementsByTagName("featured");
Element firstNameElement = (Element)oNameList1.item(0);
NodeList textNList1 = firstNameElement.getChildNodes();
featuredArr = changeToBoolean(((Node)textNList1.item(0)).getNodeValue().trim()); // value taken
System.out.println("#####The Parsed data#####");
System.out.println("featured : " + ((Node)textNList1.item(0)).getNodeValue().trim());
System.out.println("#####The Parsed data#####");
}
}