• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XML help

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I won't even attempt reading that without using the valid CODE tags...
 
Brandon Broschinsky
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic