2

我需要按升序解析 XML 代码。我的 XML 代码如下:

<node label="Tree Data">
<node name="View" type="Page">
<node name="Organisational Structure" type="Page"/>
<node name="Experience" type="Page"/>
<node name="Expertise" type="Language">
<node name="Flex" type="Language"/>
<node name="Android" type="Language"/>
<node name="Java" type="Language"/>
</node>
<node name="Project" type="Project">
<node name="Sabre" type="Project"/>
<node name="Android Project" type="Project"/>
<node name="Media Framework" type="Project"/>
<node name="CCD" type="Project"/>
<node name="Redbus" type="Project"/>
<node name="India Today" type="Project"/>
<node name="Money Control" type="Project"/>
</node>
</node>
<node name="Reorganize" type="Action"/>
<node name="Add New Employee" type="Action">
<node name="Dir" type="Page"/>
<node name="P.M" type="Page"/>
<node name="S.E" type="Page"/>
<node name="JUN" type="Page"/>
<node name="EMP" type="Page"/>
</node>
<node name="Add Project Members" type="Action"/>
</node>

我编写了以下java代码以升序对其进行排序

Java代码:

import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    Document dom;

    public MainActivity() {
        MainActivity();

        ArrayList<String> listName = getGroups();
        Collections.sort(listName);
        for (String name : listName) {
            System.out.println("name:" + name);
        }

    }

    private ArrayList<String> getGroups() {
        ArrayList<String> alNames = null;
        try {
            alNames = new ArrayList<String>();
            Element documentElement = (Element) dom.getDocumentElement();
            System.out.println("root element is :"
                    + documentElement.getNodeName());
            NodeList childNodes = documentElement.getElementsByTagName("node");

            for (int i = 0; i < childNodes.getLength(); i++) {

                if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) childNodes.item(i);
                    if (((Element) childNodes.item(i)).hasAttribute("name")) {
                        String name = element.getAttribute("name");
                        String type = element.getAttribute("type");

                        alNames.add(name);
                        System.out.println("name : " + name);
                        System.out.println("type : " + type);
                    }
                }
            }
        } catch (Exception e) {
        }
        return alNames;

    }

    private void MainActivity() {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {
            String sURL = "http://hhjjhhjcom";
            URL url = new URL(sURL);
            DocumentBuilder db = dbf.newDocumentBuilder();

            dom = db.parse(new InputSource(url.openStream()));

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

我的 logcot 显示以下输出:

02-01 11:02:24.368: I/System.out(798): root element is :node
02-01 11:02:24.368: I/System.out(798): name : View
02-01 11:02:24.377: I/System.out(798): type : Page
02-01 11:02:24.397: I/System.out(798): name : Organisational Structure
02-01 11:02:24.397: I/System.out(798): type : Page
02-01 11:02:24.397: I/System.out(798): name : Experience
02-01 11:02:24.397: I/System.out(798): type : Page
02-01 11:02:24.397: I/System.out(798): name : Expertise
02-01 11:02:24.397: I/System.out(798): type : Language
02-01 11:02:24.397: I/System.out(798): name : Flex
02-01 11:02:24.397: I/System.out(798): type : Language
02-01 11:02:24.397: I/System.out(798): name : Android
02-01 11:02:24.397: I/System.out(798): type : Language
02-01 11:02:24.397: I/System.out(798): name : Java
02-01 11:02:24.407: I/System.out(798): type : Language
02-01 11:02:24.407: I/System.out(798): name : Project
02-01 11:02:24.407: I/System.out(798): type : Project
02-01 11:02:24.407: I/System.out(798): name : Sabre
02-01 11:02:24.407: I/System.out(798): type : Project
02-01 11:02:24.407: I/System.out(798): name : Android Project
02-01 11:02:24.407: I/System.out(798): type : Project
02-01 11:02:24.407: I/System.out(798): name : Media Framework
02-01 11:02:24.407: I/System.out(798): type : Project
02-01 11:02:24.407: I/System.out(798): name : CCD
02-01 11:02:24.407: I/System.out(798): type : Project
02-01 11:02:24.407: I/System.out(798): name : Redbus
02-01 11:02:24.407: I/System.out(798): type : Project
02-01 11:02:24.407: I/System.out(798): name : India Today
02-01 11:02:24.407: I/System.out(798): type : Project
02-01 11:02:24.407: I/System.out(798): name : Money Control
02-01 11:02:24.407: I/System.out(798): type : Project
02-01 11:02:24.407: I/System.out(798): name : Reorganize
02-01 11:02:24.407: I/System.out(798): type : Action
02-01 11:02:24.407: I/System.out(798): name : Add New Employee
02-01 11:02:24.448: I/System.out(798): type : Action
02-01 11:02:24.448: I/System.out(798): name : Dir
02-01 11:02:24.448: I/System.out(798): type : Page
02-01 11:02:24.448: I/System.out(798): name : P.M
02-01 11:02:24.448: I/System.out(798): type : Page
02-01 11:02:24.448: I/System.out(798): name : S.E
02-01 11:02:24.448: I/System.out(798): type : Page
02-01 11:02:24.448: I/System.out(798): name : JUN
02-01 11:02:24.448: I/System.out(798): type : Page
02-01 11:02:24.448: I/System.out(798): name : EMP
02-01 11:02:24.448: I/System.out(798): type : Page
02-01 11:02:24.467: I/System.out(798): name : Add Project Members
02-01 11:02:24.467: I/System.out(798): type : Action
02-01 11:02:24.467: I/System.out(798): name:Add New Employee
02-01 11:02:24.467: I/System.out(798): name:Add Project Members
02-01 11:02:24.467: I/System.out(798): name:Android
02-01 11:02:24.487: I/System.out(798): name:Android Project
02-01 11:02:24.487: I/System.out(798): name:CCD
02-01 11:02:24.487: I/System.out(798): name:Dir
02-01 11:02:24.487: I/System.out(798): name:EMP
02-01 11:02:24.487: I/System.out(798): name:Experience
02-01 11:02:24.487: I/System.out(798): name:Expertise
02-01 11:02:24.487: I/System.out(798): name:Flex
02-01 11:02:24.487: I/System.out(798): name:India Today
02-01 11:02:24.487: I/System.out(798): name:JUN
02-01 11:02:24.487: I/System.out(798): name:Java
02-01 11:02:24.487: I/System.out(798): name:Media Framework
02-01 11:02:24.487: I/System.out(798): name:Money Control
02-01 11:02:24.487: I/System.out(798): name:Organisational Structure
02-01 11:02:24.487: I/System.out(798): name:P.M
02-01 11:02:24.487: I/System.out(798): name:Project
02-01 11:02:24.487: I/System.out(798): name:Redbus
02-01 11:02:24.487: I/System.out(798): name:Reorganize
02-01 11:02:24.487: I/System.out(798): name:S.E
02-01 11:02:24.487: I/System.out(798): name:Sabre
02-01 11:02:24.487: I/System.out(798): name:View

但我需要如下输出:

name:Add New Employee
type="Action"

name:Add Project Members
type="Action"

等等......

4

2 回答 2

1

只需将解析结果存储到 ArrayList 中,然后您可以根据需要对数据进行排序。我认为这是所有开发人员在解析后对数据进行排序的最佳方法。

您可以参考此链接在 ArrayList 中对数据进行排序。

希望你明白了。

于 2013-02-01T05:44:31.057 回答
1

首先,没有办法在你去的时候以排序的方式读取文件。

将数据存储在一个对象中,例如:

public class XmlEntry implements Comparable<XmlEntry>{
    private final String mName;
    private final String mType;

    public String getName() {
        return mName;
    }

    public String getType() {
        return mType;
    }

    public XmlEntry(String name, String type) {
        mName = name;
        mType = type;
    }

    @Override
    public int compareTo(XmlEntry xmlEntry) {
        //--sort by name--
        return mName.compareTo(xmlEntry.getName());
    }
}

添加完成后创建ArrayList<XmlEntry>并添加项目alEntries.add(new XmlEntry(name,type)),调用Collections.sort(alEntries).

您可以更改XmlEntry类以保存任意数量的字段,并将其比较方法设置为按字段的任意组合进行排序。

于 2013-02-01T05:56:42.830 回答