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!

Constructors

You no longer have to construct an XMLElement object unless you are building documents. When parsing documents, the object implementing IXMLBuilder (usually StdXMLBuilder) will provide the root element through its getResult() method (covered below). So you really only need to concern yourself with the following methods if you need to build documents with NanoXML.

public XMLElement()

public XMLElement(String name)

The default constructor is provided for #PCDATA text. To support mixed content in the XMLElement() class, #PCDATA is treated as an XMLElement object with no element name. We'll go into this in more detail in the Child Methods section (page 606), but this point is very important.

Use the default constructor XMLElement() for #PCDATA. Use the other constructors for element nodes.

The name argument represents the name of the new element.

Children Methods

These methods enable access to child elements. They are typically used after parsing a document. Note that all elements in a document, including #PCDATA text, are represented as XMLElement objects. There is no concept of siblings in NanoXML as there is in the Document Object Model (DOM). Each element is a child of the element directly above it.

public int getChildrenCount()

public boolean isLeaf()

public boolean hasChildren()

public Enumeration enumerateChildren()

public Vector getChildren()

public XMLElement getChildAtIndex(int index)

public XMLElement getFirstChildNamed(String name)

public Vector getChildrenNamed(String name)

public Vector getChildren()

Again we see the quirkiness of NanoXML: getChildrenCount() was called countChildren() in version 1.6.7. There is no apparent reason for the name change except perhaps to further the incompatibility between the two releases! Also, isLeaf()and hasChildren() are redundant methods, providing the same information.

The arguments name and index are the name or index of the desired child(ren). enumerateChildren() and getChildren() existed in version 1.6.7 and return an Enumeration or Vector of child XMLElements.

getChildAtIndex() will throw an ArrayIndexOutOfBoundsException if its index argument isn't valid. Likewise, getFirstChildNamed() will return null if no such child with element name name exists.

Usage and Examples

Here's an example that gets all elements named ItemId in an XML document fragment and outputs each element's #PCDATA content. We'll cover the getContent() method in the next section.

Enumeration enum =root.getChildrenNamed("ItemId").elements();
while (enum.hasMoreElements()) {
XMLElement elem = (XMLElement)enum.nextElement();
System.out.println("content is " + elem.getContent());
}
Now let's go over the methods for adding, removing, and accessing individual child elements.

Child Methods

public void addChild(XMLElement child)

public void removeChild(XMLElement child)

public void public void removeChildAtIndex(int index)

public void setContent(String content)

public String getContent()

Arguments Type Effect
child net.n3.nanoxml. XMLElement The element to add or remove to or from the document
index int The index into the document where the first element is 0
name java.lang.String The name of the element

addChild() adds an XMLElement to the document as a child of another element, while removeChild() and removeChildAtIndex() remove an element from a document. The latter provides a very simple XPath-style way of removing children.

setContent() and getContent() allow you to set the #PCDATA content between an element. It is important to know how these methods behave with regards to setName() and getName(), the functions used to get/set the name of an XMLElement. Using setContent() and getContent() incorrectly will break the XMLWriter class, which is used for outputting documents (see Class XMLWriter, page 613). If you create an XMLElement object with the constructor:

public XMLElement(String name)

this creates an element with name name. The correct way to add #PCDATA to this element is to create another XMLElement object using the default constructor:

public XMLElement()

then calling setContent() on the returned object, and adding that object to the first one using addChild(). If you instead call setContent() on the object returned by the named constructor, XMLWriter won't display subelements of that object.

To summarize, here is a code snippet that works just fine:

root = new XMLElement("Request");
XMLElement rootPCDATA = new XMLElement();
rootPCDATA.setContent("An Auction Request");
root.addChild(rootPCDATA);
XMLElement child1 = new XMLElement("Parameters");
root.addChild(child1);
XMLWriter writer = new XMLWriter(System.out); //output the 
document
writer.write(root);
The output of this snippet is:

<Request>
An Auction Request
<Parameters/>
</Request>

and here is a code snippet that does not work fine (even though it looks like it should):

root = new XMLElement("Request");
root.setContent("An Auction Request");
child1 = new XMLElement("Parameters");
root.addChild(child1);
writer = new XMLWriter(System.out); //output the document
writer.write(root);
The output of this snippet is:

<Request>An Auction Request</Request>

You can see that the <Parameters> element is missing.


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 JitterPlug Into The Cloud
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 EvolutionPyramid Research
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