import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
public class xmlBase
{
public xmlBase()
{
}
public void createURL(JFileChooser jfcURL)
{
Document xmlCommon = null;
try
{
xmlCommon = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
Element rootElement = xmlCommon.createElement("settings");
xmlCommon.appendChild(rootElement);
jfcURL.resetChoosableFileFilters();
jfcURL.addChoosableFileFilter(new FileNameExtensionFilter("Расписание","xml"));
Element timetable = xmlCommon.createElement("timetable");
rootElement.appendChild(timetable);
if(jfcURL.showDialog(jfcURL, "Select")== JFileChooser.APPROVE_OPTION)
{
Text timetableText = xmlCommon.createTextNode(jfcURL.getSelectedFile().getAbsolutePath());
timetable.appendChild(timetableText);
}
else System.exit(0);
jfcURL.resetChoosableFileFilters();
jfcURL.addChoosableFileFilter(new FileNameExtensionFilter("Время","xml"));
Element time = xmlCommon.createElement("time");
rootElement.appendChild(time);
if(jfcURL.showDialog(jfcURL, "Select")== JFileChooser.APPROVE_OPTION)
{
Text timeText = xmlCommon.createTextNode(jfcURL.getSelectedFile().getAbsolutePath());
time.appendChild(timeText);
}
else System.exit(0);
jfcURL.resetChoosableFileFilters();
jfcURL.addChoosableFileFilter(new FileNameExtensionFilter("Монастырь","xml"));
Element church = xmlCommon.createElement("church");
rootElement.appendChild(church);
if(jfcURL.showDialog(jfcURL, "Select")== JFileChooser.APPROVE_OPTION)
{
Text churchText = xmlCommon.createTextNode(jfcURL.getSelectedFile().getAbsolutePath());
church.appendChild(churchText);
}
jfcURL.resetChoosableFileFilters();
jfcURL.addChoosableFileFilter(new FileNameExtensionFilter("Тип службы","xml"));
Element type = xmlCommon.createElement("type");
rootElement.appendChild(type);
if(jfcURL.showDialog(jfcURL, "Select")== JFileChooser.APPROVE_OPTION)
{
Text typeText = xmlCommon.createTextNode(jfcURL.getSelectedFile().getAbsolutePath());
type.appendChild(typeText);
}
else System.exit(0);
jfcURL.resetChoosableFileFilters();
jfcURL.addChoosableFileFilter(new FileNameExtensionFilter("Священник","xml"));
Element priest = xmlCommon.createElement("priest");
rootElement.appendChild(priest);
if(jfcURL.showDialog(jfcURL, "Select")== JFileChooser.APPROVE_OPTION)
{
Text priestText = xmlCommon.createTextNode(jfcURL.getSelectedFile().getAbsolutePath());
priest.appendChild(priestText);
}
else System.exit(0);
Transformer t = null;
try
{
t = TransformerFactory.newInstance().newTransformer();
}
catch (Exception e)
{
e.printStackTrace();
}
catch (TransformerFactoryConfigurationError e)
{
e.printStackTrace();
}
t.setOutputProperty(OutputKeys.METHOD, "xml");
t.setOutputProperty(OutputKeys.VERSION, "1.0");
t.setOutputProperty(OutputKeys.ENCODING, "windows-1251");
try
{
t.transform(new DOMSource(xmlCommon), new StreamResult(new FileOutputStream(new File("common.xml"))));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (TransformerException e)
{
e.printStackTrace();
}
}
public void setURL(String fileName)
{
try
{
Document xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(fileName));
Element root = xmlDoc.getDocumentElement();
NodeList seetings = root.getChildNodes();
for(int i = 0; i<5; i++)
{
Node field = seetings.item(i);
if(field instanceof Element)
{
Element fieldElement = (Element) field;
this.URL[i] = fieldElement.getTextContent();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String[] getURL()
{
return URL;
}
public Object[][] readXml(String xmlURL) throws Exception
{
Document xmlTimeTable = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(xmlURL));
Element root = xmlTimeTable.getDocumentElement();
NodeList meetings = root.getChildNodes();
Object[][] mChurch = new Object[meetings.getLength()][5];
for(int i = 0; i < meetings.getLength(); i++)
{
Element record = (Element) meetings.item(i);
NodeList data = record.getChildNodes();
for(int j = 0; j < data.getLength(); j++)
{
Node field = data.item(j);
if(field instanceof Element)
{
Element fieldElement = (Element) field;
if(fieldElement.getTagName().equals("date")) mChurch[i][j] = fieldElement.getTextContent();
else if(fieldElement.getTagName().equals("time")) mChurch[i][j] = fieldElement.getTextContent();
else if(fieldElement.getTagName().equals("church")) mChurch[i][j] = fieldElement.getTextContent();
else if(fieldElement.getTagName().equals("type")) mChurch[i][j] = fieldElement.getTextContent();
else if(fieldElement.getTagName().equals("priest")) mChurch[i][j] = fieldElement.getTextContent();
}
}
}
return mChurch;
}
private String[] URL = new String[5];
}