XML处理专题

enshiwzw 2008-09-24 10:16:47
RT。XML处理专题。用JAVA处理XML,都有哪些经典做法?
我现在处理的方式有
引用开源的API,如JDOM DOM4J 等。
用SUN,IBM自带的API进行处理 结合 XPATH XQUERY。
大家有什么好招,SHOW SHOW。
希望大家从中受益。
...全文
161 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
nemocarl 2008-09-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ZhMilo 的回复:]
我见过一个特殊的方法,就是用Antlr来定义自己的xml的语法规则,然后生成一个语法树来解析。
[/Quote]
这个我也听说过,不过没有见过实例...
anyway,帮顶~~
enshiwzw 2008-09-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ZhMilo 的回复:]
我见过一个特殊的方法,就是用Antlr来定义自己的xml的语法规则,然后生成一个语法树来解析。
[/Quote]
这儿有个例子,大家可以看看
http://blog.163.com/quentin_wjb/blog/static/47269761200871414935890/
谢谢楼上的。
milo-higgs 2008-09-25
  • 打赏
  • 举报
回复
我见过一个特殊的方法,就是用Antlr来定义自己的xml的语法规则,然后生成一个语法树来解析。
enshiwzw 2008-09-25
  • 打赏
  • 举报
回复
有见解的写点东西。此贴沉了就离开CSDN。
enshiwzw 2008-09-24
  • 打赏
  • 举报
回复
大家接力吧,好的经典做法帮忙跟下来:代码,对应XML,外部引用库,JAVA版本特别要求。
enshiwzw 2008-09-24
  • 打赏
  • 举报
回复
上面用的是XPATH处理,下面来个用JAXB处理XML到POJO然后POJO到XML的,这一步可更新也可以创建,可生成文件也可以产生流,以便于处理。

自己写的代码如下:
package demo.jaxb.basic;

import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class DemoTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//将XSD反编组成POJO后,相应的类存放的包路径就是下面
//JAXB的JAXBContext上下文。
JAXBContext jc = JAXBContext.newInstance("demo.jaxb.basic");
Unmarshaller un = jc.createUnmarshaller();
User user = (User) un.unmarshal(new File("user.xml"));
System.out.println(user.getUserAge());
System.out.println(user.getUserID());
System.out.println(user.getUserName());
user.setUserAge(100);
user.setUserID("eric");
user.setUserName("ericwang");
Marshaller ma = jc.createMarshaller();
ma.marshal(user, new File("enshiwzw.xml"));

//use the ObjectFactory to create an object and xml
ObjectFactory of = new ObjectFactory();
User user1 = of.createUser();
user1.setUserAge(26);
user1.setUserID("nma");
user1.setUserName("张某人");

String name = user1.getUserName();
byte b[]=name.getBytes();
// name = new String(b,"");
user.setUserName(name);
System.out.println(name);
ma.marshal(user1, new File("enshiwzw1.xml"));
ma.marshal(user1, System.out);
//

//

//
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}
生成的代码如下:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-646
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2008.09.22 at 03:37:42 下午 CST
//


package demo.jaxb.basic;

import javax.xml.bind.annotation.XmlRegistry;


/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the generated package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {


/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated
*
*/
public ObjectFactory() {
}

/**
* Create an instance of {@link User }
*
*/
public User createUser() {
return new User();
}

}
如下:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-646
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2008.09.22 at 03:37:42 下午 CST
//


package demo.jaxb.basic;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="userID" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="userName" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="userAge" type="{http://www.w3.org/2001/XMLSchema}int"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"userID",
"userName",
"userAge"
})
@XmlRootElement(name = "User")
public class User {

@XmlElement(required = true)
protected String userID;
@XmlElement(required = true)
protected String userName;
protected int userAge;

/**
* Gets the value of the userID property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getUserID() {
return userID;
}

/**
* Sets the value of the userID property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUserID(String value) {
this.userID = value;
}

/**
* Gets the value of the userName property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getUserName() {
return userName;
}

/**
* Sets the value of the userName property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUserName(String value) {
this.userName = value;
}

/**
* Gets the value of the userAge property.
*
*/
public int getUserAge() {
return userAge;
}

/**
* Sets the value of the userAge property.
*
*/
public void setUserAge(int value) {
this.userAge = value;
}

}
使用的JAXB2.0.5
enshiwzw 2008-09-24
  • 打赏
  • 举报
回复
import java.io.IOException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

public class XPathExample {

public static void main(String[] args)
throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException {

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("books.xml");

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr
= xpath.compile("//*/text()");

Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
System.out.print("String&&&&&&&&&&&&&&&&&&");
String str = expr.evaluate(doc);
System.out.println(str);

}

}



对应的XML books.xml


<SMSMSG>
<SMS_DXID>12345435fdfdg3234</SMS_DXID>
<SMS_DATE>2008-08-17</SMS_DATE>
<SMS_SJH>13820372222</SMS_SJH>
<SMS_NR>IWJ0*111101*070515</SMS_NR>
<SMS_TFID>6205</SMS_TFID>
<SMS_GSBZ>06</SMS_GSBZ>
</SMSMSG>
yeah920 2008-09-24
  • 打赏
  • 举报
回复
帮忙顶一下。
enshiwzw 2008-09-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 termite 的回复:]
俺们用的是Axis和XFire
[/Quote]
这是做web service的,当然,他们传输数据的格式也是XML的。

从WSDL对WEB SERVICE的描述到信息封闭用到的SOAP协议。

但XML的处理都被屏了。我现在关注XML 的操作 用JAVA及一些与语言平台无关的东西。
termite 2008-09-24
  • 打赏
  • 举报
回复
俺们用的是Axis和XFire

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