home
NEWS       BLOGS       FORUMS       NEWSLETTERS       RESEARCH       EVENTS       DIGITAL LIBRARY       CAREERS  
Network Computing Network Computing Powered by InformationWeek Business Technology Network

IMMERSE YOURSELF:

SOA

  |

Data Center

  |

802.11n

  |

Data Privacy

  |
APO  |

Virtualization

  |

NAC

  |

Security

  |

Network Mgmt

  |

Enterprise Apps

  |

Storage & Servers



Netdesign Manual

Part 2

Java XML Programmers Reference

Chapter 11: XML Tools for Information Appliances


September 24, 2001


Brought to you by:





Check It Out!

Children Methods

These methods enable access to child elements. They are typically used after parsing a document. Remember that all elements in the document are represented as XMLElement objects.

There is no way of accessing "sibling" elements in NanoXML as there is in DOM; each element is a child of the element directly above it.

public int countChildren()

public java.util.Enumeration enumerateChildren()

These methods take no arguments. Interestingly enough, countChildren() only returns the number of children one level deep in the tree. The other method returns the children n-levels deep.

Child Methods

These methods perform operations on elements in the document. It is helpful to think of the structure in memory as a Document Object Model (DOM), even though it is not. Remember that all elements in the document are represented as XMLElement objects; this is similar to the Node interface in the package org.w3c.Dom. Again, there are no siblings in NanoXML - only children.

public void addChild(nanoxml.XMLElement child)

public void removeChild(nanoxml.XMLElement child)

public void removeChild(String key)

public void setTagName(String tagName)

public String getTagName()

public void setContent(String content)

public String getContents()

public String toString()

public void write(java.io.Writer writer)

public void write(java.io.Writer writer, int indent)

Some of the quirks of this version of NanoXML are apparent here: we have a method called setContent(), but its accessor is called getContents().This is resolved in NanoXML 2.0 beta. setTagName() and getTagName() set the element name. setContent() and getContents() set the #PCDATA of the node. The write() methods output the node and its subnodes to a java.io.Writer.

Arguments Type Effect
child nanoxml.XMLElement The element to be added or removed to or from the document
key String The name of the attribute to be removed from the XMLElement
tagName String The textual name given to the element
content String The #PCDATA content of the element
writer java.io. Writer The writer to which the element (or document) should be written
indent int The number of spaces to indent for each new nested element
Usage and Examples

Here is an example of how an XML document can be generated and written to a java.io.Writer.

Example: Generating and Writing an XML Document

The XML we generate is a request from our auction site, for the detail information on item numbers 553 and 554.

Sample Request.xml

<?xml version="1.0" encoding="UTF-8"?>
<Request name="ItemDetail">
<Parameters/>
<ItemId type="Integer">553</ItemId>
<ItemId type="Integer">554</ItemId>
</Request>

NanoXML gives us no way to add processing instructions to a document, so we will output the prolog <?xml version="1.0" encoding="UTF-8"?> ourselves. The addProperty() methods haven't been introduced yet, but they will be in the next section.

Source Request.java

import nanoxml.XMLElement;
import java.io.*;
public class Request {
public static void main(String args[]) throws IOException {
//create the document and set the root
XMLElement root = new XMLElement();
root.setTagName("Request");
root.addProperty("name", "ItemDetail");
//create and set the first child
XMLElement child1 = new XMLElement();
child1.setTagName("Parameters");
root.addChild(child1);
//create and set next child
XMLElement child2 = new XMLElement();
child2.setTagName("ItemId");
child2.setContent("553");
child2.addProperty("type", "Integer");
root.addChild(child2);
//create and set the last child
XMLElement child3 = new XMLElement();
child3.setTagName("ItemId");
child3.setContent("554");
child3.addProperty("type", "Integer");
root.addChild(child3);
//create a writer and output the prolog
BufferedWriter bw = new BufferedWriter(new
FileWriter("Request.xml"));
bw.write(new String("<?xml version=\"1.0\" 
encoding=\"UTF-8\"?>"), 0, 38);
//output the document and close the writer
root.write(bw);
bw.close();
}
}
Compiling and Running

To compile Request.java, do the following from the directory in which Request.java is saved:

> javac -classpath \NanoXML\nanoxml.jar;. Request.java

To run Request.class, do the following:

> java -classpath \NanoXML\nano.xml.jar Request

The output is a file in the current directory called Request.xml. It should look like Request.xml in the beginning of this example.


PAGE: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | NEXT PAGE
 





Ready to take that job and shove it?

Function:

Keyword(s):

State:
SPONSOR
RECENT JOB POSTINGS
CAREER NEWS
Go beyond Google and get vertical. These specialized search sites will help you find the business information you need -- fast.

Ari Balogh was named to the post of chief technology officer as the companys for a "realignment" of employees.










InformationWeek U.S. IT Salary Survey 2008
Salaries for business technology professionals are falling. Here's what you need to know in order to make good hiring decisions and personal career choices. Download Today
 
ROLLING RIGHT ALONG
Follow key Network Computing Reviews from conception to completion. This Week: Holistic APM.



Network Computing Reports Emerging Enterprise Podcast Series: Secrets to Success








TechSearch


Microsite of the Week


Powerful Information at Your Fingertips



InformationWeek Business Technology Network
InformationWeekInformationWeek 500InformationWeek 500 ConferenceInformationWeek AnalyticsInformationWeek CIO
InformationWeek EventsInformationWeek ReportsInformationWeek MagazinebMightyByte and SwitchDark Reading
Digital LibraryIntelligent EnterpriseInternet EvolutionNetwork ComputingNo Jitter
space
Techweb Events Network
InteropVoiceConWeb 2.0 ExpoWeb 2.0 SummitEnterprise 2.0 ConferenceMobile Business ExpoSoftware ConferenceCSI - Computer Security Institute
Black HatGTECEnergy CampMashup CampStartup Camp
space
Light Reading Communications Network
Light ReadingLight Reading EuropeUnstrungLight Reading's Cable Digital NewsConstantinopleInternet Evolution
Heavy ReadingLight Reading Live!Light Reading InsiderEthernet ExpoOptical ExpoTeleco TVTower Technology Summit
space
Financial Technology Network
Advanced TradingBank Systems & TechnologyInsurance & TechnologyWall Street & TechnologyAccelerating Wall StreetBank Systems & Technology Executive SummitBuyside Trading SummitInsurance & Technology Executive Summit
space
Microsoft Technology Network
MSDN MagazineTechNetThe Architecture Journal
space
App Infrastructure   |   Messaging & Collaboration   |   Network & Systems Mgmt   |   Network Infrastructure   |   Security  |   Storage & Servers   |   Wireless   |   Enterprise Apps
About Us  |  Contact Us  |  Site Map  |  Technology Marketing Solutions  |  Advertising Contacts  |   Briefing Centers
Copyright © 2008  United Business Media LLC  |  Privacy Statement  |  Terms of Service  |  Your California Privacy Rights