-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreatePictureFile.java
82 lines (64 loc) · 2.65 KB
/
CreatePictureFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
//package xmlbase64decoder;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import java.io.File;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/**
*
* @author saeliux
*/
public class CreatePictureFile {
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("/cs/home/cse73232/tomcat/webapps/FoodsRUs/WEB-INF/catalog.data/Catalog.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
//NodeList listOfPictures = doc.getElementsByTagName("/Categories/Picture/");
NodeList categoryList = doc.getElementsByTagName("Categories");
int totalCategories = categoryList.getLength();
for (int i=0; i<totalCategories;i++){
Node category =categoryList.item(i);
Element categoryElement = (Element)category;
NodeList catIDs = categoryElement.getElementsByTagName("CategoryID");
NodeList pics = categoryElement.getElementsByTagName("Picture");
String picPath = pics.item(0).getFirstChild().getNodeValue();
String cID = catIDs.item(0).getFirstChild().getNodeValue();
System.out.println("catID = " + cID + " picPath = " + picPath);
decodePic(picPath, "Pictures/"+cID +".jpg");
}
}
private static void decodePic(String decodePath, String filename)
{
String base64 =decodePath;
//decode the content
try {
System.out.println("decoding to " + filename + ".jpg");
byte decoded[] = new sun.misc.BASE64Decoder().decodeBuffer(base64);
FileOutputStream file = new FileOutputStream(filename);
for (int i = 78; i < decoded.length; i++)
{
file.write(decoded[i]);
}
file.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}