我需要按升序解析 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"
等等......