【发布时间】:2019-06-03 16:28:12
【问题描述】:
我正在尝试使用 DOM-Library(必须使用它)在 Java 中迭代这个 xml 文件,到目前为止,我已经能够通过直接以这种方式请求它来获取某些元素:
NodeList pList = document.getElementsByTagName("position");
Node pNode = pList.item(0);
Element pElement = (Element) pNode;
double xp = Double.parseDouble(pElement.getAttribute("x"));
double yp = Double.parseDouble(pElement.getAttribute("y"));
double zp = Double.parseDouble(pElement.getAttribute("z"));
但这不再起作用,一旦我想访问本身可能有多个标签的元素。例如,如果我要添加更多球体,就会有更多标记为球体的标签。
我尝试遍历 Sphere/Lights 标签,但我似乎没有找到一种方法来访问子标签,它不依赖于我手动输入确切的行数。
我需要遍历所有表面/灯光标签,因为一旦我获得了所有信息,我就会在为其收集数据的实例中创建一个新的表面/灯光。
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE scene SYSTEM "scene.dtd">
<scene output_file="example1.png">
<background_color r="0.0" g="0.0" b="0.0"/>
<camera>
<position x="0.0" y="0.0" z="1.0"/>
<lookat x="0.0" y="0.0" z="-2.5"/>
<up x="0.0" y="1.0" z="0.0"/>
<horizontal_fov angle="45"/>
<resolution horizontal="512" vertical="512"/>
<max_bounces n="8"/>
</camera>
<lights>
<ambient_light>
<color r="1.0" g="1.0" b="1.0"/>
</ambient_light>
</lights>
<surfaces>
<sphere radius="1.0">
<position x="-2.1" y="0.0" z="-3.0"/>
<material_solid>
<color r="0.17" g="0.18" b="0.50"/>
<phong ka="0.3" kd="0.9" ks="1.0" exponent="200"/>
<reflectance r="0.0"/>
<transmittance t="0.0"/>
<refraction iof="2.3"/>
</material_solid>
</sphere>
<sphere radius="1.0">
<position x="0.0" y="0.0" z="-3.0"/>
<material_solid>
<color r="0.5" g="0.17" b="0.18"/>
<phong ka="0.3" kd="0.9" ks="1.0" exponent="200"/>
<reflectance r="0.0"/>
<transmittance t="0.0"/>
<refraction iof="2.3"/>
</material_solid>
</sphere>
<sphere radius="1.0">
<position x="2.1" y="0.0" z="-3.0"/>
<material_solid>
<color r="0.18" g="0.50" b="0.17"/>
<phong ka="0.3" kd="0.9" ks="1.0" exponent="200"/>
<reflectance r="0.0"/>
<transmittance t="0.0"/>
<refraction iof="2.3"/>
</material_solid>
</sphere>
</surfaces>
</scene>
【问题讨论】: