how to read few points from xml using java -
I'm new to XML and I want to add points from an XML file to the point container.
This is the XML file:
& lt ;? Xml version = "1.0" encoding = "UTF-8" standalone = "no"? & Gt; & Lt; Container & gt; & Lt; Point & gt; & Lt; X & gt; 56 & lt; / X & gt; & Lt; Y & gt; 58 & lt; / Y & gt; & Lt; / Point & gt; & Lt; Point & gt; & Lt; X & gt; 59 & lt; / X & gt; & Lt; Y & gt; 40 & lt; / Y & gt; & Lt; / Point & gt; & Lt; Point & gt; & Lt; X & gt; 70 & lt; / X & gt; & Lt; Y & gt; 30 & lt; / Y & gt; & Lt; / Point & gt; & Lt; / Container & gt; I did this:
PERSONAL ZERO OpenFile () throws pars configuration exception, SAXException, IOException {JFileChooser = new JFileChooser (); Int option = of.showOpenDialog (of); While (! Of.getSelectedFile (). GetName (). EndsWith (".xml")) {string error = "error, please select txt file"; JOptionPane.showMessageDialog (this, error, "wrong type of file", JOptionPane.INFORMATION_MESSAGE); New = jefileclosures (); Option = of.showOpenDialog (of); } If (option == JFileChooser.APPROVE_OPTION) {thisFile = new file (of.getSelectedFile). GetPath ()); Documentbilder factory dbfc = documentbilderfactor.Newinstance (); DocumentBuilder debugger = DBFTC NE DocumentBuilder (); Document doctor = debilider.parse (this file); Doc.getDocumentElement () Normal () .; Nodelist nList = doc.getElementsByTagName ("container"); Element line = (element) nList.item (0); For (int i = 0; i & lt; nList.getLength (); i ++) {element point = (element) line. Get Elements biatagnam ("point"). Item (i); X = integer. Parsint (dot.getLabelBitname ("X"). Item (0) .getTextContent ()); Y = integer.parseint (dot.getlabeltagname ("y"). Item (0) .getTextContent ()); Duepoint (X, Y); Pc.add (new point (X, Y)); }} My problem is that it is a one time loop.
nList container is the list of nodes , And you have only one such element in the XML document, you need to get the point element instead:
node list nList = doc.getElementsByTagName ("Container "); Element Container Element = (element) nList.item (0); Nodelist point dot = containerAgent.getAlimateBitatname ("point"); For (int i = 0; i & lt; pointNodes.getLength (); i ++) {element point = (element) points nodes..me (i); ...
Comments
Post a Comment