下面是我的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 CODE:
import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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<Element> listGroup = getGroups();
}
private ArrayList getGroups() {
try {
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");
System.out.println("name : " + name);
System.out.println("type : " + type);
}}
}
} catch (Exception e) {
}
return null;
}
private void MainActivity() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
String sURL = "huhhadjdhjshdjks.com";
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();
}
}
}
我得到以下输出:
logcat:
01-31 17:45:59.330: I/System.out(1871): root element is :node
01-31 17:45:59.340: I/System.out(1871): name : View
01-31 17:45:59.360: I/System.out(1871): type : Page
01-31 17:45:59.380: I/System.out(1871): name : Organisational Structure
01-31 17:45:59.380: I/System.out(1871): type : Page
01-31 17:45:59.400: I/System.out(1871): name : Experience
01-31 17:45:59.400: I/System.out(1871): type : Page
01-31 17:45:59.400: I/System.out(1871): name : Expertise
01-31 17:45:59.400: I/System.out(1871): type : Language
01-31 17:45:59.400: I/System.out(1871): name : Flex
01-31 17:45:59.410: I/System.out(1871): type : Language
01-31 17:45:59.410: I/System.out(1871): name : Android
01-31 17:45:59.420: I/System.out(1871): type : Language
01-31 17:45:59.420: I/System.out(1871): name : Java
01-31 17:45:59.430: I/System.out(1871): type : Language
01-31 17:45:59.430: I/System.out(1871): name : Project
01-31 17:45:59.430: I/System.out(1871): type : Project
01-31 17:45:59.440: I/System.out(1871): name : Sabre
01-31 17:45:59.440: I/System.out(1871): type : Project
01-31 17:45:59.440: I/System.out(1871): name : Android Project
01-31 17:45:59.450: I/System.out(1871): type : Project
01-31 17:45:59.450: I/System.out(1871): name : Media Framework
01-31 17:45:59.460: I/System.out(1871): type : Project
01-31 17:45:59.460: I/System.out(1871): name : CCD
01-31 17:45:59.460: I/System.out(1871): type : Project
01-31 17:45:59.460: I/System.out(1871): name : Redbus
01-31 17:45:59.460: I/System.out(1871): type : Project
01-31 17:45:59.460: I/System.out(1871): name : India Today
01-31 17:45:59.480: I/System.out(1871): type : Project
01-31 17:45:59.480: I/System.out(1871): name : Money Control
01-31 17:45:59.489: I/System.out(1871): type : Project
01-31 17:45:59.489: I/System.out(1871): name : Reorganize
01-31 17:45:59.500: I/System.out(1871): type : Action
01-31 17:45:59.500: I/System.out(1871): name : Add New Employee
01-31 17:45:59.500: I/System.out(1871): type : Action
01-31 17:45:59.500: I/System.out(1871): name : Dir
01-31 17:45:59.500: I/System.out(1871): type : Page
01-31 17:45:59.510: I/System.out(1871): name : P.M
01-31 17:45:59.510: I/System.out(1871): type : Page
01-31 17:45:59.510: I/System.out(1871): name : S.E
01-31 17:45:59.530: I/System.out(1871): type : Page
01-31 17:45:59.530: I/System.out(1871): name : JUN
01-31 17:45:59.530: I/System.out(1871): type : Page
01-31 17:45:59.530: I/System.out(1871): name : EMP
01-31 17:45:59.530: I/System.out(1871): type : Page
01-31 17:45:59.530: I/System.out(1871): name : Add Project Members
01-31 17:45:59.530: I/System.out(1871): type : Action
我需要所有名字按升序排列。 有什么帮助吗?
答案 0 :(得分:1)
创建变量items
以存储name
和type
:
Map<String, String> items = new HashMap<>();
items.put(name, type);
排序names
:
List<String> keys = new ArrayList(items.keySet());
Collections.sort(keys);
然后输出:
for (String key : keys) {
System.out.println("name : " + key);
System.out.println("type : " + items.get(key));
}
答案 1 :(得分:0)
将其加载到数组中并对其进行排序。 XML以两种方式读取(通常)。
1。)你构建了一个树(DOM),所以它不是加入顺序或任何东西。您可以执行树遍历并获得结果排序。
2.。)SAX就像解析标签一样,需要做一些处理,但在阅读文档时占用的内存却少得多。
答案 2 :(得分:0)
这是一个递归元素分类器。不幸的是,它只适用于XOM Elements。
private void sortElement(Element e) {
// Remove them all.
Nodes nodes = e.removeChildren();
// Sort them.
Map<String, List<Element>> sorted = new TreeMap<String, List<Element>>();
for (int i = 0; i < nodes.size(); i++) {
Node n = nodes.get(i);
if (n instanceof Element) {
Element it = (Element) n;
// Get it sorted.
sortElement(it);
// Key it by name and then its xml.
String key = it.getLocalName() + "!" + it.toXML();
// Add it.
List<Element> in = sorted.get(key);
if (in == null) {
in = new ArrayList<Element>();
}
in.add(it);
sorted.put(key, in);
}
}
// Add them all back in.
for (Entry<String, List<Element>> entry : sorted.entrySet()) {
for (Element a : entry.getValue()) {
e.appendChild(a);
}
}
}
答案 3 :(得分:0)
在这里,您可以找到static method sortNodes按任何属性
对NodeList进行排序简单的XML示例:
*** Sort by id ***
1 : 100.00
2 : 5.10
3 : 29.99
*** Sort by price ***
2 : 5.10
3 : 29.99
1 : 100.00
希望它有所帮助。