Аномалия

  • Автор темы hamsid
  • Дата начала
H

hamsid

#1
Народ, помогите, есть класс:
Код:
public class xmlBase 
{
//Здесь значение строк по умолчанию
public xmlBase()
{
for(int i = 0; i < 5; i++) URL[i] = "null";
}

//Здесь я извлекаю из xml-файла строки и вставляю их в массив
public void setURL()
{
Document xmlDoc = null;

try {
xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("common.xml"));
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Element root = xmlDoc.getDocumentElement();
NodeList seetings = root.getChildNodes();
for(int i = 0; i<5; i++)
{
Node field = seetings.item(i);
if(field instanceof Element)
this.URL[i] = field.getTextContent();

}
}

//Здесь я возвращаю массив
public String[] getURL()
{
return URL;
}
}
Проблема в том, что в setURL() я присваиваю поле-массив объекта и я уже это знаю ведь в этом методе уже распечатывал массив в цикле. Но когда я запускаю getURL() я получаю значения, заданные в конструкторе. АНОМАЛИЯ!!!!!!!
 
H

hamsid

#3
в классе, это его поле:
<!--shcode--><pre><code class='java'>private String[] URL = new String[5][/CODE]
 
H

hamsid

#5
Главный класс:
Код:
import javax.swing.*;

public class JDataBase
{
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} 
catch (Exception e) {
e.printStackTrace();
} 

mainFrame frmMain = new mainFrame();
frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMain.setVisible(true);
}
}
Фрейм:
Код:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class mainFrame extends JFrame 
{

public mainFrame() 
{

this.setLayout(null);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
this.setLocation(screenSize.width/4, screenSize.height/6);
this.setSize(800, 640);
this.setTitle("Священная база");

this.addWindowListener(new WindowAdapter()
{
public void windowOpened(WindowEvent we)
{
if(!new File("common.xml").exists())
work.createURL(new JFileChooser());
work.setURL("common.xml");
}
}
);

contentPanel cntPanel = new contentPanel(work.getURL());
this.add(cntPanel);
}
private xmlBase work = new xmlBase();
}
Панель:
Код:
import javax.swing.*;

public class contentPanel extends JPanel 
{
public contentPanel(String[] args)
{
String[] names = {"Дата","Время","Монастырь","Тип службы","Священник"};
xmlBase work = new xmlBase();

try 
{
JTable jtBase = new JTable(work.readXml(args[0]),names);
this.add(jtBase);
} 
catch (Exception e) 
{
e.printStackTrace();
}


}
}
Класс:
Код:
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];
}