xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeff Boston" <dad...@us.ibm.com>
Subject Re: Not receiving DOM_ATTR_MODIFIED notifications
Date Wed, 06 Dec 2000 20:01:41 GMT

Eric,

Thanks for posting to the xerces-j-dev.  Here's a small snippet to show the
problem.

The output from running the program shows I'm missing the last name change
for the second person.  The last name has a default in the DTD and the
second person was added to the tree.  I'm receiving the attribute change
notifications for the other changes.

Person1 - LastName=Doe FirstName=John
Person2 - LastName=SMITH FirstName=
handleEvent() - eventType=DOMAttrModified attribute=LastName oldValue=Doe
newValue=Johnson
handleEvent() - eventType=DOMAttrModified attribute=FirstName oldValue=John
newValue=Jane
handleEvent() - eventType=DOMAttrModified attribute=FirstName oldValue=null
newValue=Sally
Person1 - LastName=Johnson FirstName=Jane
Person2 - LastName=Miller FirstName=Sally


Jeff





import java.io.*;
import org.apache.xerces.dom.*;
import org.apache.xerces.dom.events.*;
import org.apache.xerces.parsers.*;
import org.w3c.dom.*;
import org.w3c.dom.events.*;
import org.xml.sax.*;
/***********************************************************************
 **********************************************************************/
public class TestDOM_ATTR_MODIFIED implements EventListener {
/***********************************************************************
 **********************************************************************/
public TestDOM_ATTR_MODIFIED() throws Exception {
     StringBuffer xmlFileData = new StringBuffer();

     //
     xmlFileData.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
     xmlFileData.append("<!DOCTYPE People [ \n");
     xmlFileData.append("<!ELEMENT People (Person)*                 >\n");
     xmlFileData.append("<!ELEMENT Person EMPTY                     >\n");
     xmlFileData.append("<!ATTLIST Person LastName  CDATA \"SMITH\"  \n");
     xmlFileData.append("                 FirstName CDATA #IMPLIED  >\n");
     xmlFileData.append("]>\n");
     xmlFileData.append("<People>\n");
     xmlFileData.append("  <Person LastName=\"Doe\" FirstName=\"John\" />\n");
     xmlFileData.append("</People>\n");
     xmlFileData.append("</xml>\n");

     //
     DOMParser parser = new DOMParser();
     parser.setFeature("http://xml.org/sax/features/validation", true);
     parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
     parser.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
     parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace",
false);
     parser.parse(new InputSource(new StringReader(xmlFileData.toString())));
     Document doc = parser.getDocument();

     // Get the People element
     NodeList nl = doc.getChildNodes();
     Element people = null;
     for (int i = 0, count = nl.getLength(); i < count; i++) {
          if (nl.item(i) instanceof Element && "People".equals(nl.item(i).getNodeName()))
{
               people = (Element) nl.item(i);
               break;
          }
     }

     // Get person 1
     nl = people.getChildNodes();
     ElementImpl person1 = null;
     for (int i = 0, count = nl.getLength(); i < count; i++) {
          if ("Person".equals(nl.item(i).getNodeName())) {
               person1 = (ElementImpl) nl.item(i);
               break;
          }
     }

     // Create the 2nd person
     ElementImpl person2 = (ElementImpl) doc.createElement("Person");
     people.appendChild(person2);

     //
     person1.addEventListener(MutationEventImpl.DOM_ATTR_MODIFIED, this, false);
     person2.addEventListener(MutationEventImpl.DOM_ATTR_MODIFIED, this, false);

     //
     System.out.println("Person1 - LastName=" + person1.getAttribute("LastName") + " FirstName="
+ person1.getAttribute("FirstName"));
     System.out.println("Person2 - LastName=" + person2.getAttribute("LastName") + " FirstName="
+ person2.getAttribute("FirstName"));

     //
     person1.setAttribute("LastName", "Johnson");
     person1.setAttribute("FirstName", "Jane");
     person2.setAttribute("LastName", "Miller");
     person2.setAttribute("FirstName", "Sally");

     //
     System.out.println("Person1 - LastName=" + person1.getAttribute("LastName") + " FirstName="
+ person1.getAttribute("FirstName"));
     System.out.println("Person2 - LastName=" + person2.getAttribute("LastName") + " FirstName="
+ person2.getAttribute("FirstName"));
}
/***********************************************************************
 **********************************************************************/
public void handleEvent(Event e) {
     MutationEvent mEvent = (MutationEvent) e;
     System.out.println("handleEvent() -" + " eventType=" + e.getType() + " attribute=" +
mEvent.getAttrName() + " oldValue=" + mEvent.getPrevValue()
+ " newValue=" + mEvent.getNewValue());
}
/***********************************************************************
 **********************************************************************/
public static void main(java.lang.String[] args) {
     try {
          new TestDOM_ATTR_MODIFIED();
     } catch (Exception e) {
          e.printStackTrace();
     }
}
}


Mime
View raw message