0

I want to parse local XML file as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<categories>
<category>
  <id>1</id>
  <name>Main name</name>
  <subcategories>
    <count>1</count>
    <subcategory>
      <id>2</id>
      <name>Sub name</name>
      <products>
        <count>5</count>
        <product>
          <id>1</id>
          <name>fullname</name>
          <description>this is full name and something</description>
          <color>white</color>
          <size>xyz</size>
          <images>
            <count>2</count>
            <image>
              <title>test</title>
              <url>www.test.com/test1.jpg</url>
            </image>
          </images>
        </product>
      </products>
    </subcategory>
  </subcategories>
</category>
<category>
  <id>2</id>
  <name>other name</name>
  <subcategories>
    <count>1</count>
    <subcategory>
      <id>2</id>
      <name>other name subname</name>
      <products>
        <count>5</count>
        <product>
          <id>1</id>
          <name>patname of othername</name>
          <description>this isapt name and something</description>
          <color>white</color>
          <size>xyz</size>
          <images>
            <count>2</count>
            <image>
              <title>test</title>
              <url>www.test.com/test1.jpg</url>
            </image>
          </images>
        </product>
      </products>
    </subcategory>
  </subcategories>
</category>
</categories>

How can I parse this local XML file in Android and how can I use it's inner tag like here in tag category it have id 1 and 2 so if id of category is 1 than print or user it's all inner tag content of that category and so on. And how can I use content of the both category's inner tag in Android?

Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
Khan
  • 7,585
  • 3
  • 27
  • 44
  • hi see this link http://stackoverflow.com/questions/4213876/how-to-do-xml-parsing-in-android – Sunny Dec 07 '11 at 11:59
  • possible duplicate of [Is there an easier way to parse XML in Java?](http://stackoverflow.com/questions/1719261/is-there-an-easier-way-to-parse-xml-in-java) – Flexo Dec 07 '11 at 16:27

3 Answers3

0

Parsing is simple and is same in all application, use any sample of SAX parser or any other to simply parse XML. You don't have to worry about the platform you are using whether it is android or not!

  public class AttendanceXMLParser {

public static final String STUDENT_ATTENDANCE = "student_attendance";

public List<Attendance> parseXML(String xml) {
    List<Attendance> attendanceList = new ArrayList<Attendance>();

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder;
    try {
        dBuilder = documentBuilderFactory.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));

        Document document = dBuilder.parse(is);

        NodeList attendanceNodeList = document.getElementsByTagName(STUDENT_ATTENDANCE);
        int nodeListLength = attendanceNodeList.getLength();
        Attendance attendance = null;
        for(int i=0; i<nodeListLength; i++) {
            attendance = new Attendance();
            Node messageNode = attendanceNodeList.item(i);
            //
            attendance.setStudentName(messageNode.getFirstChild().getTextContent());
            attendance.setCourseName(messageNode.getChildNodes().item(1).getTextContent());
            attendance.setSemester(messageNode.getChildNodes().item(2).getTextContent());
            attendance.setAttendance(messageNode.getChildNodes().item(3).getTextContent());
            attendanceList.add(attendance);
        }
        return attendanceList;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

}

please find the code, it actually doing parsing of simple attendace list based on the node student_attendance , and the function generate a list of students attendance, read the code carefully, this may help full to you!!

Jimshad Abdulla
  • 167
  • 2
  • 8
0

you would find the example in detail here: http://www.jondev.net/articles/Android_XML_SAX_Parser_Example

0

No no, can't use SAXParser in this case since many tags having same name...it's better to use XPath: http://developer.android.com/reference/javax/xml/xpath/package-summary.html

Pete Houston
  • 14,931
  • 6
  • 47
  • 60