package au.edu.qut.yawl.unmarshal;/**
* <p>Title: Pretty XML</p>
*
* <p>Description: Pretty XML</p>
*
* <p>Copyright: Copyright (c) 2004</p>
*
* <p>Company: </p>
*
* @author YuLimin
* @version 1.0
*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;public class PrettyXML
{
private String extName = ""; public PrettyXML()
{
} public String getExtName()
{
return extName;
} public void pretty(String filename)
{
} public void pretty(String filename,String strTemp)
{
try
{
// Build the document with SAX and Xerces, no validation
SAXBuilder builder = new SAXBuilder(); // Create the document
Document doc = builder.build(new File(filename)); // Output the document, use Pretty formatter
XMLOutputter fmt = new XMLOutputter();
fmt.setFormat(Format.getPrettyFormat());
fmt.setFormat(Format.getPrettyFormat().setEncoding("GB2312")); //FileOutputStream
OutputStream os = new FileOutputStream(filename + extName);
fmt.output(doc,os);
os.flush();
os.close();
}
catch(Exception e)
{
e.printStackTrace();
}
} public void setExtName(String ExtName)
{
this.extName = ExtName;
} /**
* 这个方法将JDom对象转换字符串.
* @param document 将要被转换的JDom对象
* @param encoding 输出字符串使用的编码
* @return String xml对象保存到的字符串
*/
public static String OutputToString(Document document,String encoding)
{
ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
XMLOutputter docWriter = new XMLOutputter(); //" ",true,encoding); try
{
docWriter.output(document,byteRep);
}
catch(Exception e)
{
e.printStackTrace();
} return byteRep.toString();
} public static void main(String[] args)
{
String filenameIn = ""; //args[0];
filenameIn = "F:\\Java\\YAWL\\取水许可申请审批New.xml"; PrettyXML prettyXML = new PrettyXML();
prettyXML.setExtName("Pretty.xml");
prettyXML.pretty(filenameIn); try
{
// Build the document with SAX and Xerces, no validation
SAXBuilder builder = new SAXBuilder();
// Create the document
Document doc = builder.build(new File(filenameIn));
// doc =builder.build(new java.io.BufferedReader("a"));
prettyXML.accessChildElement(doc);
}
catch(Exception e)
{
e.printStackTrace();
}
} public static void accessChildElement(Document myDocument)
{
Element root = myDocument.getRootElement();
System.out.println(root.getName());
System.out.println(root.getText()); Element child = root.getChild("specification"); //.getChild("specification").getChild("name");
java.util.List lst = root.getChildren();
for(int i = 0;i < lst.size();i++)
{
System.out.println(i + ":" + ((Element)lst.get(i)).getName());
}
if(child != null)
{
System.out.println("Here is the element we found: " + child.getName() + ". Its content: " + child.getText() + "\n");
}
else
{
System.out.println("Something is wrong. We did not find a year Element");
}
}
}
* <p>Title: Pretty XML</p>
*
* <p>Description: Pretty XML</p>
*
* <p>Copyright: Copyright (c) 2004</p>
*
* <p>Company: </p>
*
* @author YuLimin
* @version 1.0
*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;public class PrettyXML
{
private String extName = ""; public PrettyXML()
{
} public String getExtName()
{
return extName;
} public void pretty(String filename)
{
} public void pretty(String filename,String strTemp)
{
try
{
// Build the document with SAX and Xerces, no validation
SAXBuilder builder = new SAXBuilder(); // Create the document
Document doc = builder.build(new File(filename)); // Output the document, use Pretty formatter
XMLOutputter fmt = new XMLOutputter();
fmt.setFormat(Format.getPrettyFormat());
fmt.setFormat(Format.getPrettyFormat().setEncoding("GB2312")); //FileOutputStream
OutputStream os = new FileOutputStream(filename + extName);
fmt.output(doc,os);
os.flush();
os.close();
}
catch(Exception e)
{
e.printStackTrace();
}
} public void setExtName(String ExtName)
{
this.extName = ExtName;
} /**
* 这个方法将JDom对象转换字符串.
* @param document 将要被转换的JDom对象
* @param encoding 输出字符串使用的编码
* @return String xml对象保存到的字符串
*/
public static String OutputToString(Document document,String encoding)
{
ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
XMLOutputter docWriter = new XMLOutputter(); //" ",true,encoding); try
{
docWriter.output(document,byteRep);
}
catch(Exception e)
{
e.printStackTrace();
} return byteRep.toString();
} public static void main(String[] args)
{
String filenameIn = ""; //args[0];
filenameIn = "F:\\Java\\YAWL\\取水许可申请审批New.xml"; PrettyXML prettyXML = new PrettyXML();
prettyXML.setExtName("Pretty.xml");
prettyXML.pretty(filenameIn); try
{
// Build the document with SAX and Xerces, no validation
SAXBuilder builder = new SAXBuilder();
// Create the document
Document doc = builder.build(new File(filenameIn));
// doc =builder.build(new java.io.BufferedReader("a"));
prettyXML.accessChildElement(doc);
}
catch(Exception e)
{
e.printStackTrace();
}
} public static void accessChildElement(Document myDocument)
{
Element root = myDocument.getRootElement();
System.out.println(root.getName());
System.out.println(root.getText()); Element child = root.getChild("specification"); //.getChild("specification").getChild("name");
java.util.List lst = root.getChildren();
for(int i = 0;i < lst.size();i++)
{
System.out.println(i + ":" + ((Element)lst.get(i)).getName());
}
if(child != null)
{
System.out.println("Here is the element we found: " + child.getName() + ". Its content: " + child.getText() + "\n");
}
else
{
System.out.println("Something is wrong. We did not find a year Element");
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货