document to xml string

1.links.mxl文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<links>
<link>
<text>一维可航</text>
<url newWindow="no">CSDN--一维可航的Blog</url>
<author>一维可航</author>
<date>
<day>2</day>
<month>1</month>
<year>2001</year>
</date>
<description>北京海淀.</description>
</link>
</links>
2.GetXml.java源码:
import java.io.IOException;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
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.xml.sax.SAXException;

public class GetXml
{

    /**
     * @param args
     * @throws IOException
     * @throws SAXException
     */
   
    public static void main(String[] args) {
        try
        {
            String myxmlString = "";
            myxmlString = getString("sunxun");
            System.out.println(myxmlString);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (SAXException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static  String getString(String name) throws IOException, SAXException
    {
        // 根据传过来的参数进行判断并解析XML TO String
        String xmlString = "";
        if (name != null & name.equalsIgnoreCase("sunxun"))
        {

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = null;
            try
            {
                db = dbf.newDocumentBuilder();
            }
            catch (ParserConfigurationException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Document doc = db.parse("links.xml");
            xmlString = toString(doc);
         }

        return xmlString;

    }

    static public String toString(Document document)
    {
        String result = null;

        if (document != null)
        {
            StringWriter strWtr = new StringWriter();
            StreamResult strResult = new StreamResult(strWtr);
            TransformerFactory tfac = TransformerFactory.newInstance();
            try
            {
                Transformer t = tfac.newTransformer();
                t.setOutputProperty(OutputKeys.ENCODING, "gb2312");
                t.setOutputProperty(OutputKeys.INDENT, "yes");
                t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html,// text
                t.setOutputProperty(
                        "{http://xml.apache.org/xslt}indent-amount", "4");
                t.transform(new DOMSource(document.getDocumentElement()),
                        strResult);
            }
            catch (Exception e)
            {
                System.err.println("XML.toString(Document): " + e);
            }
            result = strResult.getWriter().toString();
        }

        return result;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值