Java & XML, 2nd Edition // Get textual (Web hosting)
Sunday, September 30th, 2007Java & XML, 2nd Edition // Get textual content, normalized (all interior whitespace compressed to // single space. For example, ” this would be ” would be // “this would be” String normalizedContent = element.getTextNormalize( ); As a result, it commonly seems that no Text class is actually being used. The same methodology applies when invoking setText( ) on an element; the text is created as the content of a new Text instance, and that new instance is added as a child of the element. Again, the rationale is that the process of reading and writing the textual content of an XML element is such a common occurrence that it should be as simple and quick as possible. At the same time, as I pointed out in earlier chapters, a strict tree model makes navigation over content very simple; instanceof and recursion become easy solutions for tree explorations. Therefore, an explicit Text class, present as a child (or children) of Element instances, makes this task much easier. Further, the Text class allows extension, while raw java.lang.String classes are not extensible. For all of these reasons (and several more you can dig into on the jdom-interest mailing lists), the Text class is being added to JDOM. Even though not as readily apparent as in other APIs, it is available for these iteration-type cases. To accommodate this, if you invoke getContent( ) on an Element instance, you will get all of the content within that element. This could include Comments, ProcessingInstructions, EntityRefs, CDATA sections, and textual content. In this case, the textual content is returned as one or more Text instances rather than directly as Strings, allowing processing like this: public void processElement(Element element) { List mixedContent = element.getContent( ); for (Iterator i = mixedContent.iterator(); i.hasNext( ); ) { Object o = i.next( ); if (o instanceof Text) { processText((Text)o); } else if (o instanceof CDATA) { processCDATA((CDATA)o); } else if (o instanceof Comment) { processComment((Comment)o); } else if (o instanceof ProcessingInstruction) { processProcessingInstruction((ProcessingInstruction)o); } else if (o instanceof EntityRef) { processEntityRef((EntityRef)o); } else if (o instanceof Element) { processElement((Element)o); } } } public void processComment(Comment comment) { // Do something with comments} public void processProcessingInstruction(ProcessingInstruction pi) { // Do something with PIs} public void processEntityRef(EntityRef entityRef) { // Do something with entity references}
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.