Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Changing Content

Now that we've successfully read information from various places in a DOM document, we'll look at changing some of that information.

Using the application we've already built, we'll first modify it so that if a location is listed as Closed, the date of the event will be changed to TBD. Then we'll change the value of the locationid to TBD.

In the next section, we'll build a document from scratch to hold participant information.

Changing Text

We'll begin by checking the value of the status element and if it's Closed, we'll reset the date element's content. Because the result affects the rest of the application (specifically the value of the date variable), we'll move the location check to the top of the method, as shown in Listing 3.15.

Listing 3.15 Checking the Location and Setting the Date

...
  public static void displayActivity(Node thisAct) {

   Element thisActElement = (Element)thisAct;

   Node locationRefNode = thisActElement
            .getElementsByTagName("locationRef").item(0);
   Element locationRefElement = (Element)locationRefNode;
   String locationRef = locationRefElement.getAttribute("locationid");

   Document doc = locationRefElement.getOwnerDocument();
   Element locationElement = doc.getElementById(locationRef);
   String locationName = "TBD";
   String locationDeck = "TBD";
   String status = locationElement.getElementsByTagName("status")
                      .item(0).getFirstChild()
                      .getNodeValue();

   if (status.equals("Closed")) {

     Node thisDate = thisActElement.getElementsByTagName("date")
                    .item(0);
     Node thisDateText = thisDate.getFirstChild();
     thisDateText.setNodeValue("TBD");

   } else {

     locationName = locationElement.getElementsByTagName("name")
                      .item(0).getFirstChild()
                      .getNodeValue();
     locationDeck = locationElement.getElementsByTagName("deck")
                     .item(0).getFirstChild()
                     .getNodeValue();
   }

   String activityCode =
            thisActElement.getAttribute("activityid");

...

First, we get the value of the status text, just as we've retrieved all the other text values. If the value is Closed, we make the change to the date element's content. To do this, we retrieve the element itself by choosing the first item in getElementsByTagName(), just as we did before. Because it's actually the value of the text node child we want to change, we get that next. Finally, we set the value of the text node using setNodeValue().

In Java, we could have accomplished all of this in a single step, using

thisActElement.getElementsByTagName("date").item(0)
          .getFirstChild().setNodeValue("TBD");

The rest of the application continues on as before, so the new value of TBD is retrieved for the date. The results are as follows:

Activity:
(A1) Zero-G Volleyball -- TBD
Type: Sports

        Even better than beach volleyball!

Spaces left: 16
Location: TBD, Deck TBD


Activity:
(A2) Stargazing -- TBD
Type: Educational

        Learn the visible constellations.

Spaces left: 5
Location: TBD, Deck TBD

Persistence

It's crucial to understand that we have just made a change to the in-memory copy of the XML document, as opposed to the original file. You can verify this by checking the file after you've run the application.

Nothing we have done here affects the original file unless and until we save the information, which we'll take care of at the end of the chapter.

Changing Attributes

Changing the value of an attribute is much more straightforward, as shown in Listing 3.16.

Listing 3.16 Setting an Attribute Value

...
   if (status.equals("Closed")) {

     Node thisDate = thisActElement.getElementsByTagName("date")
                    .item(0);
     Node thisDateText = thisDate.getFirstChild();
     thisDateText.setNodeValue("TBD");

     locationRefElement.setAttribute("locationid", "TBD");

   } else {

     locationName = locationElement.getElementsByTagName("name")
                      .item(0).getFirstChild()
                      .getNodeValue();
     locationDeck = locationElement.getElementsByTagName("deck")
                     .item(0).getFirstChild()
                     .getNodeValue();
   }
...

The setAttribute() method takes the name of the attribute and the desired value. If the named attribute doesn't exist, setAttribute() creates it, then sets the value.

  • + Share This
  • 🔖 Save To Your Account