XML总结(二)

来源:互联网 发布:纹身图案软件 编辑:程序博客网 时间:2024/06/04 20:57

今天我们继续看XML相关知识
我们来看下解析XML的几种主流方法
1.DOM解析
DOM模式解析XML,是把整个XML文档当成一个对象来处理,会先把整个文档读入到内存里。是基于树的结构,通常需要加载整文档和构造DOM树,然后才能开始工作。
DOM方式的优缺点:
优点:a、由于整棵树在内存中,因此可以对xml文档随机访问b、可以对xml文档进行修改操作c、较sax,dom使用也更简单。

缺点:a、整个文档必须一次性解析完a、由于整个文档都需要载入内存,对于大文档成本高
相较于sax解析:容易随机访问
接下来看看DOM解析的实例

<?xml version="1.0" encoding="UTF-8"?><nba><player team="lakers"><name>kobe</name><age>39</age><salary>300milion</salary></player></nba>
package wangcc.xml;public interface DealWithXml {    public void parseXml(String filename);}
package wangcc.xml;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;/** * @ClassName: DealWithXmlByDom * @Description: TODO(这里用一句话描述这个类的作用) * @author wangcc * @date 2017-6-6 上午11:39:28 *       用DOM的好处优点就是整个文档都一直在内存中,我们可以随时访问任何节点,并且对树的遍历也是比较熟悉的操作 *  *       当成一棵树 */public class DealWithXmlByDom implements DealWithXml {    private Document dom;    private Element root;    /*     * (非 Javadoc) <p>Title: parseXml</p> <p>Description: </p>     * 获取文档对象:DocumentBuilderFactory → DocumentBuilder → Document     *      * @param filename     *      * @see wangcc.xml.DealWithXml#parseXml(java.lang.String)     */    @Override    public void parseXml(String filename) {        // TODO Auto-generated method stub        // 获得DocumentBuilderFactory        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();        // 除去空格        factory.setIgnoringElementContentWhitespace(true);        DocumentBuilder db;        try {            db = factory.newDocumentBuilder();            dom = db.parse(filename);            // 得到根元素            System.out.println("src\\Start");            root = dom.getDocumentElement();            list(root);        } catch (Exception e) {            // TODO: handle exception        }    }    private void list(Node node) {        if (node.getNodeType() == node.ELEMENT_NODE) {            System.out.println(node.getNodeName());        }        if (node.getNodeType() == node.TEXT_NODE                && (!node.getTextContent().matches("\\s*|\t|\r|\n"))) {            System.out.println(node.getNodeName());            System.out.println(node.getTextContent());        }        NodeList nodeList = node.getChildNodes();        for (int i = 0; i < nodeList.getLength(); i++) {            Node n = nodeList.item(i);            list(n);        }    }    public void read() {        NodeList nodeList = dom.getElementsByTagName("player");        Element e = (Element) nodeList.item(0);        System.out.println("TEAM:" + e.getAttribute("team"));        Element name = (Element) e.getElementsByTagName("name").item(0);        System.out.println(name.getTextContent());    }    // 此外,还可以修改,删除,增加等    public void add() throws Exception {        Element player = dom.createElement("player");        player.setAttribute("team", "WARRIORS");        Element name = dom.createElement("name");        name.setTextContent("DURANT");        Element age = dom.createElement("age");        age.setTextContent("28");        Element salary = dom.createElement("salary");        salary.setTextContent("300milion");        player.appendChild(age);        player.appendChild(salary);        player.appendChild(name);        root.appendChild(player);        TransformerFactory factory = TransformerFactory.newInstance();        Transformer tf = factory.newTransformer();        tf.transform(new DOMSource(dom), new StreamResult("src\\NBA.xml"));    }}

2.JDOM

package wangcc.xml;import java.util.List;import org.jdom.Document;import org.jdom.Element;import org.jdom.input.SAXBuilder;public class DealWithXmlByJDom implements DealWithXml {    @Override    public void parseXml(String filename) {        // TODO Auto-generated method stub        SAXBuilder builder = new SAXBuilder();        try {            Document dom = builder.build(filename);            Element nba = dom.getRootElement();            List playerList = nba.getChildren("player");            for (int i = 0; i < playerList.size(); i++) {                Element player = (Element) playerList.get(i);                List playerInfo = player.getChildren();                for (int j = 0; j < playerInfo.size(); j++) {                    System.out.println(((Element) playerInfo.get(j)).getName());                    System.out                            .println(((Element) playerInfo.get(j)).getValue());                }            }        } catch (Exception e) {            // TODO: handle exception        }    }}

3DOM4j

package wangcc.xml;import java.io.File;import java.util.Iterator;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class DealWithXmlByDom4j implements DealWithXml {    @Override    public void parseXml(String filename) {        // TODO Auto-generated method stub        File inputXml = new File(filename);        SAXReader saxReader = new SAXReader();        try {            Document document = saxReader.read(inputXml);            Element users = document.getRootElement();            for (Iterator i = users.elementIterator(); i.hasNext();) {                Element user = (Element) i.next();                for (Iterator j = user.elementIterator(); j.hasNext();) {                    Element node = (Element) j.next();                    System.out.println(node.getName() + ":" + node.getText());                }                System.out.println();            }        } catch (DocumentException e) {            System.out.println(e.getMessage());        }    }}

4.SAX

二.Castor组件
Castor
数据绑定
marshal :直译为“编排”,在计算机中特指将数据按某种描述格式编排出来,通常来说一般是从非文本格式到文本格式的数据转化。
unmarshal

官方解释:
For those not familiar with the terms “marshal” and “unmarshal”, it’s simply the act of converting a stream
(sequence of bytes) of data to and from an Object. The act of “marshalling” consists of converting an Object to
a stream, and “unmarshalling” from a stream to an Object.

我们需要重点关注下auto-complete属性,看看他的作用
auto-complete 属性
If true, the class will be introspected to determine its
field and the fields specified in the mapping file will
be used to overide the fields found during the
introspection.

The auto-complete attributes is interesting as it allow a fine degree of control of the introspector: it is possible
to specifiy only the fields whose Castor default behavior does not suite our needs. These feature should
simplify the handling of complexe class containing many fields. Please see below for an example usage of this
attribute.

map-to 属性
Used if the name of the element is not the name of the
class. By default, Castor will infer the name of the
element to be mapped from the name of the class: a
Java class named ‘XxxYyy’ will be transformed in
‘xxx-yyy’. If you don’t want Castor to generate the
name, you need to use to specify the name
you want to use. is only used for the root
element.
我们来看看auto-complete的作用`

package wangcc.castor;import java.io.IOException;import java.io.StringReader;import java.io.StringWriter;import java.io.Writer;import org.exolab.castor.mapping.Mapping;import org.exolab.castor.mapping.MappingException;import org.exolab.castor.xml.Marshaller;import org.exolab.castor.xml.Unmarshaller;public class CastorUtil<T> {    // public static Mapping mapping = null;    private String url = null;    private Mapping mapping = null;    public CastorUtil(String url) {        this.url = url;        mapping = new Mapping();        try {            mapping.loadMapping(url);        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (MappingException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    public T unmarsharl(String xml) throws Exception {        if (mapping == null) {            mapping = new Mapping();            mapping.loadMapping(url);        }        Unmarshaller umsa = new Unmarshaller(mapping);        T obj = (T) umsa.unmarshal(new StringReader(xml));        return obj;    }    public String marsharl(T obj) throws Exception {        String returnXml = "";        if (obj != null) {            Writer writer = new StringWriter();            Marshaller marshaller = new Marshaller(writer);            if (mapping == null) {                mapping = new Mapping();                mapping.loadMapping(url);            }            marshaller.setMapping(mapping);            marshaller.marshal(obj);            returnXml = writer.toString();        }        return returnXml;    }}
package wangcc.castor;public class Constants {    // 应该从属性文件中读取    public final static String ORDERITEM_URL = "src\\mapping.xml";}
package wangcc.castor;public class OrderItem {    private Integer orderQuantity;    private String teamName;    public String getTeamName() {        return teamName;    }    public void setTeamName(String teamName) {        this.teamName = teamName;    }    private String id;    @Override    public String toString() {        return "OrderItem [orderQuantity=" + orderQuantity + ", id=" + id + "]";    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public Integer getOrderQuantity() {        return orderQuantity;    }    public void setOrderQuantity(Integer orderQuantity) {        this.orderQuantity = orderQuantity;    }}

mapping文件

<?xml version="1.0" encoding="UTF-8"?><mapping><class name="wangcc.castor.OrderItem" auto-complete="true" ><map-to xml="item"></map-to><field name="id" type="string"><bind-xml name="identity" node="attribute"></bind-xml></field><!-- <field name="orderQuantity" type="integer"><bind-xml name="order-quantity" node="element"></bind-xml></field> --></class></mapping>
package wangcc.castor;import java.io.BufferedReader;import java.io.FileReader;public class XmlToStringUtil {    public static String getString(String filename) throws Exception {        BufferedReader br = new BufferedReader(new FileReader(filename));        String line = "";        StringBuffer sb = new StringBuffer();        while ((line = br.readLine()) != null) {            if (!line.contains("<!")) {                sb.append(line);            }        }        return sb.toString();    }}

测试类

package wangcc.castor;import org.junit.Test;public class TestCastor {    @Test    public void testString() throws Exception {        String s = XmlToStringUtil.getString(Constants.ORDERITEM_URL);        System.out.println(s);    }    // 如果没有自动补全 auto-complete="true" 则只会记录一个绑定的数据    // 得到<?xml version="1.0" encoding="UTF-8"?>    // <item identity="3"/>    // 如果有自动补全    /*     * <?xml version="1.0" encoding="UTF-8"?> <item     * identity="3"><order-quantity>     * 12</order-quantity><team-name>BULLS</team-name></item> 根据get     * set方法的名称,以驼峰命名法来说 如:orderQuantity 变成order-quantity     */    @Test    public void testmarsharl() throws Exception {        OrderItem orderitem = new OrderItem();        orderitem.setId("3");        orderitem.setOrderQuantity(new Integer(12));        orderitem.setTeamName("BULLS");        CastorUtil<OrderItem> cUtil = new CastorUtil<OrderItem>(                Constants.ORDERITEM_URL);        String xml = cUtil.marsharl(orderitem);        System.out.println(xml);    }    // auto-complete="true" 没有auto-complete="true" 报错    @Test    public void testunmarsharl() throws Exception {        CastorUtil<OrderItem> cUtil = new CastorUtil<OrderItem>(                Constants.ORDERITEM_URL);        String xml = XmlToStringUtil.getString("src\\OrderItem.xml");        OrderItem orderitem = cUtil.unmarsharl(xml);        System.out.println(orderitem.toString());    }    public static void main(String[] args) throws Exception {        CastorUtil<OrderItem> cUtil = new CastorUtil<OrderItem>(                Constants.ORDERITEM_URL);        String xml = XmlToStringUtil.getString("src\\OrderItem.xml");        OrderItem orderitem = cUtil.unmarsharl(xml);        System.out.println(orderitem.toString());    }}
原创粉丝点击