本人想用java来实现as3中对XML文件的处理方法,
首先得判断该文件是否是XML文件,本人想到了用java中的正则表达式匹配一个简单的XML文件;
XML文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<student>
<id>01</id>
<name>王山虎</name>
<age>22</age>
<phone>15824884169</phone>
</student>
再往下就是第一句<?xml version="1.0" encoding="UTF-8"?>本人在用正则表达式匹配的时候发现这个字符串里面有"",最基本的匹配是无论如何也匹配不上了!!甚是郁闷!
请高手们指点一下吧!!
如果有哪们高手对本人所要实现的即as3中对XML的处理方法有新的看法,或者说是可以用其它比较好的办法来实现的话,请给本人指点一下!!
最好是用一些初级的正则表达式的知识解决该问题,请够提供一些例子代码,本人是刚进入编程这个行业,还没有太多的经验,希望高手们讲的简单一些,谢谢!!!
------解决方案--------------------
jdom dom4j
------解决方案--------------------
一般都用工具包,如一楼所说,这些工具会验证xml和不合法的
------解决方案--------------------
我有一个MTracer.exe,228K,若需要,给个Email
------解决方案--------------------
不是,你把XML文件调入后,你可以编写正则表达式,它能即时显示匹配结果,便于你分析正则表达式是否符合要求。它是一个辅助性软件。
------解决方案--------------------
去研究一下dom4j吧.....比较好用....
官方网站 http://www.dom4j.org/
或者 你用java自带的xml解析器,给你一个我自己写的类....效率....就将就着看看也行.....
- Java code
package my.test;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.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;public class uDom { Document doc=null; public uDom(String xmlfile) { DocumentBuilder dbf; try { dbf = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = dbf.parse(xmlfile); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 读取XML文件节点内容 * @param nodeName 节点名称 * @return */ public Object[] readNode(String nodeName) { Object nodeValue[]=null; NodeList nodelist = doc.getElementsByTagName(nodeName); nodeValue=new Object[nodelist.getLength()]; //System.out.println("getLength:" + nodelist.getLength()); //System.out.println("getNodeName"+"\t"+"getNodeValue"+"\t"+"getAttribute"); for (int i = 0; i < nodelist.getLength(); i++) { Element element =(Element)nodelist.item(i); //System.out.println(element.getNodeName()+"\t"+element.getNodeValue()+"\t"+element.getAttribute("num")); nodeValue[i]=element.getNodeValue(); } return nodeValue; } /** * 读取XML文件节点属性内容 * @param nodeName 节点名称 * @param Attribute 节点属性名称 * @return */ public Object[] readNode(String nodeName,String Attribute) { Object AttrValue[]=null; NodeList nodelist = doc.getElementsByTagName(nodeName); AttrValue=new Object[nodelist.getLength()]; for (int i = 0; i < nodelist.getLength(); i++) { Element element =(Element)nodelist.item(i); //System.out.println(element.getNodeName()+"\t"+element.getNodeValue()+"\t"+element.getAttribute("num")); AttrValue[i]=element.getAttribute(Attribute); } return AttrValue; } public uDom() { // TODO Auto-generated constructor stub try { this.readxml(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void readxml() throws ParserConfigurationException, SAXException, IOException { String nodename = null; short nodetype = 0; String nodevalue = null; DocumentBuilder dbf = DocumentBuilderFactory.newInstance().newDocumentBuilder(); System.out.println("Get DocumentBuilder Successful..."); Document doc = dbf.parse("d:/TestFile/links.xml"); System.out.println("Get Document Successful..."); NodeList nl1 = doc.getElementsByTagName("message"); System.out.println("Get NodeList(links) Successful..."); System.out.println("getLength:" + nl1.getLength()); Node node2 = doc.getFirstChild().getChildNodes().item(1); // Node node3 = doc.getFirstChild(); Node node1 = nl1.item(4); // Node node2 = doc.getFirstChild(); // Node node3 = node2.getFirstChild(); // Node node4 = node3.getFirstChild(); for (int i = 0; i < 2; i++) { System.out.println(node2.getChildNodes().item(i).getNodeName()); System.out.println(node2.getChildNodes().item(i).getNodeValue()); } // System.out.println(node2.getFirstChild().getNextSibling().getNodeName()); System.exit(0); nodename = node1.getFirstChild().getNodeName(); nodetype = node1.getFirstChild().getNodeType(); nodevalue = node1.getFirstChild().getNodeValue(); System.out.println("getNodeName:" + nodename); System.out.println("getNodeType:" + nodetype); System.out.println("getNodeValue:" + nodevalue); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub uDom udom = new uDom("xml/test.XML"); System.out.println("读取XML文件成功~~"); Object[] temp=udom.readNode("root","num"); System.out.println("获得节点长度为:"+temp.length); for(int i=0;i<temp.length;i++) { //temp[i]可能会得到空指针异常 System.out.println(i+" Value: "+temp[i].toString()); } }}