xml文件为
<?xml version="1.0" encoding="UTF-8"?>
<A>
<ONE single="true">
<aa>aa1</aa>
<bb>bb1</bb>
<cc>cc1</cc>
</ONE>
<TWO single="true">
<aa>aa2</aa>
<bb>bb2</bb>
<cc>cc2</cc>
</TWO>
</A>现在需要些一个方法 getXml(str1,str2)
比如getXml("ONE","aa")返回aa1
getXml("ONE","bb")返回bb1
getXml("TWO","aa")返回bb2请教各位怎么写!
<?xml version="1.0" encoding="UTF-8"?>
<A>
<ONE single="true">
<aa>aa1</aa>
<bb>bb1</bb>
<cc>cc1</cc>
</ONE>
<TWO single="true">
<aa>aa2</aa>
<bb>bb2</bb>
<cc>cc2</cc>
</TWO>
</A>现在需要些一个方法 getXml(str1,str2)
比如getXml("ONE","aa")返回aa1
getXml("ONE","bb")返回bb1
getXml("TWO","aa")返回bb2请教各位怎么写!
解决方案 »
- 今天刚考的c的笔试题
- 把100分成 1 5 10 20 的所有分法,写程序实现所有的方法数
- java执行cmd的问题
- eclipse生成jar
- 一道面试题,考考你.
- 看看这个java Applet有什么问题
- 请问各位大虾有谁知道或做过用java开发的发短信的软件啊?
- 多线程问题!!!
- applet在jbuilder环境下没问题,用ie调用出applet can't be instantiated,什么原因呢?
- 求救,sql语句的中文问题
- 找高手请教ResultSet的rs对象该在何处rs.close()
- java怎样获取进程id ,怎样重起进程,终止进程 小弟在这里24在线等候各位大虾进来指导
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;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.SAXException;public class ParseMSN {
public ParseMSN(String fileName){
if(!fileName.substring(fileName.length() - 4).equals(".xml")){
fileName = fileName + ".xml";
}
File file = new File(fileName);
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
parse(doc);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
public void parse(Document doc){
Element el = doc.getDocumentElement();
NodeList message = el.getElementsByTagName("Message");
NodeList from = el.getElementsByTagName("From");
NodeList to = el.getElementsByTagName("To");
for(int i=0;i<message.getLength();i++){
Node node_m = message.item(i);
Node node_f = from.item(i).getFirstChild();
Node node_t = to.item(i).getFirstChild();
NamedNodeMap attirbutes_m = node_m.getAttributes();
NamedNodeMap attirbutes_f = node_f.getAttributes();
NamedNodeMap attirbutes_t = node_t.getAttributes();
String info = "";
info = info + attirbutes_m.getNamedItem("DateTime").getTextContent() + " ";
info = info + attirbutes_f.getNamedItem("FriendlyName").getTextContent() + " to ";
info = info + attirbutes_t.getNamedItem("FriendlyName").getTextContent() + " ";
System.out.println(info + node_m.getTextContent());
}
}
}
问题中有写错
getXml("TWO","aa")返回aa2
String str=rx.getNodeValue("ONE/aa");
str即是你想要的!
我就是封装的它的那个例子
第二用dom解析也容易啊,把xml读如成documengt用dom操作去你要的内容就行了,也是现成的方法.
具体的看看API就解决了.楼主表偷懒,自己看书解决很有成就感的,具体例子我有写就不给你了:)
重载getXml()方法可以读取任意层次的叶子节点。
第一个参数是最外层的根节点,依次类推。package com.mycompany.xmltest;import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;public class ARead { /*
* Reload this method to get text contents within a leaf of any level
*
* @Return null if there is no such a leaf
*/
public Object getXml(String key1, String key2, String key3) {
HashMap retMap = readXmlFile(new File("F:\\a.xml"));
if (!retMap.isEmpty()) {
Object obj = retMap.get(key1);
if (obj instanceof HashMap) {
retMap = (HashMap) obj;
if (!retMap.isEmpty()) {
obj = retMap.get(key2);
if (obj instanceof HashMap) {
retMap = (HashMap) obj;
if (!retMap.isEmpty()) {
return retMap.get(key3);
}
}
}
}
}
return null;
} private HashMap readXmlFile(File thefile) {
HashMap map = new HashMap(); BufferedReader bfreader = null;
DocumentBuilder docbuilder = null;
Document doc = null; try {
bfreader = new BufferedReader(new FileReader(thefile));
} catch (FileNotFoundException e) {
e.printStackTrace();
} DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
docbuilder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} try {
doc = docbuilder.parse(new InputSource(bfreader));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bfreader.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (doc.hasChildNodes()) {
NodeList nlist_01 = doc.getChildNodes();
parseChild(nlist_01, map);
} return map;
} /*
* Recursive call
*/
private void parseChild(NodeList nlist, HashMap themap) {
for (int i = 0; i < nlist.getLength(); i++) {
Node node = nlist.item(i);
if (node.hasChildNodes()
&& node.getFirstChild().getNextSibling() != null) {
HashMap sonMap = new HashMap();
parseChild(node.getChildNodes(), sonMap);
themap.put(node.getNodeName(), sonMap);
} else {
if (node.getTextContent() != null
&& !"".equals(node.getTextContent())) {
themap.put(node.getNodeName(), node.getTextContent());
}
}
} } public static void main(String[] args) {
ARead test = new ARead();
System.out.println(test.getXml("A", "ONE", "aa"));
System.out.println(test.getXml("A", "ONE", "cc"));
System.out.println(test.getXml("A", "TWO", "bb"));
}
}