Android: How Can I display all XML values of same tag name

前端 未结 4 1608
Happy的楠姐
Happy的楠姐 2021-01-20 15:43

I have the ff. XML from a URL:

 

    
                 


        
4条回答
  •  [愿得一人]
    2021-01-20 16:16

    I found an XML tutorial online here and editied it to work with your XML file. Below is the code. For the sake of testing it on my machine, I've sourced the XML file from a local file rather than online, but it shouldn't be too hard to work out.

    This should hopefully point you in the right direction.

    package phonebook;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import java.io.File;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    
    public class program {
        public static void main(String argv[]) {
    
              try {
                  File file = new File("phonebook.xml");
                  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                  DocumentBuilder db = dbf.newDocumentBuilder();
                  Document doc = db.parse(file);
                  doc.getDocumentElement().normalize();
                  System.out.println("Root element " + doc.getDocumentElement().getNodeName());
                  NodeList nodeLst = doc.getElementsByTagName("PhonebookEntry");
                  System.out.println("Information of all entries");
    
                  for (int s = 0; s < nodeLst.getLength(); s++) {
    
                    Node fstNode = nodeLst.item(s);
    
                    if (fstNode.getNodeType() == Node.ELEMENT_NODE)
                    {
                      Element fstElmnt = (Element) fstNode;
    
                      // Firstname
                      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");
                      Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
                      NodeList fstNm = fstNmElmnt.getChildNodes();
                      System.out.println("First Name : "  + ((Node) fstNm.item(0)).getNodeValue());
    
                      // Lastname
                      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname");
                      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
                      NodeList lstNm = lstNmElmnt.getChildNodes();
                      System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());
    
                      // Address
                      NodeList addrNmElmntLst = fstElmnt.getElementsByTagName("Address");
                      Element addrNmElmnt = (Element) addrNmElmntLst.item(0);
                      NodeList addrNm = addrNmElmnt.getChildNodes();
                      System.out.println("Address : " + ((Node) addrNm.item(0)).getNodeValue());
                    }
                  }
              } catch (Exception e) {
                e.printStackTrace();
              }
             }
    }
    

提交回复
热议问题