How do I Iterate over an XML in Java, with tags, that have the same name

守給你的承諾、 提交于 2021-02-17 06:10:26

问题


I'm trying to iterate over this xml-file in Java using the DOM-Library(have to use it), and so far I've been able to get certain elements just by directly asking for it this way:

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"));

but this doesn't work anymore, once I want to access elements, that might have multiple tags in itself. for example, if I were to add more Spheres, there would be more tags labeled Sphere.

I've tried to loop through the Sphere/Lights Tag, but I don't seem to find a way, to access the sub-tag, that doesn't rely on me manually typing in the exact number of rows.

I need to iterate over all surface/light-tags, since once I have all the information I create a new surface/light in the instance of collecting the data for it.

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

回答1:


document.getElementsByTagName(tagname) searches all the Elements contained in the document.

element.getElementsByTagName(name) searches all descendant Elements, i.e. only the sub-tree of the given element.

So you can do it like this:

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
}


来源:https://stackoverflow.com/questions/56431233/how-do-i-iterate-over-an-xml-in-java-with-tags-that-have-the-same-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!