Two Laptop Bag*
The moose likes Java in General and the fly likes XML help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "XML help" Watch "XML help" New topic
Author

XML help

Brandon Broschinsky
Ranch Hand

Joined: Aug 21, 2004
Posts: 41
I am writing a program, so far I am able to load the XML document into a DOM tree. I can navigate to the first entry that i have. I have a button that I want to press that will move me to the next entry. When i do this is move to the next entry and then goes to the one after that. Essentially gets every other entry. I have attached source of the methods to go through the XML and also included the format of my XML document. In ideas or better ways are appreciated.

/////////////CODE////////////////////////////
public class getXml {
private String[] entryInfo=new String[11];
Node currentEntry;

public String[] getEntry(Node node) throws IOException {//used to retrive one record in XML file
int count =0; //counter for number entries
int location=0; //used to hold location in array
//Node firstEntry = node.getFirstChild().getFirstChild().getNextSibling(); //takes to first <entry>
// Node currentEntry = node.getFirstChild().getFirstChild().getNextSibling();//<entry> portion of current entry
Node current = node.getFirstChild().getFirstChild().getNextSibling();//takes to 1st<entry>
currentEntry = current;
count = (current.getChildNodes().getLength()/2);
current = current.getFirstChild().getNextSibling();//at fname
//System.out.println(current.getFirstChild().getNodeValue());
count--;
while(count>0){
entryInfo[location]=current.getFirstChild().getNodeValue();
current=current.getNextSibling().getNextSibling();
System.out.println(current.getFirstChild().getNodeValue());

location++;
count--;
}
return entryInfo;
}
public String[] getNext(){//used to get the next entry
Node node = currentEntry;
int count = 0;
int location =0;
Node current = node.getNextSibling().getNextSibling();//takes to 1st<entry>
currentEntry = current;
count = (current.getChildNodes().getLength()/2);
current = current.getFirstChild().getNextSibling();//at fname
System.out.println(current.getFirstChild().getNodeValue());
count--;
while(count>0){
entryInfo[location]=current.getFirstChild().getNodeValue();
current=current.getNextSibling().getNextSibling();
System.out.println(current.getFirstChild().getNodeValue());

location++;
count--;
} System.out.println("end");
return entryInfo;
}
public String[] getPrevious(){//used to get previous entry
return entryInfo;
}
}
////////////CODE TO LOAD XML INTO DOCUMENT
iterator = new getXml();
try {
// Use JAXP to find a parser
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
// Turn on namespace support
factory.setNamespaceAware(true);
DocumentBuilder parser = factory.newDocumentBuilder();
// Read the entire document into memory
Node document = parser.parse(addressFile);
// Process it starting at the root
entryInfo = iterator.getEntry(document);

} catch (SAXException e) {
System.out.println(addressFile + " is not well-formed.");
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e);
} catch (ParserConfigurationException e) {
System.out.println("Could not locate a JAXP parser");
}
/////////XML FILE/////////////////////
<?xml version="1.0" encoding="iso-8859-1"?>
<Address_Book>
<entry>
<fName>first name</fName>
<lName>last name</lName>
<address1>first part</address1>
<address2>first part</address2>
<city>city</city>
<state>state</state>
<zip>zip</zip>
<phone>phone</phone>
<cell>cellphone</cell>
<email>email address</email>
<notes>this is important</notes>
</entry>
<entry>
<fName>Jpe</fName>
<lName>Blow</lName>
<address1>first part</address1>
<address2>first part</address2>
<city>city</city>
<state>state</state>
<zip>zip</zip>
<phone>phone</phone>
<cell>cellphone</cell>
<email>email address</email>
<notes>this is important</notes>
</entry>
<entry>
<fName>Joey</fName>
<lName>what up name</lName>
<address1>fhey heyt part</address1>
<address2>first part</address2>
<city>city</city>
<state>state</state>
<zip>zip</zip>
<phone>phone</phone>
<cell>cellphone</cell>
<email>email address</email>
<notes>this is important</notes>
</entry>
</AddressBook>

Thanks
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
I won't even attempt reading that without using the valid CODE tags...
Brandon Broschinsky
Ranch Hand

Joined: Aug 21, 2004
Posts: 41
What is incorrect about the tags. I mistyped the last closing tag it should be <Address_Book> Java will parse this html document and loads it with no errors and without throwing any exceptions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML help