0
<layout>

<menu1>
    <title>Oil Painting</title>
    <sectioncount>5</sectioncount>
    <cost>0</cost>
    <appcode>0</appcode>
    <section1.1>
        <title>Oil Painting basics</title>
        <subtitle>9 minutes 57 secs*!How to do Oil Painting</subtitle>
        <video1.1></video1.1>
        <imgvideo>Landscape19.jpg</imgvideo>
        <description>video1.html</description>
    </section1.1>
    <section1.2>
        <title>Advacned Oil Painintg</title>
        <subtitle>6 minutes 23 secs*!Advanced Oil Painting</subtitle>
        <video1.2></video1.2>
        <imgvideo>Landscape19.jpg</imgvideo>
        <description>video1.html</description>
    </section1.2>
    <section1.3>
        <title>Watercolors basic</title>
        <subtitle>9 minutes 51 secs*!How to use watercolors</subtitle>
        <video1.3></video1.3>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section1.3>
    <section1.4>
        <title>Watercolors Advanced</title>
        <subtitle>3 minutes 46 secs*!Advacned training- Watercolors</subtitle>
        <video1.4></video1.4>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section1.4>
    <section1.5>
        <title>Enamel Painting Basic</title>
        <subtitle>1 minute 56 secs*!Advacned training- Watercolors</subtitle>
        <video1.5></video1.5>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section1.5>     
</menu1>

<menu2>
    <title>Graphite Pencil Sketching</title>
    <sectioncount>4</sectioncount>
    <cost>.99</cost>
    <appcode>0</appcode>
    <section2.1>
        <title>Basic Graphite Pencil Sketching</title>
        <subtitle>5 minutes 29 secs*!What you need to know</subtitle>
        <video2.1></video2.1>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section2.1>
    <section2.2>
        <title>Advanced Graphite Pencil Sketching</title>
        <subtitle>3 minutes*!The proffessional way</subtitle>
        <video2.2></video2.2>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section2.2>
    <section2.3>
        <title>Graphite Pencil Tools</title>
        <subtitle>5 minutes*!what should you use</subtitle>
        <video2.3></video2.3>
        <imgvideo>Landscape19.jpg</imgvideo>
        <description>video1.html</description>
    </section2.3>
    <section2.4>
        <title>Do's and Don't of Graphite Pencil Sketching</title>
        <subtitle>5 minutes*Cautions and precautions</subtitle>
        <video2.4></video2.4>
        <imgvideo>Landscape19.jpg</imgvideo>
        <description>video1.html</description>
    </section2.4>
</menu2>

<menu3>
    <title>Pen Sketching</title>
    <sectioncount>3</sectioncount>
    <cost>0</cost>
    <section3.1>
        <title>Basic Pen Sketching</title>
        <subtitle>2 minutes*!what you need to know</subtitle>
        <video3.1></video3.1>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section3.1>
    <section3.2>
        <title>Advanced Pen Sketching</title>
        <subtitle>3 minutes*!proffessional approach</subtitle>
        <video3.2></video3.2>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section3.2>
    <section3.3>
        <title>Pen Sketching Tools</title>
        <subtitle>3 minutes*!which pens to use</subtitle>
        <video3.3></video3.3>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section3.3>
</menu3>
</layout>
  1. 我想解析这个 xml 代码
  2. 我可以得到所有的值,但是我有问题如何将它存储在类中并检索它并在列表视图中显示它。我可以获得标题,部分代码。但问题是如何获取多个部分的子节点并将其存储在 ArrayList 中。
  3. 请帮我解析这个xml。我有我正在处理的代码,但它太乱了。但如果有人想看到,我可以展示出来。

-

public class twitterFeedHandler extends DefaultHandler

StringBuilder sb = null;
String ret = "";
boolean bStore = false;
int howMany = 0;
Context context;
String a;
boolean config = false;
boolean menu = false;
Boolean section = false;

String elementValue = null;
Boolean elementOn = false;

private final static String TAG_Layout = "layout";
private final static String TAG_COnfig = "configuration";
private final static String TAG_Menu = "menu";

public static XMLGettersSetters data = null;
public static XMLGettersSettersMenu datamenu = null;

twitterFeedHandler() {
}


}

@Override
public void startDocument() throws SAXException {

}

@Override
public void endDocument() throws SAXException {

}

