XMLまとめ(二)
29954 ワード
今日は引き続きXMLに関する知識を見て、XMLを解析するいくつかの主流の方法を見てみましょう.DOM解析DOMモード解析XMLは、XMLドキュメント全体を1つのオブジェクトとして処理し、まずドキュメント全体をメモリに読み込みます.ツリーベースの構造で、通常はドキュメント全体をロードし、DOMツリーを構築してから作業を開始する必要があります.DOM方式の長所と短所:a、木全体がメモリにあるため、xmlドキュメントに対してランダムにbにアクセスでき、xmlドキュメントに対して修正操作cを行うことができ、saxよりdomの使用も簡単である.
欠点:a、全体のドキュメントは一度にaを解析しなければならない、全体のドキュメントはすべてメモリをロードする必要があるため、大きいドキュメントのコストはsax解析に比べて高い:ランダムにアクセスしやすい次はDOM解析の例を見てみましょう
2.JDOM
3DOM4j
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の役割を見てみましょう`.
mappingファイル
テストクラス
欠点: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|
"))) {
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());
}
}