I'm suppose to find the stock symbol, quantity and price. The first line is RY, 15, 49.5 (symbol, quantity and price). Please help. I don't know what method to use. Note that the stock can be this: ><Stock symbol="RY"></Stock> or this: ><Stock symbol = "RY"> </Stock> [HIDE]<?xml version="1.0"?> <Portfolio> <Investment><Stock symbol="RY"></Stock><Qty>15</Qty><Comment>this is a good one; esp. the BookValue</Comment><BookValue>49.5</BookValue></Investment> <Investment><Comment>this is not "bad"</Comment><Stock symbol = "NT"></Stock><Qty>10</Qty> <BookValue>1.85</BookValue></Investment> <Investment><Qty>2</Qty><BookValue>37.02</BookValue><Comment>not sure about this; >= last time</Comment><Stock symbol="BMO"> </Stock></Investment> </Portfolio> [/HIDE]
pretty straight forward. use xpath or domparser or some xml parser those will give you the methods you need to get your data. xpath is excellent, but if you want, domparser is very easy to use too.
I didn't learn these yet. So I did this. lol String Symbol = ""; int Quantity = 0; double Price = 0.0; for(; fileInput.hasNext(); ) { String line = fileInput.nextLine(); line = line.replace(" ", ""); int frontSymbol = (line.indexOf("<Stocksymbol")) ; int endSymbol = (line.indexOf("</Stock>")) ; int frontQuantity = (line.indexOf("<Qty>")) ; int endQuantity = (line.indexOf("</Qty>")); int frontPrice = (line.indexOf("<BookValue>")); int endPrice = (line.indexOf("</BookValue>")); if((frontSymbol>-1)&&(endSymbol>-1)&&(frontQuantity>-1)&&(endQuantity>-1)&&(frontPrice>-1)&&(endPrice>-1)) { Symbol = line.substring(frontSymbol+ 14,endSymbol- 2); Quantity = Integer.parseInt(line.substring(frontQuantity+ 5, endQuantity)); Price = Double. parseDouble(line.substring(frontPrice+ 11, endPrice)); output.println(Symbol + " ------------" + Quantity + "---------- " + Price); } } I only learn how to use String class, can you think of another way using the String class. My code prints out the symbol, quantity, price with delay. lol
wait, did your prof/teacher allow you to use external methods? or is your assignment to CREATE a parser for that xml?
I don't even know what xml is. The book says we will learn it later. I just have to treat it as normal text. So hard.
oh. xml stands for extended markup language, the text that you have there, with all the "<" and ">", is a form of language, best to use when serializing data. what you have there, is xml. (note "<?xml version="1.0"?>" in your text) why is your teacher telling you to parse the xml without teaching it to you >.> ugh. i hate profs who do that. stupid profs
anyways you do realize that this isn't really a programming forum, and we're just lending a hand where we can. i don't think there are a lot of programmers in PA. you'd have better chance in a java forum.