@Override
public void startElement(String namespaceURI, String localName,
        String qName, Attributes atts) throws SAXException {


    try {





        elementOn = true;

        if (localName.equals(TAG_Layout)) {
            data = new XMLGettersSetters();
            datamenu = new XMLGettersSettersMenu();
        }if (localName.equals(TAG_COnfig)) {

            config = true;
            Log.i("Config localName", "configuration start");

        } else if (localName.equals(TAG_Menu)) {

            Log.i("Menu localName", "menu1 start");

            if (localName.equals("section1")) {
                if(localName.equals("stitle1"))
                Log.i("Section localName", "section start");
                section = true;

            }

            menu = true;


            Log.i("DATA --- > Menu ", datamenu.getCompany().toString());
        }

    } catch (Exception ee) {

        Log.d("error in startElement", ee.getStackTrace().toString());
    }
}

@Override
public void endElement(String namespaceURI, String localName, String qName)
        throws SAXException {

    if (localName.equals("configuration")) {

        Log.i("Config localName", "configuration end");
        bStore = false;
    } else if (localName.equals("menu")) {
        Log.i("Menu localName", "menu end");

        if (localName.equals("section1")) {
            Log.i("Section localName", "section end");
            section = false;
        }
        menu = false;
    }
     if(localName.equals("title"))
         datamenu.setTitle(elementValue);
     if(localName.equals("sectioncount"))
         datamenu.setCompany_SectionCount(elementValue);
     if(localName.equals("section1"))
         if(localName.equals("stitle1"))
             datamenu.setSection(elementValue);

     Log.i("Menu localName214213132132132"," " + datamenu.getCompany_SectionCount() + " size " + datamenu.getSection());
    elementOn = false;

    }


@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {

    if (elementOn) {
        elementValue = new String(ch, start, length);
        Log.i("--------->Name", section + " " );

        if (config && menu == false)
            data.setCompany(elementValue);
        else 
            datamenu.setCompany(elementValue);

        elementOn = false;
    }

}

public class XMLGettersSetters {
    private ArrayList<String> company = new ArrayList<String>();

    public ArrayList<String> getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company.add(company);

    }
}

public class XMLGettersSettersMenu {
    private ArrayList<String> company = new ArrayList<String>();
    private ArrayList<String> title = new ArrayList<String>();
    private ArrayList<String> section_count = new ArrayList<String>();
    private ArrayList<String> section = new ArrayList<String>();


    public ArrayList<String> getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company.add(company);

    }

    public void setTitle(String company) {
        this.title.add(company);

    }

    public ArrayList<String> getTitle() {
        return title;
    }

    public void setCompany_SectionCount(String company) {
        this.section_count.add(company);

    }

    public ArrayList<String> getCompany_SectionCount() {
        return section_count;
    }

    public void setSection(String company) {
        this.section.add(company);

    }

    public ArrayList<String> getSection() {
        return section;
    }
}
}
4

3 回答 3

0

i used this code to parse xml

xml is like this

<point>
  <ImageFile name="hi"/>
</point>

NodeList image = doc.getElementsByTagName("Point");
            Node singleTerminalNode = image.item(i);
            Element firstLevel = (Element)singleTerminalNode;
            NodeList value1Nodes = (firstLevel).getElementsByTagName("ImageFile");
            imagess = ((Element)value1Nodes.item(imageindex)).getAttribute("name");
            System.out.println("image index issssssssss " + imageindex);

hope this will help you

于 2012-05-11T08:57:54.143 回答
0

使用 SAX 解析器;这是最好的选择。

于 2012-05-11T05:31:58.963 回答
0

我想解析这个 xml 代码

=> 实现 SAX 解析器

我可以获取所有值,但我有问题如何将它存储在类中并检索它并在列表视图中显示它。我可以获得标题,部分代码。但问题是如何获取多个部分的子节点并将其存储在 ArrayList 中。

对于这个问题,我建议您创建一个具有标题、副标题和其他属性的类,并为其创建 Getter/setter 方法。

为什么我建议这个解决方案? 因为您正在尝试创建不同的 ArrayList:

private ArrayList<String> company = new ArrayList<String>();
private ArrayList<String> title = new ArrayList<String>();
private ArrayList<String> section_count = new ArrayList<String>();
private ArrayList<String> section = new ArrayList<String>();

相反,您可以创建单个 ArrayList 并每次添加对象。

在解析整个 XML 结束时,您将拥有包含多个对象的 ArrayList,现在您可以使用此 ArrayList 为 ListView 创建适配器。

于 2012-05-11T05:55:22.970 回答