前言:
而今姐妹们对“js 解析xml”可能比较关怀,大家都想要分析一些“js 解析xml”的相关文章。那么小编在网摘上搜集了一些对于“js 解析xml””的相关内容,希望我们能喜欢,兄弟们一起来了解一下吧!java中比较常见的xml解析主要包括SAXParse、DomParse和Jdom三种,下面介绍具体使用方法。
一、DomParse解析
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DomParse {
public static void main(String[] args) throws Exception
{
//获取工厂
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//解析xml
Document doc = db.parse("src/com/sxt/day21/xml_base/ThreeKing.xml");
//根据名字获取所有的子标签集合
NodeList nl = doc.getElementsByTagName("hero");
for(int i = 0;i < nl.getLength();i++){
Node node = nl.item(i);
Element ele = (Element)node;
System.out.println(ele.getAttribute("name"));
System.out.println(ele.getTextContent());
}
}
}
二、SAXParse解析
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SaxParse {
/*xml的解析:
*1.sax解析:
*优点:解析效率高
*缺点:解析步骤麻烦
*2.dom解析:
*优点:解析步骤明确简单
*缺点:浪费效率
*
*/
public static void main(String[] args) throws Exception
{
saxParse();
}
//sax解析
public static void saxParse() throws Exception{
//获取工厂对象
SAXParserFactory spf = SAXParserFactory.newInstance();
//获取解析器对象
SAXParser sp = spf.newSAXParser();
//解析
MyHandler mh = new MyHandler();
sp.parse("src/com/sxt/day21/xml_base/ThreeKing.xml", mh);
List<Hero> list = mh.list;
for (Hero hero : list)
{
System.out.println(hero);
}
}
}
class MyHandler extends DefaultHandler{
List<Hero> list = new ArrayList<Hero>();
Hero hero;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
if("hero".equals(qName)){
String name = attributes.getValue("name");
String weapon = attributes.getValue("weapon");
hero = new Hero(name,weapon,null);
list.add(hero);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
if(hero!=null){
String str = new String(ch,start,length);//获取xml中的文本
hero.detail = str.trim();
hero = null;//在当前标签处理完成后,让hero不再指向当前标签的对象
}
}
}
class Hero{
String name;
String weapon;
String detail;
public Hero(String name, String weapon, String detail)
{
super();
this.name = name;
this.weapon = weapon;
this.detail = detail;
}
@Override
public String toString()
{
return "Hero [name=" + name + ", weapon=" + weapon + ", detail=" + detail + "]";
}
}
三、Jdom解析(属于第三方插件,需要导入jdom.jar)
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class Jdom2Use {
public static void main(String[] args) throws Exception
{
write();
}
//使用jdom2解析xml
public static void parse() throws Exception{
//生成解析器对象
SAXBuilder sb = new SAXBuilder();
//解析xml
Document doc = sb.build("src/com/sxt/day21/xml_base/ThreeKing.xml");
//获取跟标签
Element ele = doc.getRootElement();
//获取所有子标签的集合
List<Element> list = ele.getChildren();//获取所有country的标签对象
for (Element element : list)
{
List<Element> child = element.getChildren();//获取当前country下的所有hero标签对象
for (Element element2 : child)
{
System.out.println(element2.getAttributeValue("name"));
System.out.println(element2.getText());
}
}
}
//写入xml
public static void write() throws IOException{
//建立Document对象
Document doc = new Document();
//指定根标签
Element root = new Element("DogInfo");
doc.setRootElement(root);
//建立其他标签并嵌入根标签
for (int i = 1; i < 4; i++)
{
Element ele = new Element("dog");
ele.setAttribute("name","dog"+i);
ele.setAttribute("age",i+2+"");
ele.setText("汪汪汪,是第"+i+"只");
root.addContent(ele);
}
//写出xml
XMLOutputter xo = new XMLOutputter(Format.getPrettyFormat());
OutputStream os = new FileOutputStream("src/com/sxt/day21/jdom_stu/dog.xml");
xo.output(doc,os);
os.close();
}
}
对比总结:其中DomParse和SAXParse是jdk自带的解析方式,Jdom是第三方提供的插件。DomParse会解析所有的xml元素,而SAXParse可以通过重写DefaultHandler实现自己解析功能,因此效率较高。而Jdom的强大之处在于它不仅可以解析xml,还可以向xml中写入数据。
标签: #js 解析xml #javaxml类 #xml解析java #java sax解析xml文件 #java获取xml