Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中迭代具有相同名称的标记的XML_Java_Xml_Dom - Fatal编程技术网

如何在Java中迭代具有相同名称的标记的XML

如何在Java中迭代具有相同名称的标记的XML,java,xml,dom,Java,Xml,Dom,我正试图使用DOM库(必须使用它)在Java中迭代这个xml文件,到目前为止,我只需通过这种方式直接请求就可以获得某些元素: NodeList pList = document.getElementsByTagName("position"); Node pNode = pList.item(0); Element pElement = (Element) pNode; double xp = Double.parseDouble(pElement.getAttribute("x")); do

我正试图使用DOM库(必须使用它)在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的标记

我试图循环使用Sphere/Lights标记,但似乎找不到一种方法来访问子标记,而不依赖于我手动键入确切的行数

我需要迭代所有的surface/light标记,因为一旦获得了所有信息,我就在为其收集数据的实例中创建了一个新的surface/light

<?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>

搜索文档中包含的所有元素

搜索所有子元素,即仅搜索给定元素的子树

所以你可以这样做:

NodeList sphereList = document.getElementsByTagName("sphere");
for (int i = 0; i < sphereList.getLength(); i++) {
    Element sphereElem = (Element) sphereList.item(i);
    Element positionElem = (Element) sphereElem.getElementsByTagName("position").item(0);

    double radius = Double.parseDouble(sphereElem.getAttribute("radius"));
    double x = Double.parseDouble(positionElem.getAttribute("x"));
    double y = Double.parseDouble(positionElem.getAttribute("y"));
    double z = Double.parseDouble(positionElem.getAttribute("z"));

    // use values here
}
NodeList sphereList=document.getElementsByTagName(“球体”);
for(int i=0;i
thx很多!在层次结构中移动对我来说似乎有点复杂,但有了这段代码,我现在明白了!