4.Attribute类:
- <table width="100%" border="0"> table>
String width = table.getAttributeValue("width");//获得attribute
int border = table.getAttribute("width").getIntValue();
table.setAttribute("vspace", "0");//设置attribute
table.removeAttribute("vspace");// 删除一个或全部attribute
table.getAttributes().clear();
5.命名空间操作
<xhtml:html></xhtml:html>
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xhtml:title>Home Page</xhtml:title>
Namespace xhtml = Namespace.getNamespace("xhtml", "http://www.w3.org/1999/xhtml");
List kids = html.getChildren("title", xhtml);
Element kid = html.getChild("title", xhtml);
kid.addContent(new Element("table", xhtml));
6.XSLT格式转换
使用以下函数可对XSLT转换
最后如果你需要使用w3c的Document则需要转换一下。
public static Document transform(String stylesheet,Document in)
throws JDOMException {
try {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(stylesheet));
JDOMResult out = new JDOMResult();
transformer.transform(new JDOMSource(in), out);
return out.getDeocument();
}
catch (TransformerException e) {
throw new JDOMException("XSLT Trandformation failed", e);
}
}
四.用例:
1、生成xml文档:
- public class WriteXML{
- public void BuildXML() throws Exception {
- Element root,student,number,name,age;
- root = new Element("student-info"); //生成根元素:student-info
- student = new Element("student"); //生成元素:student(number,name,age)
- number = new Element("number");
- name = new Element("name");
- age = new Element("age");
- Document doc = new Document(root); //将根元素植入文档doc中
- number.setText("001");
- name.setText("lnman");
- age.setText("24");
- student.addContent(number);
- student.addContent(name);
- student.addContent(age);
- root.addContent(student);
- Format format = Format.getCompactFormat();
- format.setEncoding("gb2312"); //设置xml文件的字符为gb2312
- format.setIndent(" "); //设置xml文件的缩进为4个空格
- XMLOutputter XMLOut = new XMLOutputter(format);//元素后换行一层元素缩四格
- XMLOut.output(doc, new FileOutputStream("studentinfo.xml"));
- }
- public static void main(String[] args) throws Exception {
- WriteXML w = new WriteXML();
- System.out.println("Now we build an XML document .....");
- w.BuildXML();
- System.out.println("finished!");
- }
- }
生成的xml文档为:
- <?xml version="1.0" encoding="gb2312"?>
- <student-info>
- <student>
- <number>001</number>
- <name>lnman</name>
- <age>24</age>
- </student>
- </student-info>
创建XML文档2:
- public class CreateXML {
- public void Create() {
- try {
- Document doc = new Document();
- ProcessingInstruction pi=new ProcessingInstruction("xml-stylesheet","type="text/xsl" href="test.xsl"");
- doc.addContent(pi);
- Namespace ns = Namespace.getNamespace("http://www.bromon.org" );
- Namespace ns2 = Namespace.getNamespace("other", "http://www.w3c.org" );
- Element root = new Element("根元素", ns);
- root.addNamespaceDeclaration(ns2);
- doc.setRootElement(root);
- Element el1 = new Element("元素一");
- el1.setAttribute("属性", "属性一");
- Text text1=new Text("元素值");
- Element em = new Element("元素二").addContent("第二个元素");
- el1.addContent(text1);
- el1.addContent(em);
- Element el2 = new Element("元素三").addContent("第三个元素");
- root.addContent(el1);
- root.addContent(el2);
- //缩进四个空格,自动换行,gb2312编码
- XMLOutputter outputter = new XMLOutputter(" ", true,"GB2312");
- outputter.output(doc, new FileWriter("test.xml"));
- }catch(Exception e) {
- System.out.println(e);
- }
- }
- public static void main(String args[]) {
- new CreateXML().Create();
- }
- }