XML笔记.docx
- 文档编号:26174913
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:15
- 大小:396.23KB
XML笔记.docx
《XML笔记.docx》由会员分享,可在线阅读,更多相关《XML笔记.docx(15页珍藏版)》请在冰豆网上搜索。
XML笔记
XML学习笔记
一.DOC方式处理XML文件
1.1XML文件的解析
准备解析的XML文件:
xmlversion="1.0"encoding="UTF-8"standalone="no"?
>
具体解析代码:
publicstaticvoidmain(String[]args)throwsException
{
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder=factory.newDocumentBuilder();
Documentdoc=builder.parse(newFile("D:
"+File.separator+"area.xml"));
NodeListnl=doc.getElementsByTagName("info");
for(inti=0;i { Elemente=(Element)nl.item(i); System.out.println("comment: "+e.getElementsByTagName("comment").item(0).getFirstChild().getNodeValue()); System.out.println("entry: "+e.getElementsByTagName("entry").item(0).getFirstChild().getNodeValue()); } } 1.1XML文件的生成 生成XML文件 publicstaticvoidmain(String[]args)throwsException { DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance(); DocumentBuilderbuilder=factory.newDocumentBuilder(); Documentdoc=builder.newDocument();//新建文档对象 //创建节点 Elementinfo=doc.createElement("info"); Elementname=doc.createElement("name"); Elementage=doc.createElement("age"); //建立节点关系 name.appendChild(doc.createTextNode("Liu")); age.appendChild(doc.createTextNode("21")); info.appendChild(name); info.appendChild(age); doc.appendChild(info); //输出XML文件 TransformerFactorytf=TransformerFactory.newInstance(); Transformert=tf.newTransformer(); t.setOutputProperty(OutputKeys.ENCODING,"UTF-8"); DOMSourcesource=newDOMSource(doc); StreamResultresult=newStreamResult(newFile("D: "+File.separator+"info.xml")); t.transform(source,result); } 生成的XML文件: xmlversion="1.0"encoding="UTF-8"? > 二.XML文件处理方式SAX 1.1SAX只能读取XML文件 1.XML文件 xmlversion="1.0"encoding="UTF-8"? > 2.首先创建解析的SAX类 importjavax.xml.parsers.SAXParser; importjavax.xml.parsers.SAXParserFactory; importorg.xml.sax.Attributes; importorg.xml.sax.SAXException; importorg.xml.sax.helpers.DefaultHandler; classMySaxextendsDefaultHandler { publicvoidstartDocument()throwsSAXException { System.out.println(" xmlversion=\"1.0\"encoding=\"UTF-8\">"); } publicvoidstartElement(Stringurl,StringlocalName,StringqName,Attributesattributes)throwsSAXException { System.out.print("<"+qName); //设置属性 for(inti=0;i { System.out.print(""+attributes.getQName(i)+"=\""+attributes.getValue(i)+"\""); } System.out.print(">"); } publicvoidcharacters(char[]ch,intstart,intlength)throwsSAXException { System.out.print(newString(ch,start,length)); } publicvoidendElement(Stringurl,StringlocalName,StringqName)throwsSAXException { System.out.print(""+qName+">"); } publicvoidendDocument()throwsSAXException { System.out.println("\n===End===="); } } 1.2解析 publicclassXMLTest{ publicstaticvoidmain(String[]args)throwsException { SAXParserFactoryspfactory=SAXParserFactory.newInstance(); SAXParsersp=spfactory.newSAXParser(); sp.parse(newFile("D: "+File.separator+"info.xml"),newMySax()); } } 解析后XML文件显示结果: xmlversion="1.0"encoding="UTF-8"> ===End==== 三.JDOC处理XML文件 首先到www.jdom.org上下载jdom.jar文件。 然后将jdom.jar文件导入到项目中。 也可以将存放jar文件路径添加到ClassPath变量中。 路径格式: D: \Codes\workspace\MyProgram\jdom-2.0.5.jar 3.1JDOC生成XML文件 具体代码: importjava.io.File; importjava.io.FileOutputStream; importorg.jdom2.Attribute; importorg.jdom2.Document; importorg.jdom2.Element; importorg.jdom2.output.XMLOutputter; publicclassXMLTest{ publicstaticvoidmain(String[]args)throwsException { Elementinfo=newElement("info"); Elementname=newElement("name"); Elementage=newElement("age"); Attributeid=newAttribute("id","ye"); Documentdoc=newDocument(info); name.setText("Stream"); name.setAttribute(id); age.setText("21"); info.addContent(name); info.addContent(age); XMLOutputterout=newXMLOutputter(); out.setFormat(out.getFormat().setEncoding("gbk")); out.output(doc,newFileOutputStream("D: "+File.separator+"info_jdom.xml")); } } 生成的XML文件 xmlversion="1.0"encoding="GBK"? > 3.2JDOC读取XML文件 读取以上例子生成的XML文件 publicstaticvoidmain(String[]args)throwsException { SAXBuilderbuilder=newSAXBuilder(); Documentdoc=builder.build("D: "+File.separator+"info_jdom.xml"); Elementroot=doc.getRootElement(); List for(inti=0;i { Elemente=list.get(i); System.out.println(e.getName()+": "+e.getValue()); } } 递归读取XML中节点信息 publicstaticvoidprint(Elemente,inti) { StringBuffers=newStringBuffer(); for(intx=0;x { s.append(""); } List childlist=e.getChildren(); if(childlist.size()>0) { i++; System.out.print(s.toString()+e.getName()+""); List for(intj=0;j { Attributea=att.get(j); System.out.print(a.getName()+"="+a.getValue()); } System.out.println(": "); for(intj=0;j { Elementce=childlist.get(j); print(ce,i); } } else { System.out.print(s.toString()+e.getName()+""); List for(intj=0;j { Attributea=att.get(j); System.out.print(a.getName()+"="+a.getValue()); } System.out.println(": "+e.getValue()); } } 4.DOM4J处理XML文件 首先要下载dom4j-1.6.1.jar文件。 在网站中标记的位置输入dom4j,搜索。 下载,导入到项目中。 4.1XML文件的生成 importjava.io.File; importjava.io.FileOutputStream; importorg.dom4j.Document; importorg.dom4j.DocumentHelper; importorg.dom4j.Element; importorg.dom4j.io.OutputFormat; importorg.dom4j.io.XMLWriter; publicclassXMLTest{ publicstaticvoidmain(String[]args)throwsException { Documentdoc=DocumentHelper.createDocument(); Elementinfo=doc.addElement("info"); Elementname=info.addElement("name"); Elementage=info.addElement("age"); name.setText("Hi"); age.setText("21"); OutputFormatformat=newOutputFormat(); format.setEncoding("UTF-8"); XMLWriterwrite=newXMLWriter(newFileOutputStream( newFile("D: "+File.separator+"info_dom4j.xml")),format); write.write(doc); write.close(); } } 生成的XML文件: xmlversion="1.0"encoding="UTF-8"? > 4.2DOM4J解析XML文件 解析代码: importjava.io.File; importjava.util.Iterator; importorg.dom4j.Document; importorg.dom4j.Element; importorg.dom4j.io.SAXReader; publicclassXMLTest{ publicstaticvoidmain(String[]args)throwsException { Filefile=newFile("D: "+File.separator+"info_dom4j.xml"); SAXReaderreader=newSAXReader(); Documentdoc=reader.read(file); Elementroot=doc.getRootElement(); Iteratoriter=root.elementIterator(); while(iter.hasNext()) { Elemente=(Element)iter.next(); System.out.println(e.getName()+": "+e.getText()); } } } 5.JavaScript操作XML文件 代码: DOCTYPEhtml> //www.w3.org/1999/xhtml"xml: lang="en"> functionfunc() { varid=newArray(1,2,3); varvalue=newArray("北京","上海","深圳"); varselect=document.getElementById("area"); select.length=1; select.options[0].selected=true; for(vari=0;i { varoption=document.createElement("option"); option.setAttribute("value",id[i]); option.appendChild(document.createTextNode(value[i])); select.appendChild(option); } }