疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

非文本的数据对象存储在xml中


 

 以下是将图片,视频等非文本型的数据存储到xml中,再进行解析。
   
    要想非文本的数据存入到xml中的基本思想是,首先创建基本的xml结点,然后将图片等非文本的数据读入,再用sun.misc.BASE64Encoder去编码成String型,读取的时候用sun.misc.BASE64Decoder类去解码成byte型。这两个类在jdk的rt.jar中,不必去下载,jdk的帮助文档是没有这两个类的。代码如下:
   
    import java.io.File;
   
    import java.io.FileInputStream;
   
    import java.io.FileOutputStream;
   
    import java.io.IOException;
   
    import java.io.InputStream;
   
    import java.io.OutputStream;
   
    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.TransformerException;
   
    import javax.xml.transform.TransformerFactory;
   
    import javax.xml.transform.dom.DOMSource;
   
    import javax.xml.transform.stream.StreamResult;
   
    import org.w3c.dom.DOMException;
   
    import org.w3c.dom.Document;
   
    import org.w3c.dom.Element;
   
    import org.w3c.dom.Node;
   
    import org.xml.sax.SAXException;
   
    import sun.misc.BASE64Decoder;
   
    import sun.misc.BASE64Encoder;
   
    public class Test {
   
    public static String getPictrueString(String fileName) throws IOException
   
    {
    [nextpage]
    // 打开图片文件,利用该图片构造一个文件流
   
    InputStream fis = new FileInputStream(fileName);
   
    byte bytes[] = new byte[fis.available()];
   
    fis.read(bytes);
   
    fis.close();
   
    return new BASE64Encoder()。encode(bytes);
   
    }
   
    public static Document createDocument(String FileName) throws ParserConfigurationException, DOMException, IOException
   
    {
   
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
   
    DocumentBuilder db = dbf.newDocumentBuilder();
   
    Document document = db.newDocument();
   
    Element root = document.createElement_x_x("pictrue");
   
    Element name = document.createElement_x_x("name");
   
    name.appendChild(document.createTextNode("pictrue"));
   
    Element img = document.createElement_x_x("image");
   
    img.appendChild(document.createTextNode(getPictrueString(FileName)));
   
    root.appendChild(name);
   
    root.appendChild(img);
   
    document.appendChild(root);
   
    return document;
   
    }
   
    public static void doc2XmlFile(Document document, String filename) throws TransformerException
   
    {
   
    TransformerFactory tFactory = TransformerFactory.newInstance();
   
    Transformer transformer = tFactory.newTransformer();
   
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
   
    DOMSource source = new DOMSource(document);
   
    StreamResult result = new StreamResult(new File(filename));
   
    transformer.transform(source, result);
   
    }
   
    public static void readImgage(Document document,String outFile) throws IOException
   
    {
   
    Node image = document.getElementsByTagName_r("image")。item(0);
   
    String strImg = image.getFirstChild()。getTextContent();
   
    byte bytes []= new BASE64Decoder()。decodeBuffer(strImg);
   
    OutputStream os = new FileOutputStream(outFile);
   
    os.write(bytes);
   
    os.flush();
   
    os.close();
   
    }
   
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
   
    {
   
    try
   
    {
   
    doc2XmlFile(createDocument("d:/a.jpg"),"d:/test.xml");
   
    } catch (TransformerException e)
   
    {
   
    // TODO Auto-generated catch block
   
    e.printStackTrace();
   
    } catch (ParserConfigurationException e)
   
    {
   
    // TODO Auto-generated catch block
   
    e.printStackTrace();
   
    }
   
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
   
    DocumentBuilder db = dbf.newDocumentBuilder();
   
    Document document = db.parse("d:/test.xml");
   
    readImgage(document,"d:/b.jpg");
   
    }
   
    }