{
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">
Liu21
===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"?
>
Stream
21
3.2JDOC读取XML文件
读取以上例子生成的XML文件
publicstaticvoidmain(String[]args)throwsException
{
SAXBuilderbuilder=newSAXBuilder();
Documentdoc=builder.build("D:
"+File.separator+"info_jdom.xml");
Elementroot=doc.getRootElement();
Listlist=root.getChildren();
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("");
}
Listchildlist=null;
childlist=e.getChildren();
if(childlist.size()>0)
{
i++;
System.out.print(s.toString()+e.getName()+"");
Listatt=e.getAttributes();
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()+"");
Listatt=e.getAttributes();
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"?
>
Hi
21
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">
JavaScriptTest
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);
}
}
没有地区