java - XML Parsing problems getting #text in nodeList -
i'm writing xml parser parse documents this:
<?xml version="1.0"?> <world x="1920" y="1080"> <samples>2</samples> <camera> <origin x="0.0" y="50.0" z="-600.0"></origin> <direction x= "0.0" y = "0.0" z = "0.0"></direction> <fov>30</fov> </camera> <objects> <plane> <shader>lambertian</shader> <colour r="1.0" g="1.0" b="1.0"></colour> <origin x="0.0" y="-80.0" z="0.0"></origin> <normal x="0.0" y="-1.0" z="0.0"></normal> </plane> <sphere> <shader>lambertian</shader> <colour r="0.0" g="1.0" b="0.0"></colour> <origin x="-100.0" y="-40.0" z="-200.0"></origin> <radius>40</radius> </sphere> <sphere> <shader>reflective</shader> <origin x="0.0" y="-40.0" z="-200.0"></origin> <radius>40</radius> </sphere> <sphere> <shader>transparent</shader> <origin x="-40.0" y="-40.0" z="-300.0"></origin> <radius>40</radius> <ri>1.07</ri> </sphere> </objects> <lights> <light> <origin x="100.0" y="200.0" z="-300.0"></origin> <colour r="1.0" g="1.0" b="1.0"></colour> </light> <light> <origin x="-100.0" y="300.0" z="-400.0"></origin> <colour r="0.5" g="0.5" b="0.5">origin</colour> </light> </lights> </world> however i'm having trouble code parsing far is:
file inputfile = new file(filename); documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); documentbuilder dbuilder = dbfactory.newdocumentbuilder(); document doc = dbuilder.parse(inputfile); doc.getdocumentelement().normalize(); element rootele = doc.getdocumentelement(); string resx = rootele.getattribute("x"); string resy = rootele.getattribute("y"); int width = integer.parseint(resx); int height = integer.parseint(resy); raytracer.setworld(width, height); nodelist nodes = rootele.getchildnodes(); system.out.println(nodes.item(0).getnodename()); node sampler = nodes.item(1); string samplestemp = sampler.gettextcontent(); system.out.println(samplestemp); int samples = integer.parseint(samplestemp); raytracer.setsampler(samples); node camera = nodes.item(2); but i'm getting weird results, instance in system.out.println(nodes.item(0).getnodename()); got #text , i'm not sure comes from. item(1) samples, item(2) #text , item(3) camera.
if xml formatted in example, e.g.
<world x="1920" y="1080"> <samples>2</samples> .... </world> then there #text between start tag <world> , start tag samples. in case there line break , 4 whitespaces. first node after <world>.
perhaps want ignore whitespaces.
Comments
Post a Comment