【问题标题】:traversing DOM parsed XML [duplicate]遍历 DOM 解析的 XML [重复]
【发布时间】:2012-08-14 05:37:33
【问题描述】:

可能重复:
Traversing complex xml File in android

在此我正在使用 DOM 解析器解析一个 xml 文件,该解析器将 dom1 作为解析后的文档。 问题是我想在此之后创建 UI,但我无法相同,因为我找不到相同的逻辑,请帮助我。这也给了我错误 getLength() em> 值。有什么问题??

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。

【问题讨论】:

  • 是的,这只是我的问题。但当时我没有用它来制作我的UI,然后卡住了。所以我想提供更多细节,以便我可以获得具体的帮助。

标签: java android xml-parsing


【解决方案1】:

我只是举个例子,试试这样吧....

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#####");

    }
}

【讨论】:

  • 看到我必须在此之后创建一个 UI,因为我正在使用 ListView 和一个 ArrayList 数组来存储我的值。问题是我必须指定一个否。到每个级别,这就是我卡住的地方。你提到的上述事情我已经尝试过了。
  • 例如,如果我的数组是 test[],那么 test[0] 应该有 main, test[1]->1L1,1L2,1L3, test[2]->2L1, test[3 ]->2L2 等等。请参考xml
猜你喜欢
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 2011-07-28
  • 1970-01-01
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
相关资源
最近更新 更多