Problem passing variables using XML........
Hello all.
The problem that I am having involves using java with XML. My application is a calendar/diary, that enables a user to save reminders on particular days which is stored in an XML file.
My application writes to the XML file, but whenever I want to save another reminder in the same month and year as an existing reminder, it does not save it within the same tags as the existing reminder. Example:
This is what I get :-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Diary SYSTEM"Diary.dtd">
<Diary>
<Year Id="2007">
<Month Id="June">
<Day Id="22">Tomorrow is Knowsley Festival!!!</Day>
</Month>
</Year>
<Year Id="2007">
<Month Id="June">
<Day Id="23">Going to Knowsley Festival</Day>
</Month>
</Year>
</Diary>
This is what I want:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Diary SYSTEM"Diary.dtd">
<Diary>
<Year Id="2007">
<Month Id="June">
<Day Id="22">Tomorrow is Knowsley Festival!!!</Day>
<Day Id="23">Going to Knowsley Festival</Day>
</Month>
</Year>
</Diary>
My SaveReminder method defines the variables and sets up the document:
publicvoid SaveReminder( String dayID, String yearID, String monthID )throws Exception{
file =new File("Diary.xml" );
doc = DocumentBuilderFactory.newInstance( ).newDocumentBuilder( ).parse( file.toURL( ).toString( ) );
year = yearID;
month = monthID;
day = dayID;
CreateEntry( doc.getDocumentElement( ) );
writeXmlFile( );
}
The CreateEntry method uses the GetYear method to find out if there is an existing reminder in the same month and year:
publicboolean CreateEntry( Node node ){
Node searchNode;
searchNode = getYear( node );
if( searchNode ==null ){
Element newNode = doc.createElement("Year" );
searchNode = node.appendChild( newNode );
newNode.setAttribute("Id", year );
}
}
private Node getYear( Node node ){
node = node.getFirstChild( );
while( node !=null ){
System.out.println ( node.getNodeName( ) );
System.out.println ( node.getNodeValue( ) );
if( node.getNodeName( ).equals("Year" ) && (node.getAttributes( ).item( 0 ).getNodeValue( ) ) == year )
return node;
node = node.getNextSibling( );
}
returnnull;
}
Using the System.out.println I try to grab the name and value of the node. The problem is the (node.getAttributes( ).item( 0 ).getNodeValue( ) ) == year )
line of code always returns null, when it should be the value of year. Can't figure it out!!!!
Cheers for any help.
[4182 byte] By [
Redstaara] at [2007-11-27 6:16:10]

A little bit more info.....
I have an example of an application which writes to an XML file, which I am using as a guide. I will post it here as it uses pretty much the same methods as my application, and the if(node.getNodeName().equals("Year")
&& Integer.valueOf(node.getAttributes().item(0).getNodeValue())==year)
code executes in this application. Why does it execute in this application, but not mine I wonder?
Here it is:
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class CallDOM
{
Document doc;
File file;
Scanner input;
int year, month, day;
String currentYear, currentMonth;
public static void main(String args[]) throws Exception
{
CallDOM cd=new CallDOM();
}
CallDOM() throws Exception
{
file=new File("Diary.xml");
//create DOM from file
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file.toURL().toString());
input=new Scanner(System.in);
System.out.println("1) Create entry");
System.out.println("2) Read entry");
System.out.println("3) Show Diary");
System.out.println("4) Quit");
int choice=input.nextInt();
while(choice!=4)
{
switch(choice)
{
case 1: GetDate();CreateEntry(doc.getDocumentElement());break;
case 2: GetDate();ReadEntry(doc.getDocumentElement());break;
case 3: ShowDiary(doc.getDocumentElement());
}
System.out.println("1) Create entry");
System.out.println("2) Read entry");
System.out.println("3) Show Diary");
System.out.println("4) Quit");
choice=input.nextInt();
}
writeXmlFile();
}
public void GetDate()
{
System.out.println("Enter date (dd mm yyyy)");
day=input.nextInt();
month=input.nextInt();
year=input.nextInt();
}
public boolean ReadEntry(Node node)
{
node = getYear(node);
if(node==null) return false;
node = getMonth(node);
if(node==null) return false;
node = getDay(node);
if(node==null) return false;
node = node.getFirstChild();
while(node!=null)
{
System.out.println(node.getNodeValue().trim());
node=node.getNextSibling();
}
return true;
}
public boolean CreateEntry(Node node)
{
Node searchNode;
searchNode = getYear(node);
if(searchNode==null)
{
Element newNode = doc.createElement("Year");
searchNode = node.appendChild(newNode);
newNode.setAttribute("Id",Integer.toString(year));
}
node = searchNode;
searchNode = getMonth(node);
if(searchNode==null)
{
Element newNode = doc.createElement("Month");
searchNode = node.appendChild(newNode);
newNode.setAttribute("Id",Integer.toString(month));
}
node = searchNode;
searchNode = getDay(node);
if(searchNode==null)
{
Element newNode = doc.createElement("Day");
searchNode = node.appendChild(newNode);
newNode.setAttribute("Id",Integer.toString(day));
}
node = searchNode;
System.out.println("Enter Text");
String entry=input.next();
entry+=input.nextLine();
Node textNode = doc.createTextNode(entry);
node.appendChild(textNode);
return true;
}
public void ShowDiary(Node node)
{
Stack<Node> stack=new Stack<Node>();
Node child;
stack.push(node);
while(!stack.empty())
{
node = stack.pop();
if(ProcessNode(node))
{
child = node.getLastChild();
while(child!=null)
{
stack.push(child);
child = child.getPreviousSibling();
}
}
}
}
private boolean ProcessNode(Node node)
{
if(node.getNodeName().equals("Year"))
currentYear=node.getAttributes().item(0).getNodeValue();
if(node.getNodeName().equals("Month"))
currentMonth=node.getAttributes().item(0).getNodeValue();
if(node.getNodeName().equals("Day"))
{
System.out.print(node.getAttributes().item(0).getNodeValue()+"/"+
currentMonth+"/"+currentYear+": ");
node=node.getFirstChild();
while(node!=null)
{
System.out.println(node.getNodeValue().trim());
node=node.getNextSibling();
}
return false;
}
return true;
}
private Node getYear(Node node)
{
node=node.getFirstChild();
while(node!=null)
{
if(node.getNodeName().equals("Year")
&& Integer.valueOf(node.getAttributes().item(0).getNodeValue())==year)
return node;
node = node.getNextSibling();
}
return null;
}
private Node getMonth(Node node)
{
node=node.getFirstChild();
while(node!=null)
{
if(node.getNodeName().equals("Month")
&& Integer.valueOf(node.getAttributes().item(0).getNodeValue())==month)
return node;
node = node.getNextSibling();
}
return null;
}
private Node getDay(Node node)
{
node=node.getFirstChild();
while(node!=null)
{
if(node.getNodeName().equals("Day")
&& Integer.valueOf(node.getAttributes().item(0).getNodeValue())==day)
return node;
node = node.getNextSibling();
}
return null;
}
private void writeXmlFile() throws Exception
{
Source source = new DOMSource(doc);
Result result = new StreamResult(file);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT,"yes");
xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"Diary.dtd");
xformer.transform(source, result);
}
}
Thanks for any help at all!!!!!