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]
# 1

Sorry, misinterpreted your question. I was thinking that you were just overwriting the previous XML file and couldn't get the old data to show up.

Sounds to me that the easiest way to check if there is an existing year/month/date would be to iterate through each line and check if the string contains the year/month/date of the new entry.

Message was edited by:

tk393

tk393a at 2007-7-12 17:27:33 > top of Java-index,Java Essentials,Java Programming...
# 2

Yeah. The problem is that the System.out.println ( node.getNodeValue( ) );

line always prints 'null' when it should be the value of 'year' defined in the SaveReminder method. It this value was the value of 'year', it should write the new reminder in the same Year tags as the original reminder because the if( node.getNodeName( ).equals( "Year" ) && (node.getAttributes( ).item( 0 ).getNodeValue( ) ) == year )

return node;

node = node.getNextSibling( );

}

statement would then be utilised. Why is the variable 'year' always null?

Redstaara at 2007-7-12 17:27:33 > top of Java-index,Java Essentials,Java Programming...
# 3

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!!!!!

Redstaara at 2007-7-12 17:27:33 > top of Java-index,Java Essentials,Java Programming...
# 4

Hi

Your problem is not about xml, but simple String comparision.

You have code like this:

if( node.getNodeName( ).equals( "Year" ) && (node.getAttributes( ).item( 0 ).getNodeValue( ) ) == year )

return node;

node = node.getNextSibling( );

}

Beware, that you can't compare Strings with == instruction, since this is objects comparision not String content. use stringObject.equals instead. Anyway you use equals in first part of statement:) why did you change second part to '==' ?

Regards

marecki6000

marecki6000a at 2007-7-12 17:27:33 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks for your suggestion mate. To be honest, I didn't realise that the '==' would not be effective in this instance.

However, I dont fully understand the syntax of what you are trying to explain to me. Would you be able to type it out in code for me? It'll just make more sense to me then!

I hope you are correct, as I would be very grateful due to this problem baffling me for two days now.

Cheers.

Redstaara at 2007-7-12 17:27:33 > top of Java-index,Java Essentials,Java Programming...
# 6

Your right mate!!!!

The code ( node.getAttributes( ).item( 0 ).getNodeValue( ) )==year

should actually of been ( node.getAttributes( ).item( 0 ).getNodeValue( ).equals( year ) )

Thanks alot for the tip!!

I believe I was using the '==' due to it being in the example code. However, that was passing an integer, not a string.

I really, really appreciate your help. Thanks alot.

Redstaara at 2007-7-12 17:27:33 > top of Java-index,Java Essentials,Java Programming...