Create parameter in java when handling xml file, (dom parser)

reopriata

New Member
I'm new to java. I'm trying to get specific outputs for my xml file using inputs 0-11. I need to create parameter that represents a month. So for example if i put in "0" it will give me all values for month january. I wrote class that parses xml file and gives output that consits of all data in said xml. Now I'm trying to return specific values based on USER input (aka system.in). What makes it a little harder is the fact that the xml file has only 1 element (besides root). so im not sure how i would distinguish january from february since they're all inside element 'date' with same id.I'm not asking for code per say, I just want to know if it's possible to assign a value to a specific node and if user input asks for that value it will return node information (in Dom if possible). So preferably a scanner (system.in), but if there are other ways then I'd like to know them as well. My xml file reads\[code\]<holidays> <date class='holiday' value='http://stackoverflow.com/questions/15508992/2013-01-01'>New Year's Day</date> <date class='holiday' value='http://stackoverflow.com/questions/15508992/2013-01-21'>Martin Luther King Day</date> <date class='holiday' value='http://stackoverflow.com/questions/15508992/2013-01-24'>Belly Laugh Day</date></holidays>\[/code\]And my java code is as follows:\[code\]import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.List;import java.util.Scanner;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import edu.nyt.cst3619.Query.IQuery;import edu.nyt.cst3619.facade.IHoliday;import edu.nyt.cst3619.model.Holiday;public class HolidayRepository<Student> implements IQuery { private NodeList pizza;public List<String> getByMonth(int arg0) { buildNodeList(); List<String> SecondaryReturn = new ArrayList<String>(); // gonna use // array for (int s = 0; s < pizza.getLength(); s++) { // same here Node nd = pizza.item(s); if (nd.getNodeType() == Node.ELEMENT_NODE) { Element elephant = (Element) nd; if (elephant.getTagName().equals("date")) { String holiday = elephant.getAttribute("value") + "|"+ elephant.getTextContent(); SecondaryReturn.add(holiday); } } } return SecondaryReturn; }public class Test_HolidayXML { public static void main(String[] args) { HolidayRepository pizzas = new HolidayRepository(); List<String> holidayStringList = pizzas.getAllDays(); List<Holiday> MainReturn = pizzas.getAllHolidays(); for (String pizza1 : holidayStringList) { System.out.println(pizza1); } for (Holiday pizza2 : MainReturn) { System.out.println("Holidays! Date=" + pizza2.getHolidayDate() + "|" + "Description=" + pizza2.getDescription()); } }}\[/code\]
 
Back
Top