xml file

<?xml version="1.0" encoding="UTF-8" standalone="no"?><movies>
    <movie id="1111">
        <title>Blackhat</title>
        <genre>thriller</genre>
        <duration> 90 mins </duration>
    <age>28</age></movie>

    <movie id="1002">
        <title>The Wedding Ringer</title>
        <genre>comedy</genre>
        <duration> 100 mins </duration>
    </movie>
    <movie id="1003">
        <title>The Avengers</title>
        <genre>action</genre>
        <duration> 180 mins </duration>   
    </movie>
    <movie id="1004">
        <title>Taken 3</title>
        <genre>action</genre>
        <duration> 100 mins </duration>
    </movie>

    <movie id="1005">
        <title>Insurgent</title>
        <genre>Science Fiction</genre>
        <duration>180 mins</duration>
    </movie>

    <movie id="1006">
        <title>Jurassic World</title>
        <genre>Adventure</genre>
        <duration>120 mins</duration>
    </movie>

    <movie id="1111">
        <title>Interstellar</title>
        <genre>Science Fiction</genre>
        <duration>180 mins</duration>
    </movie>
</movies>

what i want to adding movie id tag to the last line, for example

<?xml version="1.0" encoding="UTF-8" standalone="no"?><movies>
    <movie id="1111">
        <title>Blackhat</title>
        <genre>thriller</genre>
        <duration> 90 mins </duration>
    <age>28</age></movie>

    <movie id="1002">
        <title>The Wedding Ringer</title>
        <genre>comedy</genre>
        <duration> 100 mins </duration>
    </movie>
    <movie id="1003">
        <title>The Avengers</title>
        <genre>action</genre>
        <duration> 180 mins </duration>   
    </movie>
    <movie id="1004">
        <title>Taken 3</title>
        <genre>action</genre>
        <duration> 100 mins </duration>
    </movie>

    <movie id="1005">
        <title>Insurgent</title>
        <genre>Science Fiction</genre>
        <duration>180 mins</duration>
    </movie>

    <movie id="1006">
        <title>Jurassic World</title>
        <genre>Adventure</genre>
        <duration>120 mins</duration>
    </movie>

    <movie id="1111">
        <title>Interstellar</title>
        <genre>Science Fiction</genre>
        <duration>180 mins</duration>
    </movie>

    **<movie id="2222">
        <title>test</title>
        <genre>test</genre>
        <duration>test</duration>
    </movie>**

</movies>

i try to use

NodeList nodes = doc.getElementsByTagName("movie");
for (int temp = 0; temp < nodes.getLength(); temp++){
Element element = (Element) doc.getElementsByTagName("movie").item(temp+1);
element.setAttribute("id", "2222");

to create and insert the attribute into last line but the

.getElementsByTagName("movie").item(temp+1);

only work with existing attribute().

It can be done, but you have the wrong approach. You must create the Element objects, and then create/set the Text in them. You will need to add the Element objects to the appropriate parent Element objects. And then you will have to serialize (write) the XML as characters to the file.

The whole think is actually somewhat tricky, with Java's native XML classes. There is a lot to be said for using a 3rd party library that wraps such things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.