Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 19 20 21 22 23 24 < 25 > 26 27 28 29 30 31 .. 166 >> Next

020 DocumentBuilder builder = factory.newDocumentBuilder();
021 m_doc = builder.parse(filename);
022 }
023 catch ( Exception e )
024 {
025 System.err.println("Error processing " +
026 filename + "." +
027 "Stack trace follows:");
028 e.printStackTrace();
029 }
030 }
031
032 /*
033 * A very simple method that switches values of
034 * days in an XML document and prints it out.
035 *
036 * @param a the value for one day
037 * @param b the value for another day
038 * @param keep a boolean value, designating if you
039 * want to keep this version of the DOM tree.
040 */
041 public void showSwitchedDays(String a , String b,
042 boolean keep)
043 {
044 Document newdoc = null;
045
046 if ( m_doc == null )
047 {
048 System.out.println("Error - no document.. ");
049 return;
050 }
051
052 /**
053 * If the keep variable was set, do the manipulation
054 * to the instance variable m_doc.. Otherwise, just
055 * do the manipulation of the copy of the tree.
056 *
057 */
058 if ( !keep )
059 newdoc = (Document)m_doc.cloneNode(true);
060 else
Listing 6.1 (continued)
When Implementations of Standard APIs Collide 55
061 newdoc = m_doc;
062
063 /* Use the DOM API to switch days */
064 NodeList nl = newdoc.getElementsByTagName("day”);
065 int len = nl.getLength();
066 for ( int i = 0; i < len; i++ )
067 {
068
069 Element e = (Element)nl.item(i);
070
071 if ( e.getAttribute("name”).equals(a) )
072 {
073 e.setAttribute("name”,b);
074 } else if ( e.getAttribute("name”).equals(b) )
075 {
076 e.setAttribute("name”, a);
077 }
078 }
079
080 System.out.println(
081 newdoc.getDocumentElement().toString()
082 );
083
084 }
085
086 /* Print out the DOM Tree */
087 public void showDays()
088 {
089 System.out.println(
090 m_doc.getDocumentElement().toString()
091 );
092 }
093
094 public static void main(String[] args)
095 {
096 if ( args.length < 1 )
097 {
098 System.err.println("Usage: Argument must be the " +
099 "filename of an XML file”);
100 System.exit(-1);
101 }
102 String filename = args[0];
103
104 ScheduleSwitcher switcher = new ScheduleSwitcher(filename);
105
106 System.out.println("\nIf you switched " +
107 " the Wed & Thurs meetings ");
108 System.out.println("this is what it would " +
109 "look like:\n*******");
Listing 6.1 (continued)
56 Item 6
110
111 switcher.showSwitchedDays("wednesday", "thursday", false);
112
113 System.out.println("\nHere is the current " +
114 " schedule:\n********");
115 switcher.showDays();
116
117 }
118 }
Listing 6.1 (continued)
In our simple example, we have written a method, showSwitchedDays(), on line 41 of Listing 6.1, that switches the name attribute on the <day/> tag of an XML file by manipulating the DOM and prints the resulting XML file to standard out. If the boolean value keep is true, then the DOM manipulation affects the DOM tree instance variable. If the Boolean value keep is false, then the method simply prints out a copy of the switched schedule but keeps the original DOM in memory. On lines 106-109 of Listing 6.1, we tell the class to print the meetings as if Wednesdays and Thursdays were switched, but to not permanently alter the schedule. Listing 6.3 shows the output of this program using the XML file in Listing 6.2, when we ran this with JDK 1.3.1, and implementations of the DOM in the crimson.jar file.
<?xml version="1. Î ? V
<meetings>
<day name="wednesday">
<meeting desc=" romans">
<member name= "ben"/><member name="billy"/><member name="chuck"/>
<member name= "dan"/><member name="keith"/><member name="kevin"/>
<member name= "matt d."/><member name="matt v."/><member 2
name="rich"/>
<member name= "todd"/>
</meeting>
</day>
<day name="thursday">
<meeting desc=" all">
<member name= "avery"/><member name="catherine"/><member
name="dawn"/>
<member name= "doverly"/><member name="gwen"/><member name="heidi" />
<member name= "holly"/><member name="jenny"/><member name="patrice "/>
<member name= "sandy b."/><member name="sandy c."/><member 2
name="sarah"/>
<member name= "shelly"/><member name="suzanne"/>
</meeting>
</day>
</meetings>
Listing 6.2 XML schedule file (schedule.xml)
When Implementations of Standard APIs Collide 57
C:\pitfalls\week5>c:\jdk1.3.1\bin\java -classpath .;crimson.jar;jaxp .jar
ScheduleSwitcher schedule.xml
If you switched the Wed & Thurs meetings,
this is what it would look like:
*******
<meetings>
<day name="thursday">
<meeting desc=”romans”>
<member name=”ben” /><member name=”billy” /><member name=” chuck” />
<member name=”dan” /><member name=”keith” /><member name=” kevin” />
<member name=”matt d.” /><member name=”matt v.” 2
/><member name=”rich” />
<member name=”todd” />
</meeting>
</day>
<day name="wednesday">
<meeting desc=”all”>
<member name=”avery”/><member name=”catherine” 2
/><member name=”dawn” />
Previous << 1 .. 19 20 21 22 23 24 < 25 > 26 27 28 29 30 31 .. 166 >> Next