Sei sulla pagina 1di 200

Extensible Markup Language (XML) FAQ From jGuru

Generated Sep 13, 2005 2:36:21 PM

Location: http://www.jguru.com/faq/XML
Ownership: http://www.jguru.com/misc/user-agree.jsp#ownership.

What is XML?
Location: http://www.jguru.com/faq/view.jsp?EID=1092
Created: Nov 16, 1999
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12)

XML stands for eXtensible Markup Language. It is an extensible document language


for specifying document content (i.e. data). Unlike HTML, XML does not contain pre-
defined elements for describing the presentation of the document. For example,
HTML contains predefined <table> and <font> tags that indicate how the data is to
be presented. XML has none of that.

XML, on the other hand, is extensible. It allows the document author to define his
own tags and assign meaning to them. The part of the XML document that
accomplishes this is the Document Type Declaration (DTD). The DTD consists of a
formal definition of the document type. It assigns names for element types and
describes the valid syntax of the document.

Who is standardizing XML?


Location: http://www.jguru.com/faq/view.jsp?EID=1093
Created: Nov 16, 1999 Modified: 2000-03-11 22:11:22.371
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12)

XML is being standardized by the XML Working Group of the World Wide Web
Consortium (W3C). The specification for Version 1.0 was accepted by the W3C on
Feb 10, 1998.

What is XML good for?


Location: http://www.jguru.com/faq/view.jsp?EID=1094
Created: Nov 16, 1999
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12)

XML is good for representing data in a vendor neutral format. It is a good format for
exchanging data between software systems. It separates the content of the data
from the presentation of it.

Who's defining standard DTDs? What are these xMLs anyway?


Location: http://www.jguru.com/faq/view.jsp?EID=1096
Created: Nov 16, 1999
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12)

It seems like almost every kind human endeavor has some kind of group that is
standardizing on DTDs that are relevant to them. By defining DTDs, they are able to
exchange tagged data. The tags represent something about the semantics of the
data. Often they name the resulting language defined by the standard DTD as
something-ML. For example, a group defining exchange of real estate data (listings
of houses, vacation properties, etc.) defined some DTDs and called the resulting
language relML.

A partial list of efforts to standardize DTDs is available here.

Comments and alternative answers

The link to the partial listing of standardized DTD's...


Author: Meadowlark Bradsher (http://www.jguru.com/guru/viewbio.jsp?EID=16913),
Jan 4, 2001
The link to the partial listing of standardized DTD's is no longer valid.

What is the DOM?


Location: http://www.jguru.com/faq/view.jsp?EID=1097
Created: Nov 16, 1999
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12)

The DOM stands for the Document Object Model. It defines a programmatic API for
accessing XML documents. The XML document is represented as a tree. Using the
DOM API, a programmer can construct, query, traverse and manipulate XML
documents programmatically.

XML parsers consume XML documents and then typically produce trees that can be
accessed using the DOM API.

Where can I get an XML->DOM parser?


Location: http://www.jguru.com/faq/view.jsp?EID=1098
Created: Nov 16, 1999 Modified: 1999-11-17 07:10:07.381
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12)

IBM's Alphaworks site has XML parsers written in Java and C++. Both produce DOM
trees.

Sun's project X also has an XML parser written in Java. It produces a DOM trees.

There are also various open-source XML parsers available.

Comments and alternative answers

Oracle provides XML Parsers to analize XML documents...


Author: Gianfranco Pietraforte (http://www.jguru.com/guru/viewbio.jsp?EID=3534),
Dec 27, 1999
Oracle provides XML Parsers to analize XML documents using either DOM or SAX
standard. Anybody can download them from the Oracle Technology Network (OTN)
site: http://technet.oracle.com (registration is free)

There is an open source port of HTML Tidy to Java ...


Author: Wiebe de Jong (http://www.jguru.com/guru/viewbio.jsp?EID=2065), Jan 13,
2000
There is an open source port of HTML Tidy to Java called JTidy which will also
produce a DOM tree.

Apache group has a open source projects Xerces and...


Author: Vladimir Zyrianov (http://www.jguru.com/guru/viewbio.jsp?EID=33305),
Apr 10, 2000
Apache group has a open source projects Xerces and Xalan - Java and C++ XML
parser (SAX and DOM) and XSL processor. See more details on
http://xml.apache.org

Re: Apache group has a open source projects Xerces and...


Author: Oleg Pilipenko (http://www.jguru.com/guru/viewbio.jsp?EID=404550),
Apr 17, 2001
Can not figure out how to use Xerces for the HTML DOM parsing. Any ideas?

From which languages can I use the DOM?


Location: http://www.jguru.com/faq/view.jsp?EID=1099
Created: Nov 16, 1999 Modified: 1999-11-17 07:11:32.372
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12)

The DOM is actually specified in a language independent fashion using OMG IDL.
Thus, in theory any language for which there is an IDL binding could be supported.
In practice, Java is the most popular language for accessing XML documents using
the DOM.

What are Xbeans?


Location: http://www.jguru.com/faq/view.jsp?EID=1100
Created: Nov 16, 1999
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12)

An Xbean is a software component that takes XML as input, processes it in some


fashion and then passes XML on to the next Xbean.

Xbeans are Java Beans. Java Bean technology supports the packaging, reuse,
connection and customization of Java code. With the appropriate set of Xbeans and a
Java Bean design tool, it is possible to build useful distributed applications with little
or no programming! See the white paper "Creating Distributed Applications Using
Xbeans" for a more detailed discussion of Xbeans and applications that use them.

Xbeans.org is an open source project to build a repository of useful reusable xbeans.


Please visit xbeans.org, if you want to contribute.

So XML represents data content, what about data presentation?


Location: http://www.jguru.com/faq/view.jsp?EID=1101
Created: Nov 16, 1999
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12)
Stylesheets are used to present XML. A stylesheet describes how to present data.
The description is separate from the data.

The Cascading Stylesheet Specification (CSS) is a language for associating styles and
elements. Cascading Stylesheets have been used in browsers with HTML. Microsoft
Internet Explorer 5 also uses cascading style sheets with XML.

The Extensible Style Language (XSL) was designed to be used with XML. XSL is
described in XML. That is, a style sheet is just and XML file.

Where can I get the specifications?


Location: http://www.jguru.com/faq/view.jsp?EID=1102
Created: Nov 16, 1999
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12)

• Extensible Markup Language (XML) 1.0 Specification


• DOM
• XSL
• Cascading Style Sheets

Comments and alternative answers

An excellent annotated XML specification is available...


Author: Kent Johnson (http://www.jguru.com/guru/viewbio.jsp?EID=3211), Dec 22,
1999
An excellent annotated XML specification is available from xml.com.

XSL: I get namespace attributes showing up all over my result document.


How do I get rid of them?
Location: http://www.jguru.com/faq/view.jsp?EID=4888
Created: Jan 13, 2000 Modified: 2000-01-14 06:46:52.487
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)

If you include a namespace in the root element of your source document, a spec-
compliant XSLT processor is supposed to duplicate it into the resulting document.
This is often not what you want. To get rid of it, there's an attribute you can include
in the XSL stylesheet:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pp="http://medwired.com/namespaces/practiceportal/1.0"
xmlns:xlink='http://www.w3.org/XML/XLink/0.9'
exclude-result-prefixes="pp xlink"
>

exclude-result-prefixes is a white-space delimited list of namespaces to supress


in the output. You can also include this directive as <xsl:exclude-result-prefixes> for
portions of an xsl transform (I think).

(Thanks to Brian Dupras, briand@centera.com, for solving this.)


Is there a mailing list for discussion of XML technologies with the Java
platform?
Location: http://www.jguru.com/faq/view.jsp?EID=9971
Created: Jan 29, 2000 Modified: 2000-01-30 13:34:23.897
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

Sun manages the XML-INTEREST list. You can signup and read the archives at
http://archives.java.sun.com/archives/xml-interest.html.

There are also the Apache XML mailing lists. See http://xml.apache.org/mail.html.

How can I convert the .txt files to xml document? Are there any reliable
softwares available, or can I do it with the help of Java?
Location: http://www.jguru.com/faq/view.jsp?EID=11615
Created: Feb 4, 2000 Modified: 2000-02-09 21:39:35.666
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12)

Windows .txt files are files that contain human readable ascii text. There is no further
structure to it. XML files are also human readable but reflect natural structure in the
file's data. Unfortunately, there is no magic. You need to understand the format of
the data in your text files, define a DTD to reflect that structure and then represent
the data in your .txt files in XML.
Comments and alternative answers

clear view on handling XML as String


Author: cool gomes (http://www.jguru.com/guru/viewbio.jsp?EID=1159553), Apr 1,
2004
say if i put the content of an XMl file as String and passing to parsing to fetch back
the data . ex: File name :"example.xml" <root> <child1>childone</child1>
<child2>childtwo</child2> <child3>childthree</child3> </root> String
xmlSource="<root><child1>childone</child1>"+ "<child2>childtwo</child2>"+
"<child3>childthree</child3></root>"; parse(xmlSource) to getback the data as XML
flat file parsing. i done like this, but i endedup with "NullPointerException" could u
give a solution ????

Re: clear view on handling XML as String


Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432),
Apr 10, 2004
I am not sure I understand your question. But this FAQ might help:

http://bobcat.webappcabaret.net/javachina/faq/xml_java_01.htm#jaxp_sax_Q05

Where can I find examples of using the XML Java API?


Location: http://www.jguru.com/faq/view.jsp?EID=12969
Created: Feb 10, 2000 Modified: 2000-04-24 19:37:45.919
Author: Gianfranco Pietraforte (http://www.jguru.com/guru/viewbio.jsp?EID=3534)
Question originally posed by dinesh krn
(http://www.jguru.com/guru/viewbio.jsp?EID=11899
There are several places to find sample code including the Oracle technology site, the
Xbeans project, IBM DeveloperWorks XML site and Sun's XML site.

What is meant by xml transformation?


Location: http://www.jguru.com/faq/view.jsp?EID=19086
Created: Feb 28, 2000 Modified: 2000-03-30 06:52:38.242
Author: Pavel Tavoda (http://www.jguru.com/guru/viewbio.jsp?EID=19085)
Question originally posed by srinivas komma
(http://www.jguru.com/guru/viewbio.jsp?EID=11177

XML transformation is the transformation of an XML document supporting one format


(DTD) to another XML document of another format. One use of XML transformation is
to generate an HTML presentation of an XML document.

Where can I find a DTD for JSP?


Location: http://www.jguru.com/faq/view.jsp?EID=23846
Created: Mar 13, 2000 Modified: 2001-06-14 22:29:34.885
Author: Maxim Senin (http://www.jguru.com/guru/viewbio.jsp?EID=21992)
Question originally posed by Morar Calin
(http://www.jguru.com/guru/viewbio.jsp?EID=21221

According to Sun, it's impossible to write a DTD for JSP because it's not XML-
complient. It cannot even be called 100% derived from SGML. But, you may use a
non-validating XML parser to parse JSP - just set DTD to null, or use non-validating
parser that doesn't use DTD at all.
Comments and alternative answers

There is a link in Sun's DTD Page...


Author: arul senthil (http://www.jguru.com/guru/viewbio.jsp?EID=490540), Nov 21,
2001
There are links for DTD/Schema for JSP, in the Sun's DTD Page for J2EE.
It is located at : http://java.sun.com/dtd/.
It has DTD as well as Schema for JSP.

Re: Where can I find a DTD for JSP?


Author: Adam Sroka (http://www.jguru.com/guru/viewbio.jsp?EID=1215330), Dec 8,
2004

There is an alternative XML compliant syntax for JSP which is not often used. You
can find an article describing its use here
http://www.onjava.com/pub/a/onjava/2001/11/28/jsp_xml.html. It conforms to a
specific DTD found here http://java.sun.com/dtd/jspxml.dtd or XML Schema here
http://java.sun.com/dtd/jspxml.xsd

Are there any commerical tools, with a Java API, that would allow dynamic
PDF document generation?
Location: http://www.jguru.com/faq/view.jsp?EID=23945
Created: Mar 14, 2000 Modified: 2000-03-16 23:17:38.248
Author: Jérôme Bernard (http://www.jguru.com/guru/viewbio.jsp?EID=23090)
Question originally posed by Gary Moh
(http://www.jguru.com/guru/viewbio.jsp?EID=23320

It seems that there is no commercial packages but however, there are many free
ones. Here is a short list of these tools:

• http://www.lowagie.com/iText/
• http://www.retep.org.uk/pdf/
• http://www.etymon.com/pj (this one offers consulting services)
• http://xml.apache.org/fop/ (the well-known XML to PDF generator)

Comments and alternative answers

http://www-4.ibm.com/software/developer/education/...
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7), Jun 20,
2000
http://www-4.ibm.com/software/developer/education/transforming-
xml/xmltopdf/index.html also describes the process of converting an XML file to
PDF.

XML to PDF
Author: Bruno Lowagie (http://www.jguru.com/guru/viewbio.jsp?EID=492448),
Sep 7, 2001
iText is able to do XML to PDF conversion too.
Take a look at the Chap07xx examples at this URL:
http://www.lowagie.com/iText/examples/

Re: http://www-4.ibm.com/software/developer/education/...
Author: Sean O'Leary (http://www.jguru.com/guru/viewbio.jsp?EID=862615),
May 1, 2002
another nice package is at www.xmlpdf.com

Commercial Tool
Author: Joshua Aversa (http://www.jguru.com/guru/viewbio.jsp?EID=842794), Apr
17, 2002
I know of one commercial tool sold by http://www.sitraka.com/ (formaly klgroup).
They sell JClass DesktopViews and JClass ServerViews. In DesktopViews they have
a PageLayout component that allows client side creation of PDFs and in ServerViews
they have a ServerReport that generates PDFs from a server.

Re: Commercial Tool


Author: Santosh M (http://www.jguru.com/guru/viewbio.jsp?EID=1151825), May
5, 2004
FREE Java PDF generation APIs is available at called Smart JPrint.
http://www.activetree.com
It not only generates PDF for ant kind of (server and front end)Java programs, it
lets you send the output to a selected printer (incase you want to print the output),
allows you to preview the PDF output in a nice GUI window so that your user can
view the pages generated from your program. User can then output the selected
pages or range of pages to a printer or to a PDF file. Use the preview feature when
you are using the Smart JPrint APIs in the Swing GUI programs.

It is just wanderful with the following features.

• 100% Java and needs no other library.


• You can convert RTF documents into PDF from swing components such as
JTextPane, JEditorPane, JTextArea and other kind of custom
JTextComponent. JTable content too can be easily converted to PDF.
• THE NICE THING WITH this APIs is that it automatically breaks the
lines and columns to create number of pages and uses the PAGE FORMAT
YOU PROVIDE.
• It has a System.out.println kind of APIs to output the TEXT (with RTF
styles, e.g. COLOR, FONT, UNDERLINE), ICONS/IMAGES, and 2D
Graphics using its write() methods. The lines are automatically broken into
pages.

Look at the online APPLET demo that lets you get the test of PDF generation
online and instantly.

How can I prevent text that contains "" or ">" from interfering with
parsing. Are there other characters I need to be concerned about?
Location: http://www.jguru.com/faq/view.jsp?EID=24552
Created: Mar 15, 2000 Modified: 2000-04-04 11:44:06.728
Author: Robert Quinn (http://www.jguru.com/guru/viewbio.jsp?EID=18022)
Question originally posed by Robert Quinn
(http://www.jguru.com/guru/viewbio.jsp?EID=18022

A simple XML "encode" should do the trick.


/**
* Escape the "special" characters as required for inclusion in XML
elements
* Replaces all incidences of
* & with &
* < with <
* > with >
* " with "
* ' with &apos;
* @param s The string to scan
* @return String
*/
public static String escapeStringForXML(String s) {
char[] array = s.toCharArray();
StringBuffer sb = new StringBuffer();
for(int i=0; i < array.length; i++) {
switch(array[i])
{
case '&':
sb.append("&");
break;
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '"':
sb.append(""");
break;
case '\'':
sb.append("&apos;");
break;
default:
sb.append(array[i]);
}
}
return sb.toString();
}
Comments and alternative answers

See http://www.w3.org/TR/REC-xml#syntax for more info....


Author: Alex Smith (http://www.jguru.com/guru/viewbio.jsp?EID=24722), Nov 27,
2000
See http://www.w3.org/TR/REC-xml#syntax for more info.

Is there any other way to declare a schema for XML other than DTD's ?
Location: http://www.jguru.com/faq/view.jsp?EID=30438
Created: Mar 30, 2000 Modified: 2000-04-04 12:38:18.174
Author: Seok-jeong Hwang (http://www.jguru.com/guru/viewbio.jsp?EID=30431)
Question originally posed by Anil Punjabi
(http://www.jguru.com/guru/viewbio.jsp?EID=23218

There are many proposals for describing xml schema but none have reached the
point that they are in products.

A good point to start is the XML schema primer.

The technical notes include:

• XML-Data
• DCD.
• DDML.

Comments and alternative answers


One of the upcoming standards right now is the XDR...
Author: Christoph Krüger (http://www.jguru.com/guru/viewbio.jsp?EID=30208),
May 10, 2000
One of the upcoming standards right now is the XDR (XML Data Reduced)
introduced by Microsoft via the BizTalk network. According to the German promotor
of BizTalk which I met on a convention recently Microsoft is actively involved in the
procedure to make XDR a W3C recommendation.
Products: The latest MS XML parser can validate XML data against a schema written
in XDR.

MS IE 5.0 supports a schema called XML Schema which...


Author: Saravanan Jayachandran
(http://www.jguru.com/guru/viewbio.jsp?EID=91462), Aug 20, 2000
MS IE 5.0 supports a schema called XML Schema which is considered a subset of
XML-Data(working draft at W3C). Datatypes(ints,floats,booleans,dates) for elements
and attributes can be specified in XML Schema. Unlike DTDs, there is no need to
learn a specilized syntax for modelling data when you are using Xml Schema as it is
based on XML syntax. The content model is open and name space integration is
supported.

What is XSLT?
Location: http://www.jguru.com/faq/view.jsp?EID=31864
Created: Apr 4, 2000 Modified: 2000-12-03 20:12:42.776
Author: Thijs Stalenhoef (http://www.jguru.com/guru/viewbio.jsp?EID=31863)
Question originally posed by Gaurav Marballi
(http://www.jguru.com/guru/viewbio.jsp?EID=5750

XSLT is the part of XSL used to transform XML documents to HTML, PDF etc. or other
XML documents. It can help you publish an XML document in a variety of formats.
For instance, it can be used to convert an XML document in a data format used by a
particular company to the XML data format used by another company.

How is XML better than prior languages such as DHTML?


Location: http://www.jguru.com/faq/view.jsp?EID=31874
Created: Apr 4, 2000 Modified: 2000-04-28 11:26:34.448
Author: Thijs Stalenhoef (http://www.jguru.com/guru/viewbio.jsp?EID=31863)
Question originally posed by Javed Karbari
(http://www.jguru.com/guru/viewbio.jsp?EID=3868

(From Mathew Cobby) XML is purely "data about data". i.e. there is no concept of
HOW something should look, it might not even have a visual representation. XML is
about shoving data around and then doing something application specific with it.
DHTML is about creating pages and how they should look (and behave). In XML there
is no concept of a page, just nodes of data. XML is really a backend tool while DHTML
is more of a front end tool.

(From Thijs Stalenhoef) It is tempting to compare HTML to XML, but they are in fact
two quite different beasts which both share the same ancestor, namely SGML
(Standard Generalized Markup Language). HTML gives data a certain look. XML on
the other hand gives data meaning. XML documents say nothing about how the data
they include should be represented. That is up to other systems such as XSL that can
convert XML documents to (amongst other things) HTML.

Simple example: say you are building a website that will contain a lot of books. in
HTML you could do something like:
XML What's it all about

Author: N.B. Inparticular

Price: $25.45

Available: Yes

This will give you 4 lines of text about the book. In XML you would do something
similar:

<Book Available="yes">
<Title>XML What's it all about</Title>
<Author>N.B. Inparticular</Author>
<Price>$25.45</Price>
</Book>

The same essential information is present, only in XML is because really easy for an
automated system to detect the structure of the document and for instance store it
in a database or convert it to HTML, PDF or something else. Check out the XML
tutorials on www.webmonkey.com for a slightly more in depth discussion of XML and
what it means. The book "The XML Handbook" published by Prentice Hall is very
good material also.

How does one decide when to use an attribute or an element for


representing data? For example, it is possible represent an order as

<ORDER ITEM="XXXX" QTY=10 UNIT="PC" />

or as

<ORDER>
<ITEM>XXXX</ITEM>
<QTY>10</QTY>
<UNIT>PC</UNIT>
</ORDER>

Location: http://www.jguru.com/faq/view.jsp?EID=31906
Created: Apr 4, 2000 Modified: 2000-04-28 14:18:52.053
Author: Thijs Stalenhoef (http://www.jguru.com/guru/viewbio.jsp?EID=31863)
Question originally posed by Nilesh Shah
(http://www.jguru.com/guru/viewbio.jsp?EID=1810
(From Thijs Stalenhoef) Although technically the above two orders are the same
there are a couple of things you might want to consider.
Elements can contain other elements, attributes do not have this kind of functionality
(i.e. their content has no explicit structure). So if you want to replace for instance
content between <UNIT> and </UNIT> with something more complex then "PC" (for
instance something containing further XML tags!) you'll run into trouble with the first
implementation. Ordering is another thing.
Attributes may only appear once in an element and their order may be completely
random. Elements however must always appear in the same order and can occur as
often as you like (or as your schema/DTD says they can occur).

Ways to think about attributes are as data that is of lesser importance, meta-data or
properties. You are free to implement elements and attributes as you see fit. There
are no real rules. Just keep the above in mind when setting up an XML document.

(From Benoit Xhenseval) One "standard" approach is to use elements to represent


data fields and attributes to represent metadata and key.

The rationale is that:

• elements are that elements make your design easier to extend than attributes
• It is difficult to model complex structures using attributes
• the attribute support is not uniform amongst parsers
• elements can be enforced by a DTD

So, in you example, I would definitely go for your second option.

Comments and alternative answers

You can find a good explanation on when to use att...


Author: Nicola Ken Barozzi (http://www.jguru.com/guru/viewbio.jsp?EID=39153),
Apr 29, 2000
You can find a good explanation on when to use attributes and when to use elements
in The Java API for Xml Parsing (JAXP) Tutorial by Eric Armstrong at the Javasoft
web site, at 4. Designing an XML Data Structure at paragraph Attributes and
Elements.

Could you describe the architecture behind jGuru.com: JSP, Servlets,


database, servers, transactions etc...?
Location: http://www.jguru.com/faq/view.jsp?EID=34896
Created: Apr 11, 2000 Modified: 2002-11-20 12:23:54.439
Author: Terence Parr (http://www.jguru.com/guru/viewbio.jsp?EID=1) Question
originally posed by Benoit Xhenseval
(http://www.jguru.com/guru/viewbio.jsp?EID=3363

[Updated Nov 20, 2002 to remove old description and point at some articles. TJP]

jGuru.com Case-Study.

Little Nybbles of Development Wisdom.

Can I use WML (wireless markup language) from a Java Server Page?
Location: http://www.jguru.com/faq/view.jsp?EID=35988
Created: Apr 13, 2000 Modified: 2000-05-12 08:13:30.136
Author: Shaun Childers (http://www.jguru.com/guru/viewbio.jsp?EID=30243)
Question originally posed by RAMESH NIMMAKAYALA
(http://www.jguru.com/guru/viewbio.jsp?EID=23087

Follow these steps for using WML instead of HTML from within JSP.

1) Make sure that the following is added to your MIME types file of your web server
and restart it: File Extension = .wml MIME Type = text/vnd.wap.wml

2) (From this point forward, you would do the following inside your servlets just as
you would inside your JSP files.) In your JSP file set your response type to text/wml
(normally it's text/html or text/plain for HTML).

3) Instead of outputting HTML code inside your JSP file, you just output WML code.
You can do this different ways: you can just use the

out.println("<wml>");
...
out.println("<card>");
...
or you could create WML classes in Java and just have a class for each WML tag
(WML is a small set of HTML, so you will have around 30 classes). Each WML class
would appropriately handle the syntax specific to itself and handle you placing data
into this syntax. Each class would also have a toString() method. From here, in your
JSP file, wherever you want your WML code, just write Java (you have the WML
classes), then to output the WML code, you would write out.println(wml.toString());
The wml.toString() method would call all of the other classes' (Card, Select, etc.)
toString() method to output all of the WML syntax. This design works well because if
the WML syntax changes and you have 100 JSP files with WML syntax inside, you
would have to change every one on them; using the WML classes, if the WML syntax
changes, you just have to change that particular class associated to whatever tag
changed, and then recompile your class.

Now, keep in mind that there is one problem with outputting WML through JSP - if
your web phone is trying to call a JSP file (not through a servlet), and there is a
problem in which the JSP file outputs an error, whatever is handling your JSP files will
probably output an error in HTML format, not WML, and therefore your phone
browser will not be able to interpret this and will give you an error page built into the
phone browser. For example, if you are using JRun to run your JSP files and it is not
up and running, you will receive an error in HTML. You could develop an error
handling system for capturing JSP errors and if you are expecting WML, you could
return an error page in WML.

Comments and alternative answers

Wml and jsp related


Author: Umair Shahid (http://www.jguru.com/guru/viewbio.jsp?EID=1204477), Oct
10, 2004
I am facing a problem when i call jsp file from wml file. This Jsp file is a simple one
and working good in IE. But when it was called from wml file it does not show any
results. I am using Nokia Toolkit 4.0 but Tomcat 4.0 for jsp. Both file code is given. I
want the answer of this imediately. ass.wml ?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.3//EN"
"http://www.wapforum.org/DTD/wml13.dtd"> <wml> <card id="card1" title="Card
#1">

TO SEE THE RESULTS:


Enter the Serial No:<input name="ser" format="*N"/> <anchor> Go <go
href="http://localhost:8080/Wap/test.jsp#home"> <postfield name="ser"
value="$ser"/> </go> </anchor>

</card> 2-test.jsp <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE wml


PUBLIC "-//WAPFORUM//DTD WML 1.3//EN"
"http://www.wapforum.org/DTD/wml13.dtd"> <wml> <%@ page
contentType="text/vnd.wap.wml" %> <% String str =
request.getParameter("ser");%> <card id="home" title="home">

You sent the value = <%=str %>

</card> </wml>

Is there an easier way to create a DTD file? Are there tools that can help me
do so?
Location: http://www.jguru.com/faq/view.jsp?EID=38211
Created: Apr 20, 2000 Modified: 2000-04-29 08:37:18.777
Author: Egil Ølberg (http://www.jguru.com/guru/viewbio.jsp?EID=38147) Question
originally posed by Anil Punjabi
(http://www.jguru.com/guru/viewbio.jsp?EID=23218

(From Egil Ølberg) Yes, the tool I use can be downloaded at:

• IBM AlphaWorks

(From Benoit Xhenseval) Yes there are. It is also recommended to use some tools as
the DTDs will evolve into XML Schema soon and those tools have early suuport for
schemas.

Here are 2 recommended tools:

• XML Spy (www.xmlspy.com)


• XML Authority (www.extensibility.com - probably most advanced...)

Comments and alternative answers

XML Spy can generate a DTD from an XML document.


Author: ashok veludandi (http://www.jguru.com/guru/viewbio.jsp?EID=224020), Oct
6, 2000
XML Spy can generate a DTD from an XML document.

There is a web service called DTDGenerator that can...


Author: jean christophe dugas
(http://www.jguru.com/guru/viewbio.jsp?EID=329041), Feb 14, 2001
There is a web service called DTDGenerator that can generate a DTD from an XML
document.

Can I get an example that works on documents which are not DTD-specific
but generic?
Location: http://www.jguru.com/faq/view.jsp?EID=38686
Created: Apr 21, 2000 Modified: 2000-04-24 20:00:47.231
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12) Question
originally posed by shravan mishra
(http://www.jguru.com/guru/viewbio.jsp?EID=38208

The xBeans project includes many examples of Java code that works on documents
of any DTD. Such code requires you to essentially interpret the DOM representation
of the document and not depend on any particular elements, attributes or document
structure.

Why would XML be a better solution than a typical delimited text file for B2B
(business to business applications), or representing data in general? I can
take a flatfile, I can parse it and extract data, and then present that data in
different ways using java such as HTML or WML. So, why would I want to
complicate things with DTDs, parsers, and APIs needed to extract and
display data?
Location: http://www.jguru.com/faq/view.jsp?EID=39592
Created: Apr 24, 2000 Modified: 2000-04-28 14:30:37.382
Author: Nicola Ken Barozzi (http://www.jguru.com/guru/viewbio.jsp?EID=39153)
Question originally posed by Matt Woody
(http://www.jguru.com/guru/viewbio.jsp?EID=36408

There are many possible answers; XML can be used, as ASCII text files, in many
ways. In fact it is also called the ASCII of the future (even if ASCII an XML define
two different things).
In B2B the main reason is that it separates the data contained in a document and its
structure from the presentation. This means that if you want to change the
presentation you can simply change the XSL (stylesheet) and keep the data
unchanged. Not to mention how it is easy to change a stylesheet compared to parse
it and extract data, and then present that data in different ways using java such as
HTML or WML.
But the main advantage is that it is easy to convert XML to XML having a different
DTD or schema. For example, firm A has to send a purchase order to firm B. It sends
it in XML. Suppose also that firm B keeps track of purchase orders written in XML.
Even if the two firms use different XML DTDs or schemas it is easy to use XSL to
change from a format to another. Now firm B can insert the order in its database.
Now imagine doing this with a a typical delimited text file ...
On the site W3 Consortium site there is XML in 10 points.

Comments and alternative answers

Could you please explain what is meant by different...


Author: Venugopal YK (http://www.jguru.com/guru/viewbio.jsp?EID=72566), Jul 1,
2000
Could you please explain what is meant by different DTDs? Do you mean copies of
the same DTD or entirely different DTD's.

Response: The idea is that there are standard industry wide DTDs for exchanging
data. Using XSLT technology, two enterprises can exchange data according to the
standard DTD. XSLT allows each enterprise to translate its XML data to and from the
standard.

Re: Could you please explain what is meant by different...


Author: Murali K (http://www.jguru.com/guru/viewbio.jsp?EID=943917), Jul 10,
2002
Here are good links which will answer all your questions on XSL and B2B
together. http://davis.wpi.edu/dsrg/xtra/#publications
http://www.vbxml.com/xml/articles/buildingxml/

What is the most efficient way to create XML documents from Java objects
and vice versa?
Location: http://www.jguru.com/faq/view.jsp?EID=40590
Created: Apr 26, 2000 Modified: 2000-05-01 08:51:25.623
Author: Nicola Ken Barozzi (http://www.jguru.com/guru/viewbio.jsp?EID=39153)
Question originally posed by Bernhard Brandmair
(http://www.jguru.com/guru/viewbio.jsp?EID=36062

There is not AN afficient way, it depends on different parameters, because efficiency


is not only how fast you parse but also how easily you do it.
Here are some examples and guidelines.

• Parsing a big XML file: you should use the SAX API, which is event driven;
this means that the document is not duplicated in memory and the parsing is
fast, but is limiting beacuse you cannot go back and forth on the tree or add
or subtract branches (input only);
• Parsing a small XML file: the DOM API is much easier to use (remember to
include the DTD so that unmeaningfull white spaces do not interfere with
parsing), and the memory usage is small; you gain also in flexibility, because
you can go back and forth on the tree, add or subtract branches and output
the tree (not part of the DOM standard API but usually present in
implementations);
• Writing XML files with basically the same structure: just output the tags
as String like

out.println("<MYTAG>"+InTagString+"</MYTAG>");
• Writing XML files with variable structure: use the DOM API , which
represent the XML as a tree; it is easy to bind a DOM tree to a JTree
component, the code that does it is in the Jva XML Tutorial on the Javasoft
site.

Now Sun is writing a binding specifications to tie the XML to Java classes (JSR-
000031 XML Data Binding Specification), but at the time of the writing it isn't ready.
Detailed info on the use of it is at White Papers about XML Technology .
Comments and alternative answers

Some alternatives
Author: Stefan Stefansson (http://www.jguru.com/guru/viewbio.jsp?EID=1119203),
Aug 10, 2005
There are a few alternatives nowadays:

XML Pull Parsing:


Excellent for reading, not sure whether/how well it handles writing though.

• http://www.xmlpull.org (Official API)


• http://www.extreme.indiana.edu/xgws/xsoap/xpp/ (Implementation)
• http://kxml.objectweb.org/ (Another Implementation)

For the purpose of the original question, serializing Java Objects to and from XML
files, one of the coolest things I've seen is XStream:

• http://xstream.codehaus.org/

Regards, Stefan.

How do you print the contents of an XML document using the DOM, i.e.
using the org.w3c.dom.* interfaces?
Location: http://www.jguru.com/faq/view.jsp?EID=42105
Created: Apr 28, 2000 Modified: 2000-04-29 08:05:08.976
Author: Pramod Hirole (http://www.jguru.com/guru/viewbio.jsp?EID=41443)
Question originally posed by Gene De Lisa
(http://www.jguru.com/guru/viewbio.jsp?EID=38746

The org.w3c.dom.* package provides a Node interface which defines methods such
as "getNodeName()" ,"getNodeType()", "getNodeValue()" for getting the information
about the current node. There are also other methods such as "getPreviousSibling()",
"getNextSibling()", "getChild()" for traversing the XML document.

Given a reference to Document you can traverse through the document using the
above methods, printing each node as it is visited.

Comments and alternative answers


We use the IBM parser named xml4j. You can directly...
Author: Srinivas Thutika (http://www.jguru.com/guru/viewbio.jsp?EID=46992), May
10, 2000
We use the IBM parser named xml4j. You can directly print the contents of an xml
document using an extended API.

If you use Apache's xerces API, you can print a do...


Author: Dmitriy Yuriev (http://www.jguru.com/guru/viewbio.jsp?EID=324416), Feb
8, 2001
If you use Apache's xerces API, you can print a document like so:
public static void prettyPrint(Document document)
{
try
{ OutputFormat format = new OutputFormat(document, "UTF-8", true);
format.setLineSeparator("\r\n");
format.setLineWidth(72);
format.setIndent(2);
format.setPreserveSpace(true);
XMLSerializer serializer = new XMLSerializer(System.out, format);
serializer.serialize(document);
} catch (IOException e) {
System.err.println(e);}
}
Most of the OutputFormat stuff ins't necessary, just makes it look better. It's an
expensive call, though.

What are XML namespaces? Please include some examples.


Location: http://www.jguru.com/faq/view.jsp?EID=42761
Created: Apr 30, 2000 Modified: 2000-05-01 08:47:26.123
Author: Pramod Hirole (http://www.jguru.com/guru/viewbio.jsp?EID=41443)
Question originally posed by paoluo paoluo
(http://www.jguru.com/guru/viewbio.jsp?EID=41783

XML namespaces are simple way to give unique names to elements and attributes
used in XML document. Namespaces enables the programmers to select the XML
tags for processing depending upon the application requirment.
<?xml version="1.0"?>

<book xmlns='www.amazon.com:books'
xmlns:isbn='urn:ISBN:0-395-36341-6'>
<title>Cheaper by the Dozen</title>
<isbn:number isbn:type='Hard_Copy'>123456789<isbn:number>
<notes>

Best book on XML!


</notes>
</book>
Comments and alternative answers

use of namespaces
Author: prasenjit narwade (http://www.jguru.com/guru/viewbio.jsp?EID=498645),
Sep 21, 2001
...and when are they used ?

I am attempting to use the XML parser provided by sun jaxp1.0. When I try
to get an instance of DocumentBuilderFactory using the static method
newInstance(), it throws a FactoryConfigurationError exception at runtime.
How can i do to overcome this?
Location: http://www.jguru.com/faq/view.jsp?EID=44859
Created: May 4, 2000 Modified: 2000-05-09 15:43:34.101
Author: M.Kumar Sankar (http://www.jguru.com/guru/viewbio.jsp?EID=31021)
Question originally posed by M Kumar Sankar
(http://www.jguru.com/guru/viewbio.jsp?EID=43788

You have to include the following class in the import section and set the classpath to
that directory. import com.sun.xml.parser.DocumentBuilderFactoryImpl;
Comments and alternative answers

Alternate solution if that didn't work


Author: Jeffrey Hoyt (http://www.jguru.com/guru/viewbio.jsp?EID=317239), May
10, 2001
It seems the order of the jar files in the classpath is vital. Make sure you use the
following order:

jaxp.jar
crimson.jar
xalan.jar

How can I map data from a DB2 UDB for AS/400 into an XML document and
the associated DTD ?
Location: http://www.jguru.com/faq/view.jsp?EID=45449
Created: May 6, 2000 Modified: 2000-05-09 15:55:15.531
Author: Carlos Ortiz (http://www.jguru.com/guru/viewbio.jsp?EID=45447) Question
originally posed by leonardo polon
(http://www.jguru.com/guru/viewbio.jsp?EID=38637

Since V4R2 of the OS400 IBM has released two Java packages the JT400.jar and
DB2_CLASSES.zip. With these you have access to almost all the funcionality of the
AS400 system (program calls, dataqueues, spool files, etc..). It also implements a 4
level JDBC Driver and record level access to physical files.

There is an article, "XML APIs for databases", published in JavaWorld -


www.javaworld.com - This article defines "a simple, pure Java implementation of XML
APIs for databases that works with any JDBC data source. With this approach, XML
tools can treat a database as a virtual XML document".

With these tools I think you have all the information to map as400 datasources to
XML files.

Has anyone parsed an XML dtd to create a DOM object? I don't know if that
is possible or not, if it isn't how could I get an object containing all the
attributes allowed for a given element?
Location: http://www.jguru.com/faq/view.jsp?EID=46977
Created: May 10, 2000 Modified: 2000-05-15 20:43:47.941
Author: Christoph Krüger (http://www.jguru.com/guru/viewbio.jsp?EID=30208)
Question originally posed by Gregory Van Ass
(http://www.jguru.com/guru/viewbio.jsp?EID=34607

You might want to have a look at the Oracle XML class generator. This neat little Java
API allows you to load a DTD and have each element generated as .java class files.
In a second step you can use these class files to build an application which generates
DOM-objects according to the DTD. It's all pretty well documented in the included
samples, so I'm leaving that to you.
Comments and alternative answers

It is not what I am looking for. I might not have ...


Author: Gregory Van Ass (http://www.jguru.com/guru/viewbio.jsp?EID=34607), May
16, 2000
It is not what I am looking for. I might not have explained this well enough. I am
writing an XML editor and I would like the end-user to be able to chose the element
he is going to insert from a list of possible elements for the given node, instead of
typing the whole element's name.

Can I design a form which consists of textboxes, submit button, reset


button, dropdown list using XML?
Location: http://www.jguru.com/faq/view.jsp?EID=47673
Created: May 11, 2000 Modified: 2000-05-15 20:50:27.915
Author: M.Kumar Sankar (http://www.jguru.com/guru/viewbio.jsp?EID=31021)
Question originally posed by srinivas komma
(http://www.jguru.com/guru/viewbio.jsp?EID=11177

Certainly you can. But for that you need to use XSL technology. Include an XSL
stylesheet at the begining of your XML with a processing instruction. In the XSL file
you can provide form and text boxes and so on. The XSL file may look as follows.
<?xml version="1.0" ?>
<xml:stylesheet mlns:xsl="http://www.w3.org/TR/WD-xsl>
<xsl:template match="/" >
<xsl:apply-templates select="/records/name" />
<xsl:template>
<xsl:template match="name[end()]" >
<html>
<body>
<form action="http://xxxx/servlet/yy">
<input type=text name="tt">
<xsl:attribute name="value"><xsl:value-of />
<xsl:attribute>
</form>
</body>
</html>
<xsl:template>
<xsl:stylesheet>
The main thing to observe here is the attribute values you want to provide
dynamically should be generated by using
<xsl:attribute name="xxx"> xxx value <xsl:attribute>
template. The given sample will create a form with one text field, and assigns the
value of the last name element in your XML file.
Comments and alternative answers

How do you make <option>s selected with XSL?


Author: code freak (http://www.jguru.com/guru/viewbio.jsp?EID=593770), Dec 22,
2001
how do you populate a value into a select list -- how do you add the "selected" bit to
the <option> tag with XML? since it's not a name-value pair that can be called an
attribute.

What is WML?
Location: http://www.jguru.com/faq/view.jsp?EID=50957
Created: May 17, 2000 Modified: 2000-09-12 19:35:10.307
Author: Niki Scevak (http://www.jguru.com/guru/viewbio.jsp?EID=49639) Question
originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

WML is a markup language based on XML and is intended for use in specifying
content and user interface for narrowband devices, including cellular phones and
pagers.
Comments and alternative answers

"WML" stands for "Wireless Markup L...


Author: Bill Day (http://www.jguru.com/guru/viewbio.jsp?EID=135825), Sep 16,
2000
"WML" stands for "Wireless Markup Language". It is a standard language specified
as a part of the Wireless Application Protocol (WAP) suite of specifications by the
WAP Forum.

WML was developed from the earlier proprietary markup language HDML (Handheld
Device Markup Language).

I am currently using an XSLT processor to render XML (into HTML)


generated from an EJB. However I also want some of my HTML to include
HTML forms (input boxes, selection boxes etc). Does anyone have any
examples where they are doing something similar?? How do you handle the
POSTs and GETs??
Location: http://www.jguru.com/faq/view.jsp?EID=53076
Created: May 21, 2000 Modified: 2000-05-24 08:48:52.873
Author: Thijs Stalenhoef (http://www.jguru.com/guru/viewbio.jsp?EID=31863)
Question originally posed by Bhupesh Wagjiani
(http://www.jguru.com/guru/viewbio.jsp?EID=47596

The first part of this question was answered by M.Kumar Sankar here.

How you handle your POSTs and GETs will depend entirely on how your system
works. You can define the form action to be whatever you want it to be. If you want
to store the data submitted somehow however it might be handy to convert the
submitted data back to XML. Something like:

<fieldname>value</fieldname>

should do the trick for most simple forms. This should keep your "display" code clean
since you won't have to display anything other than XML.

If the amount of stored data gets bigger you might want to look at using an XML
store of some sort. Products like eXcelon, Tamino etc. might be what you need.

What is RSS (Rich Site Summary)?


Location: http://www.jguru.com/faq/view.jsp?EID=56964
Created: May 30, 2000 Modified: 2000-05-30 16:08:24.092
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Originally known as "RDF Site Summary", RSS, the Rich Site Summary format is the
de facto standard meta-data format for news headlines over the web.
WebReference's RSS page contains a number of helpful links to information on RSS.

What is the best way to convert HTML to XML, separating the content(data)
from presentation in HTML? Are there any Java API's that we can make use
of?
Location: http://www.jguru.com/faq/view.jsp?EID=66681
Created: Jun 6, 2000 Modified: 2000-10-11 09:33:33.868
Author: Brill Pappin (http://www.jguru.com/guru/viewbio.jsp?EID=60239) Question
originally posed by Praveen Paranjay
(http://www.jguru.com/guru/viewbio.jsp?EID=40359

Try Cocoon and ECS, or JetSpeed from the Apache group (java.apache.org).

Thijs Stalenhoef adds:

There aren't really any APIs specifically for doing this. What do you want to do with
the HTML-data after it has been converted to XML? If all you want to do is display it
again using different style-sheets you should consider converting the HTML to
XHTML. XHTML is simply an XML compliant form of HTML

If you want to convert the HTML to XML compliant with a DTD or schema of your own
making then doing it can pose many problems. It all depends on the HTML. Do all
the HTML-files use the same "template"? Then it is possible to write a program to
convert it. If they are all different it is probably easy to do it by hand.
Comments and alternative answers

convert HTML to XML


Author: amit shukla (http://www.jguru.com/guru/viewbio.jsp?EID=870149), May 7,
2002
Hope this can help : public org.w3c.dom.Document
executeXML(org.w3c.dom.Document request) { return _execute(request) ; } public
org.w3c.dom.Document execute(HTTPServletRequest iRequest)
{ org.w3c.dom.Document request = iRequest.getRequestDoc(); return
_execute(request) ; } public org.w3c.dom.Document
_execute(org.w3c.dom.Document request) { String xml = this.execute(request);
org.w3c.dom.Document response = parser.parse(xml); xml = null; return response; }

if objective is for parsing HTML, JTIDY or JDK's HTML parser can be


considered
Author: Avani Shah (http://www.jguru.com/guru/viewbio.jsp?EID=28330), Jul 23,
2003
JTIDY is an open source and HTML parser is available in JDK1.4 (swing parser)

The answer is JEDI


Author: Daniel Matyas (http://www.jguru.com/guru/viewbio.jsp?EID=445610), Jan
20, 2005
JEDI - the most complex project solving this problem.
http://www.ipsi.fraunhofer.de/oasys/projects/jedi/ Good luck

I am in a project in which I want to display the data fetched from a data


base. I use a java bean to get the data and pass it to a JSP. From that I
want to make use of XML technology to print the data to the user browser. I
am using an XSL file. After getting the data from the data base how can I
manage that into an XML file? Do I have to create an XML file dynamically?
If so should I have to store it on the disk? And how can I bind an XML file to
a XSL file? I want to change the style sheet (XSL) each time.
Location: http://www.jguru.com/faq/view.jsp?EID=64722
Created: Jun 6, 2000 Modified: 2000-06-06 16:55:43.004
Author: Thijs Stalenhoef (http://www.jguru.com/guru/viewbio.jsp?EID=31863)
Question originally posed by M.Kumar Sankar
(http://www.jguru.com/guru/viewbio.jsp?EID=31021

1. Data from database to XML: Depends on the complexity of your data. A simple
table is easily convert like:
<fieldname1>data</fieldname1>
<fieldname2>data</fieldname2>
etc.

When you have linked tables use the primary keys to creates a more hierarchical
representation:

<field1fromtable1>data</field1fromtable1>
<field2fromtable1> (suppose this is primary key)
<field1fromtable2>data</field1fromtable2>
etc. etc.
</field2fromtable1>
etc.

For more a more indepth discussion take a look at The XML Handbook or a similar
publication.

2. Should I create XML dynamically?

Some databases can generate XML directly (Oracle 8i being one of them). The
method described above can be used when your database doesn't do this.

3. Store it on disk yes/no?

Storing it on disk will of course increase performance but lose flexibility. In other
words, if you store in on disk you'll have to check every time whether or not the data
you need has changed in the database. If so you'll have to generate the XML file
again. If your conversion code is fast enough you don't have to save it into a file
everytime. You might consider doing it anyway as a form of result caching though.

4. Binding XML to XSL

Just make your XML header look something like this:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="somestylesheet.xsl" ?>

The second line make the reference to the stylesheet you want to use. When given
an XML containing this line IE5 for instance will simply get the XSL in question and
use it to transform the XML for you. You can change this line everytime you output
the XML. If you are serving up XML from disk you might want to leave out the header
and add it at the time you are serving it to the client. A simple servlet should do the
trick.

Comments and alternative answers

Display XML in browser


Author: bradley gordan (http://www.jguru.com/guru/viewbio.jsp?EID=490569), Oct
11, 2001
I really need to point this out because it saved my project:
I am receiving real-time XML data and data updates from a database and via SOAP
connections.
This XML data is then translated into objects e.g. graphs or lists and displayed in my
browser.
I developed an entire application (including wap) with software from a third party
vendor on www.altio.com in less than 1 week :-).

Bradley.
To Dynamically generate xml and save it into hard disk and then create its xsl and then generate th
Author: pravin sharma (http://www.jguru.com/guru/viewbio.jsp?EID=1161628), Apr 28, 2004
GenerateXml.java//This file will connect to to ur database fetch records and store it in the disk as an xml
import java.io.*; import java.sql.Driver; import java.util.*; public class GenerateXml { public void getDa
new Properties(); prop.put("user","sa"); prop.put("password","sa"); Connection con = null; Statement stm
null; List user = new ArrayList(); List dept = new ArrayList(); List mail = new ArrayList(); try { Driver d
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); con =
driver.connect("jdbc:microsoft:sqlserver://SRV03\\PREDATOR_O:1433;user=sa;password=sa;DatabaseN
stmt = con.createStatement(); rs = stmt.executeQuery("select * from Employee"); while (rs.next()){ Strin
(rs.getString("username")); String department = (rs.getString("department")); String email = (rs.getString
user.add(username); dept.add(department); mail.add(email); } ByteArrayOutputStream baos = new ByteA
PrintStream ps = new PrintStream(baos); ps.println("<?xml version=\"1.0\"?>\r\n"); ps.println("<Templat
user.size(); Iterator itr1 = user.iterator(); Iterator itr2 = dept.iterator(); Iterator itr3 = mail.iterator(); while(
ps.println("<UserName>"); ps.print(itr1.next()); ps.println("</UserName>"); ps.println("");ps.println("<p
ps.println(itr2.next()); ps.print("");ps.println("</product>"); ps.print("");ps.println("<email>"); ps.println(
ps.println("</email>"); } ps.println("</Template>"); FileOutputStream fos = new FileOutputStream("Emp
baos.writeTo(fos); fos.close(); }catch (Exception e){ e.printStackTrace();} } public static void main(Strin
xml = new GenerateXml(); xml.getData(); } } then generate the xsl code Employee.xsl <?xml version="1
8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="/"> <fo:root
xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master maste
margin-right="1.0cm" margin-left="1.0cm" margin-bottom="2cm" margin-top="1cm" page-width="21cm
> <fo:region-body margin-top="1cm" margin-bottom="1.0cm"/> <fo:region-before extent="1cm"/> <fo:r
extent="1.5cm"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference
flow-name="xsl-region-body"> <xsl:apply-templates select="Template"/> </fo:flow> </fo:page-sequence
</xsl:template> <xsl:template match="Template"> <fo:block font-size="10pt" font-family="verdana" col
align="center"> <xsl:apply-templates select="UserName"/> <xsl:apply-templates select="product"/> <xs
select="email"/> </fo:block> </xsl:template> <xsl:template match="UserName"> <fo:block> <xsl:apply-
select="UserName"/> </fo:block> </xsl:template> <xsl:template match="UserName"> <fo:block> <xsl:v
</fo:block> </xsl:template> <xsl:template match="product"> <fo:block> <xsl:apply-templates select="pr
</xsl:template> <xsl:template match="product"> <fo:block> <xsl:value-of select="."/> </fo:block> </xsl
match="email"> <fo:block> <xsl:apply-templates select="email"/> </fo:block> </xsl:template> <xsl:tem
<fo:block> <xsl:value-of select="."/> </fo:block> </xsl:template> </xsl:stylesheet> and then write the co
using fop. XmlToPdf.java package intentia; import java.io.ByteArrayOutputStream; import java.io.File; i
java.io.FileOutputStream; //import org.apache.avalon.framework.logger.ConsoleLogger; //import
org.apache.avalon.framework.logger.Logger; import org.apache.fop.apps.Driver; import org.apache.fop.a
org.apache.fop.apps.XSLTInputHandler; public class XmlToPdf { public void PDF () { try { Driver drive
driver.setRenderer(Driver.RENDER_PDF); //Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_
//driver.setLogger(log); File xml = new File("Employee.xml"); File xsl = new File("Employee.xsl"); Inpu
new XSLTInputHandler(xml,xsl); FileOutputStream filePDF = new FileOutputStream("Employee.pdf");
driver.setOutputStream(filePDF); driver.render(inputHandler.getParser(),inputHandler.getInputSource());
out = new ByteArrayOutputStream(); byte[] content = out.toByteArray(); filePDF.write(content); driver.r
filePDF.close(); } catch (Exception e) { System.out.println(" error is " + e); } } public static void main(St
test = new XmlToPdf(); test.PDF(); } } This will generate your pdf
I am generating an XML file dynamically from a Java program and I want
the same to display to the browser with the help of XSL file. I want to select
the XSL file dynamically. How can I do that?
Location: http://www.jguru.com/faq/view.jsp?EID=64656
Created: Jun 6, 2000 Modified: 2000-06-06 17:08:26.448
Author: Thijs Stalenhoef (http://www.jguru.com/guru/viewbio.jsp?EID=31863)
Question originally posed by M.Kumar Sankar
(http://www.jguru.com/guru/viewbio.jsp?EID=31021

In order for the browser (IE 5 is the only shipping browser at this time to support
XML and XSL) to know which XSL you want it to use you have to have to header that
looks something like this:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="yourstylesheet.xsl" ?>

in your XML. The second line defines which stylesheet will be used. Since you are
generating your XML dynamically, changing this second line to point to another XSL
file should be easy.

Comments and alternative answers

Display XML in browser


Author: bradley gordan (http://www.jguru.com/guru/viewbio.jsp?EID=490569), Oct
11, 2001
I am receiving real-time XML data and data updates from a database and via SOAP
connections.
This XML data is then translated into objects e.g. graphs or lists and displayed in my
browser.
I developed an entire application (including wap) with software from a third party
vendor on www.altio.com in less than 1 week :-).
Bradley.

How can I "serialize" Swing user interfaces as XML documents?


Location: http://www.jguru.com/faq/view.jsp?EID=70422
Created: Jun 9, 2000 Modified: 2000-06-09 10:02:25.62
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)

There is an article at The Swing Connection that explains this process:

http://java.sun.com/products/jfc/tsc/articles/persistence/index.html

After generating an XML file dynamically from a Java program I want the
generated file to be displayed on the client's browser. How can I do that?
Location: http://www.jguru.com/faq/view.jsp?EID=46915
Created: Jun 9, 2000 Modified: 2000-06-09 16:27:25.756
Author: M.Kumar Sankar (http://www.jguru.com/guru/viewbio.jsp?EID=31021)
Question originally posed by M.Kumar Sankar
(http://www.jguru.com/guru/viewbio.jsp?EID=31021
IBM's XML parser for Java provides a TXDocument class in which there is a method
printWithFormat(writer). Give the servletResponseWriter to this argument so that the
document will be displayed on to the client's browser, provided the browser supports
the XML file format. When generating the XML file dynamically be careful to ensure
that there are no null values, otherwise an exception will be thrown by
printWithFormat.
Comments and alternative answers

I tried using the printWithFormat method in the TX...


Author: Uthpala Varadarajan (http://www.jguru.com/guru/viewbio.jsp?EID=94445),
Jul 5, 2000
I tried using the printWithFormat method in the TXDocument class but I'm having a
problem trying to generate an element recursively. Any ideas or examples for this type
of generation of an XML document?

I have code in which I'm using XLinks. I'm not getting any error but the
XLink is not working. I am using IE5. What is the reason for this?
Location: http://www.jguru.com/faq/view.jsp?EID=64657
Created: Jun 9, 2000 Modified: 2000-06-09 16:37:59.852
Author: Thijs Stalenhoef (http://www.jguru.com/guru/viewbio.jsp?EID=31863)
Question originally posed by Siva Prasad Balaraju
(http://www.jguru.com/guru/viewbio.jsp?EID=46435

IE 5 does not support XLink or XPointers at the moment. A reason for this might be
that XLink is still a "working draft" at W3.org.

A way to work around this for the time being might be to use an XSL to transfer
them to normal HREF's.

Comments and alternative answers

Re. Xlinks Problem


Author: Ravi R (http://www.jguru.com/guru/viewbio.jsp?EID=938498), Jul 4, 2002
Hi siva, Can you send me the code to me including th XSL. I am having the same
problem but using Ie 6.0. I want to try your code..

How can I convert XML file to PostScript?


Location: http://www.jguru.com/faq/view.jsp?EID=86663
Created: Jul 1, 2000 Modified: 2000-07-01 11:03:18.514
Author: Thijs Stalenhoef (http://www.jguru.com/guru/viewbio.jsp?EID=31863)
Question originally posed by Dmitri Rogov
(http://www.jguru.com/guru/viewbio.jsp?EID=51189

If you are familiar with the PostScript language you could simply write an XSL to do
the work for you. The actual work can then be done using some form of XSL-
Transformation tool (Xalan is one, http://xml.apache.org").
There are some commercial packages that do it for you (sorta) but they are
generally very expensive.

Comments and alternative answers

Some suggestions for further information regarding...


Author: Frank Falkenberg (http://www.jguru.com/guru/viewbio.jsp?EID=25357), Jul
8, 2000
Some suggestions for further information regarding the XML to PDF transformation:

1. From the Apache-Pages:

FOP is the world's first print formatter driven by XSL formatting objects. It is a Java
application that reads a formatting object tree and then turns it into a PDF document.
The formatting object tree, can be in the form of an XML document (output by an
XSLT engine like XT or Xalan) or can be passed in memory as a DOM Document or
(in the case of XT) SAX events.

FOP is part of Apache's XML project. The homepage of FOP is:


http://xml.apache.org/fop.

2. From Peter T. Mount (alias http://www.retep.org.uk):

The retepPDF library allows any Java application that is capable of printing, to
generate PDF (portable Document Format) files. These files can then be published on
the Internet, sent by mail, and then viewed either in a web browser (using the Adobe
Acrobat PlugIn) or by using a suitable PDF viewer.
Licencing:
The PDF library is distributed under the GNU General Library Public Licence as a
precompiled jar file, and as source in tar.gz format.

You can find the homepage under: http://www.retep.org.uk/pdf/

3. From IBMs webpages:

An interesting tutorial to this topic can be found under:


http://www-4.ibm.com/software/developer/education/transforming-
xml/xmltopdf/index.html

As a caveat: I have not tried out any of these in a serious project, so I cannot give any
kind of information regarding usability or performance etc.

How do you convert from:


<users>
<user firsname="John" lastname="Joe" article="One"/>
<user firsname="Jack" lastname="Jill" article="Three"/>
<user firsname="John" lastname="Joe" article="Two"/>
</users>
to
<users>
<user>
<username>
<first>John</first>
<last>Joe</last>
</username>
<articles>
<article>One</article>
<article>Two</article>
</articles>
</user>
<user>
<username>
<first>Jack</first>
<last>Jill</last>
<username>
<articles>
<article>Three</article>
</articles>
</user>
</users>
Is it possible with XSLT?

Location: http://www.jguru.com/faq/view.jsp?EID=91201
Created: Jul 5, 2000 Modified: 2000-07-05 12:32:38.189
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by Nicola Ken Barozzi
(http://www.jguru.com/guru/viewbio.jsp?EID=39153
Yes, it is possible. Use the following XSL file.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:variable name="my-user-list" select="//user" />
<xsl:variable name="my-user-list2"
select="//user[not(./@firstname=following::user/@firstname)]" />
<xsl:template match="/">
<users>
<xsl:for-each select="$my-user-list2">
<xsl:variable name="my-user" select="." />
<user>
<username>
<first>
<xsl:value-of select="$my-user/@firstname" />
</first>
<last>
<xsl:value-of select="$my-user/@lastname" />
</last>
<articles>
<xsl:for-each select="$my-user-list">
<xsl:variable name="my-user2" select="." />
<xsl:if test='//*[$my-user/@firstname = $my-
user2/@firstname]'>
<article>
<xsl:value-of select="$my-user2/@article" />
</article>
</xsl:if>
</xsl:for-each>
</articles>
</username>
</user>
</xsl:for-each>
</users>
</xsl:template>
</xsl:stylesheet>
The output is as follows:
<users>
<user>
<username>
<first>Jack</first>
<last>Jill</last>
<articles>
<article>Three</article>
</articles>
</username>
</user>
<user>
<username>
<first>John</first>
<last>Joe</last>
<articles>
<article>One</article>
<article>Two</article>
</articles>
</username>
</user>
</users>

Are there any SOAP-specific FAQs available?


Location: http://www.jguru.com/faq/view.jsp?EID=96437
Created: Jul 5, 2000 Modified: 2000-07-05 12:57:41.922
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

The DevelopMentor SOAP FAQ is probably the best place to start. Microsoft also has a
SOAP FAQ.

We are getting the inputs using HTML forms (mostly text box). The
validations (like not empty) are done using Java Script. After the successful
validation we have to generate XML document by using Java script and the
same will be passed to EJB via JSP. Are there any solutions for generating
XML using Java Script?
Location: http://www.jguru.com/faq/view.jsp?EID=94679
Created: Jul 5, 2000 Modified: 2000-07-05 13:06:53.57
Author: Simon Brown (http://www.jguru.com/guru/viewbio.jsp?EID=44588)
Question originally posed by vetrivel chidambaram
(http://www.jguru.com/guru/viewbio.jsp?EID=87192

Whilst there don't seem to be any solutions for building XML using JavaScript, there
is another, maybe easier way that you can achieve the same result.

After validating the text fields on the client-side, you could POST the contents of the
form back to another JSP. This JSP could use one of the various XML libraries
available (e.g. Xerces from Apache or JAXP from Sun) to build the XML document for
subsequently passing it to your EJB.
Comments and alternative answers

Please note, that from a security point of view, you...


Author: Jörg Schaible (http://www.jguru.com/guru/viewbio.jsp?EID=116737), Aug 2,
2000
Please note, that from a security point of view, you cannot guarantee, that the data
your server receives is really valid, since anyone can fake your form very easily by
using a package with functionality like httpunit (see JUnit. So you have to do the
validation on the server side anyway, the client side validation is just optional.

Re: Please note, that from a security point of view, you...


Author: Leonard Grove (http://www.jguru.com/guru/viewbio.jsp?EID=540634),
Nov 6, 2001
True. You should do the validation on the server as opposed to using Javascript on
the client side. Check out the free version of ezVerify at www.ezverify.com. It
performs the validation (and even modifies the data if you want) on the server and
then forwards the results to wherever you to forward to.

I am generating XML and using XSL I am able to display it on the IE5


browser. But not all browsers support XML & XSL. So I would like to convert
that XML and XSL to HTML in order to display it on all browsers. Is this
possible? How?
Location: http://www.jguru.com/faq/view.jsp?EID=64654
Created: Jul 5, 2000 Modified: 2000-07-05 15:40:29.051
Author: Thijs Stalenhoef (http://www.jguru.com/guru/viewbio.jsp?EID=31863)
Question originally posed by Srinivas Thutika
(http://www.jguru.com/guru/viewbio.jsp?EID=46992

Converting XML and XSL to HTML is exactly what IE5 does internally. This process is
called a transformation. XML is transformed, using an XSL stylesheet, to HTML. There
are many ways to do XSLT (XSL Transformation) and a lot of people/companies have
written Java classes to do this. Some are free other aren't.

I recommend you check out xml.apache.org for Xalan. This is a free and open-source
XSLT (and XPath!) engine and should be all you need.

Comments and alternative answers


Xalan is probably the slowest XSLT engine. You might...
Author: Roshan Shrestha (http://www.jguru.com/guru/viewbio.jsp?EID=130068), Feb
27, 2001
Xalan is probably the slowest XSLT engine. You might want to use XT
(www.jclark.com)-probably the fastest Java-based XSLT processor (though I believe
James Clark has stopped further development on it). You can also try out
Saxon(http://users.iclway.co.uk/mhkay/saxon/index.html).

How can an XML document like this be created with the JAXP API?

<address>
<name City="New York">
<first>John</first>
<last>Joe</last>
</name>
</address>
<address>
<name City="Boston">
<first>Jack</first>
<last>Jill</last>
</name>
</address>

Location: http://www.jguru.com/faq/view.jsp?EID=80888
Created: Jul 5, 2000 Modified: 2000-07-05 15:47:51.968
Author: M.Kumar Sankar (http://www.jguru.com/guru/viewbio.jsp?EID=31021)
Question originally posed by Jens Hilbert
(http://www.jguru.com/guru/viewbio.jsp?EID=59910
To generate an XML document using Jaxp the following sample code can help you.
import java.io.*;

import javax.xml.parsers.*;

import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import org.w3c.dom.Text;

import com.sun.xml.parser.DocumentBuilderFactoryImpl;
import com.sun.xml.parser.*;
import com.sun.xml.tree.*;

public class JaxpDoc


{
public static void main(String a[])
{
try{
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element root = doc.createElement("root");
Element address = doc.createElement("address");
Element name = doc.createElement("name");
name.setAttribute("City","New York");
Element first = doc.createElement("first");
first.appendChild(doc.createTextNode("John"));
Element last = doc.createElement("last");
last.appendChild(doc.createTextNode("Joe"));
name.appendChild(first);
name.appendChild(last);
address.appendChild(name);
root.appendChild(address);
doc.appendChild(root);
XmlDocument tdoc = (XmlDocument)doc;
tdoc.write(new PrintWriter(System.out));
System.out.println("Document created successfully ...");
} catch(Exception e)
{
e.printStackTrace();
}
}
}

Note that it is one way. You can use XmlDocument object directly to get an
Docuemnt instance and use the above same methods to generate document. You can
place it in a loop so that any number of address nodes can be created.

Where can I get basic information about XML? Are there any good tutorials?
Location: http://www.jguru.com/faq/view.jsp?EID=91089
Created: Jul 5, 2000 Modified: 2000-07-05 16:38:33.084
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by chetan chandawalla
(http://www.jguru.com/guru/viewbio.jsp?EID=69365

Several people provided good answers to this question.

From Davanum Srinivas:

Two good starting points for XML are http://www.javasoft.com/xml and


http://www.xml.org/.

There is a very good tutorial at http://java.sun.com/xml/tutorial_intro.html.

From Harish Pai:

Check out the WDVL site for XML tutorials.

From Curtis Hatter: There are of good places to get information on xml.

1. www.xml.com
2. www.xml.org
3. www.webmonkey.com
4. www.w3.org

If you are a java developer you may wanna also check out these links.

1. www.alphaworks.ibm.com/formula/XML
2. www.java.sun.com/xml

There are plenty of others but those are good starting points. And the IBM and SUN
sites provide tutorials and information on how to use their Java XML parsers.

From Bruce Martin:

I describe a few easy steps to begin accessing XML data in Java in Lowering the
bar of the DOM API article at IBM's developerWorks.

From M.Kumar Sankar:

The best book for XML is web. As this is still evolving technology, there are very few
books that cover entire XML. Even some of them may be very specific to onces
implementations. Some of the best sites to go through are:

1. W3C
2. xml.com
3. xml101.com
4. xmlinfo.com

In addition to these Sun, Oracle and IBM sites are also providing information in their
sites. The tutorials are mentioned in those sites.

How do I write and deploy a simple hello world program that uses XML and
can be seen in a browser. What is the required software to start working
with XML?
Location: http://www.jguru.com/faq/view.jsp?EID=58437
Created: Jul 6, 2000 Modified: 2000-07-06 11:45:08.93
Author: swarna kumar Reddy (http://www.jguru.com/guru/viewbio.jsp?EID=44506)
Question originally posed by deepak ghosh
(http://www.jguru.com/guru/viewbio.jsp?EID=54313

Several people have contributed to answering this question:

From Thijs Stalenhoef


To answer you second question first:
A text editor is really all you need.
XML is a human-readable format which is a lot like HTML in *appearance*. As such
you can create XML documents using a simple text-editor. Graphical tools for creating
XML are available but not necessary.

Remember though that XML is "nothing more" then a way to describe data in
structured way. A "hello world"-like program using XML doesn't really exist, as XML
isn't a programming language.
If you want to use Java to output XML that can be viewed in a browser you should
use Java Server Pages or a Java Servlet that outputs XML. You do this in a way
analoguous to outputting HTML using JSP or Servlets.

From swarna kumar Reddy


To display XML file in a browser it requires corresponding XSL file.
Hello.xml is the XML file:

<?xml version="1.0"?>
<?xml:stylesheet type="text/xsl" href="Hello.xsl"?>
<Greet>
<wish>
<info>Hello World</info>
</wish>
</Greet>

Hello.xsl is the XSL file:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template match="/">
<html>
<head>
<title> Hello World Application </title>
</head>
<body>
<xsl:for-each select="Greet/wish">
<xsl:value-of select="info" />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Now open the Hello.xml file in a browser that supports XML, such as IE 5.0.

From ganesh vednere


To deploy any XML program on a browser you would need to convert that XML into
HTML. HTML conversion is performed by applying XSL templates on the XML
document. This can be done on the browser (only for IE5) or by writing code to
process the XSL and XML. Processors can be downloaded from the apache.org
website. Look for Xalan /Xerces. The Xalan processor will process the XML and XSL
and generate either HTML or XML. You can do this either at command line or by
writing Java code on the server (e.g. servlet).

From Majid Imtiaz


I recommend the following to get fammiliar with XML codding. http://www-
4.ibm.com/software/developer/education/xmlintro/xmlintro-1-4.html

I have two XML documents and want to combine them into one single
document. How do I go about doing this?
The rules are simple,
1. If an attribute is present in both files but have different values, then the
one in the 2nd file should be put into the combined document.
2. If the 2nd file has some tags/attributes that are not present in the 1st file
then it should be added to the combined document.

I'm using Xerces-Java downloaded from xml.apache.org.


Location: http://www.jguru.com/faq/view.jsp?EID=98079
Created: Jul 7, 2000 Modified: 2000-07-13 11:46:13.467
Author: hong zhang (http://www.jguru.com/guru/viewbio.jsp?EID=53935) Question
originally posed by Anuj Seth (http://www.jguru.com/guru/viewbio.jsp?EID=50429

Please check out IBM's alpha works page. The XMLTreeDiff utility can be used to
perform your task.

Is any method in Java which can create the Doctype parameter in the XML
document, that is to produce !DOCTYPE Response SYSTEM
"MyResponse.dtd"> ?
Location: http://www.jguru.com/faq/view.jsp?EID=99425
Created: Jul 10, 2000 Modified: 2000-07-19 11:18:04.743
Author: Oliver Springauf (http://www.jguru.com/guru/viewbio.jsp?EID=32379)
Question originally posed by Sonu TheKool
(http://www.jguru.com/guru/viewbio.jsp?EID=3468

There is no such method in the DOM spec. However, the implementation you are
using might provide one. In Sun's JAXP implementation, there is the method
XmlDocument.setDoctype(String dtdPublicId, String dtdSystemId, String
internalSubset) that you can use. Unfortunately, there is no official documentation
on the implementation, and you are losing your independence from the
implementation when you are calling these methods.

Is there a way to generate XSL from HTML? That is, the generated style
sheet would represent the style of the HTML page.
Location: http://www.jguru.com/faq/view.jsp?EID=102196
Created: Jul 14, 2000 Modified: 2000-07-16 20:52:09.006
Author: Håkan Cervin (http://www.jguru.com/guru/viewbio.jsp?EID=43294)
Question originally posed by stephane giner
(http://www.jguru.com/guru/viewbio.jsp?EID=99360

No. The XML to HTML transformation is a many to many relation. One HTML page
can be generated by several different style sheets, depending on the data in the XML
file.

Can one transfer images & other BLOB objects using XML? How?
Location: http://www.jguru.com/faq/view.jsp?EID=103038
Created: Jul 15, 2000 Modified: 2000-07-19 11:12:26.523
Author: Software Framework (http://www.jguru.com/guru/viewbio.jsp?EID=61156)
Question originally posed by Anil Punjabi
(http://www.jguru.com/guru/viewbio.jsp?EID=23218
You may want to use mime to hold the XML and the binary data. Alternatively you
can use a CDATA block or you can encode the binary data using base64 encoding.
Finally, you can just store a URL to the XML document that points to the data.

I am working on the data communication between two applications. I am


using an XML file that is generated dynamically in one application and
sending it to the other through RMI. On the other end I want to parse the
XML document object to get the information in that object. In this process,
if I use SAX, it is giving exception that document should contain root
element. But if I use the same document object with DOM API it is working
properly. Where is the problem? How can I use SAX API to parse where I
have only a Document object, but not the physical XML file stored on the
disk?
Location: http://www.jguru.com/faq/view.jsp?EID=103043
Created: Jul 15, 2000 Modified: 2000-07-19 12:12:28.444
Author: Software Framework (http://www.jguru.com/guru/viewbio.jsp?EID=61156)
Question originally posed by M.Kumar Sankar
(http://www.jguru.com/guru/viewbio.jsp?EID=31021

Print out the xml data where you are getting


the SAX exception. Make sure it is still
well formed.

If the document looks like this:


<element1/>
<element2/>

then I don't think it is well formed.


It should look like:
<root>
<element1/>
<element2/>
</root>

(Obviously, You can use any name you like


for 'root', 'element1' and 'element2'.)
It is also possible that the dom parser you
are using accepts the first case and adds
a dummy root element for you or ignores
the <element2/> text.
Comments and alternative answers

Missing document root when SAX parsing


Author: Andy Caulkett (http://www.jguru.com/guru/viewbio.jsp?EID=231266), Dec
21, 2001

I encountered this problem using SAX. The proposed answer is correct: the problem
is that SAX parsing expects the stream to contain a single well formed document ... so

<e1>data</e1>
<e2>datatoo</e2>
looks like two documents with root elements e1 & e2. So as suggested wrap the
whole transmission on the stream:

<XML-STREAM>
<e1>data</e1>
<e2>datatoo</e2>
</XML-STREAM>

We have successfully used this on XML streams that are open for hours to send
messages from one app to another.

Re: Missing document root when SAX parsing


Author: Stefan Muscat Doublesin
(http://www.jguru.com/guru/viewbio.jsp?EID=724205), Jan 17, 2002
Hi,

I have the same problem, and after trying solutions I still remain with the same
problem! (Error: SAXException during parseXML():The root element is required
in a well-formed document.)

Here is the complete XML file.

<XML-STREAM>
<e1>data</e1>
<e2>datatoo</e2>
</XML-STREAM>

That is as basic as it can get. Am I missing something?

Any help would be appreciated!

How do I render an HTML page using XSL if I have a style sheet and the
data reside in a data object? I do not want to output the data to a text file
and parse it.
Location: http://www.jguru.com/faq/view.jsp?EID=103048
Created: Jul 15, 2000 Modified: 2000-07-19 12:02:24.339
Author: Software Framework (http://www.jguru.com/guru/viewbio.jsp?EID=61156)
Question originally posed by khien siow
(http://www.jguru.com/guru/viewbio.jsp?EID=55971

You don't have to output the objects to a file. You can output to a String and then
parse the string.

If you don't want to convert your objects to an xml document (in a file or string)
then you to have generate the DOM objects or SAX events so that then can be fed to
a XSLT processor. Xalan from xml.apache.org can take input from DOM and SAX
event generators.

How do I transfer XML documents over http?


Location: http://www.jguru.com/faq/view.jsp?EID=103049
Created: Jul 15, 2000 Modified: 2000-07-17 08:15:11.534
Author: Software Framework (http://www.jguru.com/guru/viewbio.jsp?EID=61156)
Question originally posed by ankur jain
(http://www.jguru.com/guru/viewbio.jsp?EID=2875

Set the content type to text/xml. An XML file is just a text file. You only have to
change the content type so that server or client knows what it is being sent.

Can I validate a DOM using a DTD and return the errors to a logfile?
Location: http://www.jguru.com/faq/view.jsp?EID=103050
Created: Jul 15, 2000 Modified: 2000-07-19 12:00:10.352
Author: Software Framework (http://www.jguru.com/guru/viewbio.jsp?EID=61156)
Question originally posed by Venkata R Kotte
(http://www.jguru.com/guru/viewbio.jsp?EID=20756

You have to read the documentation of your DOM parser. Typically validation is done
on XML strings before the DOM is generated. Check out the docs for xerces at
www.apache.org.
Comments and alternative answers

To turn on validation call: parser.setFeature(&quo...


Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7), Jan 7, 2001
To turn on validation call:
parser.setFeature("http://xml.org/sax/features/validation",
true);

Where can I get an XML parser/DOM implementation that allows me to


programmatically query the DTD?
Location: http://www.jguru.com/faq/view.jsp?EID=103052
Created: Jul 15, 2000 Modified: 2000-07-18 10:02:03.292
Author: Software Framework (http://www.jguru.com/guru/viewbio.jsp?EID=61156)
Question originally posed by sheshu kumar
(http://www.jguru.com/guru/viewbio.jsp?EID=93396

The IBM parser extends the DOM to support querying of the DTD.

How can I use in XSL "value-of" to get the content of a tag, which includes
other tags. I tried that and I got only the content of all tags but I also want
to have the other tags.
Location: http://www.jguru.com/faq/view.jsp?EID=103053
Created: Jul 15, 2000 Modified: 2000-07-17 08:13:23.251
Author: Software Framework (http://www.jguru.com/guru/viewbio.jsp?EID=61156)
Question originally posed by Nermin Denizer
(http://www.jguru.com/guru/viewbio.jsp?EID=94409
You should use xsl:copy or xsl:copy-of.
See http://metalab.unc.edu/xml/books/bible/updates/14.html
Search for xsl:copy

In XSL, an xsl:for-each statement sets a context based on the select


attribute of that statement. Once you're within that context, how can you
access elements higher up the tree? For example, given the following XML:

<document attrib="foo">>
<body>
<item>1</item>
<item>2</item>
<item>2</item>
<body>
</document>
If I do
<xml:for-each select="body/item">
how can I access
document/@attribute
since
<xml:value-of select="document/@attribute"/>
would now be equivalent to body/item/document/@attribute?
Location: http://www.jguru.com/faq/view.jsp?EID=103902
Created: Jul 17, 2000 Modified: 2000-07-19 12:07:48.263
Author: Peter Ciuffetti (http://www.jguru.com/guru/viewbio.jsp?EID=103867)
Question originally posed by Dan Bradley
(http://www.jguru.com/guru/viewbio.jsp?EID=50679
All you need to do is start the pattern with slash. E.g.
<result>
<xsl:for-each select="body/item">
<item>
<xsl:attribute name="attrib">
<xsl:value-of select="/document/@attrib"/>
</xsl:attribute>
<xsl:value-of select="."/>
</item>
</xsl:for-each>
</result>
Will produce
<result>
<item attrib="foo">1</item>
<item attrib="foo">2</item>
<item attrib="foo">3</item>
</result>
Comments and alternative answers

level-up
Author: Damic Orion (http://www.jguru.com/guru/viewbio.jsp?EID=747316), Feb 5,
2002
<xsl:value-of select="../@attrib"/> works too! If you wanna go up more than one
level, repeat '../' as much as the number of levels you want to go up...
How does one use XSLT to output the name of an element or an attribute?
For example, given input:

<a b="c"/>
where a, b can be anything, how would you use XSLT to produce:
<b a="c"/>
Basically, I'm looking for XSLT fragment like the following:
<xsl:template match="*">
<xsl:element name="nameof(attribute[1])">
<xsl:attribute name="nameof(.)">
<xsl:value-of select="@attribute[1]"/>
</xsl:attribute>
</xsl:element>
</xsl:template>

Location: http://www.jguru.com/faq/view.jsp?EID=103920
Created: Jul 17, 2000 Modified: 2000-07-19 11:16:11.532
Author: Peter Ciuffetti (http://www.jguru.com/guru/viewbio.jsp?EID=103867)
Question originally posed by Software Framework
(http://www.jguru.com/guru/viewbio.jsp?EID=61156
What you need is something like this:
<xsl:template match="root">
<result>
<xsl:for-each select="*">
<xsl:element name="{local-name(@*)}">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="@*"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</result>
</xsl:template>
This uses the local-name() function of XPATH to obtain the name of the element and
the name of the first attribute in that element. It will transform:
<root>
<a b="c"/>
<d e="f"/>
<g h="i"/>
</root>
Into...
<result>

<e d="f"></e>
<h g="i"></h>
</result>
Note: When you pass local-name a node set (like @*), it returns only the name of
the first node in document order. So you can't use this trick to obtain the name of a
second or subsequent attribute.

How can I store XML data into a database?


Location: http://www.jguru.com/faq/view.jsp?EID=105808
Created: Jul 19, 2000 Modified: 2000-07-19 11:46:47.692
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12) Question
originally posed by srinivas komma
(http://www.jguru.com/guru/viewbio.jsp?EID=11177

Lots of jGuru members contributed to the answer.

Mark Penningroth writes:

There are a number of ways to do this, but it depends on what you need. <bl>

Store whole document in a blob.


If you know the structure of the doc (DTD etc.), you could define a schema
based on the info and then use DOM or SAX to extract the data and insert/update
the database. (There are tools that help. See Ceriumworks)
You could look at OO databse extensions. eXcelon and Poet are a couple that
have done a lot of work here </bl>

Pavel Tavoda writes:

Some database support storing and retrieving data natively. Now Oracle and DB2
support XML as native format and also as query language (XQL). Support for other
database is on way. For storing specific XML datas is best way parse XML document
with some DOM parser (e.g. IBM, SUN, Oracle, ...) to memory and store it to
database how you like and back. Because if you are storing data you must deal with
some problems e.g. flat storing, recursion, structure parsing, datatype recognision,
formating which are not handled always right way in standard products.

Thijs Stalenhoe writes:

There are several possible answers to this questions: <bl>

You could use a standard relational database, but then it will be quite tricky to
maintain the hierarchical nature of XML documents.
You can use an Object Oriented database such as Ozone (which is written in
Java) to store the XML. This will preserve the hierarchy.
There are specialized databases for XML storage available. One of them is
www.dbxml.org (not yet released) but there are many others. www.xml.com has
several listed.
Loads of companies have jumped on the XML bandwagon, each with their own
unique ideas, features and quirks. My advice would be to look well at all that is being
offered, evaluate some and then pick what's best for the project you are working on.
</bl> Later Thijs adds:

This is a tricky question. You could of course use a simple SQL database and have a
table which takes just two entries, for instance:

"id" and "xml-doc"

You would then get it back by just doing a simple SQL query on "id" and returning
"xml-doc". This however does not give you much flexibility. You can't really have the
database return certain tags and/or parts of a given XML-document without first
grabbing the whole thing. What if I wanted to get every <PERSON> tag and its
contents out of each document? The amount of SQL queries would be absurd.

A second way of storing XML in a SQL-db is to try and preserve the hierarchical
structure of the XML by using multiple tables linked together by keys.

This works fine for preserving a single type of XML document. It becomes
cumbersome however once you start using loads of different types of documents, as
for every type of document you would need to create an (almost) entirely new set of
linked tables.

Of course you could go out and buy an XML store like eXcelon (which isn't really a
db) or Tamino (which is quite expensive) but there is another way:
Store your XML in an LDAP database. LDAP uses an hierarchical model for storing
information, which maps nicely to and from XML. It may seems weird at first, but it
works and the implementation of such a scheme is pretty straightforward. Once done
you can easily implement something like an XPath processor (which btw, maps nicely
to LDAP queries) to select various parts of your XML document.

Also see, http://www.jguru.com/jguru/faq/view.jsp?EID=64722

jguru m writes:

Oracle iFS is will allow you to store xml documents in its file system and it will map
the xml data to tables so that you can do faster searching and indexing.

Comments and alternative answers

Another good tool comes from Ronald Bourret called...


Author: Thomas Auinger (http://www.jguru.com/guru/viewbio.jsp?EID=94950), Sep
25, 2000
Another good tool comes from Ronald Bourret called XML-DBMS.

However, consider that this tool (and probably nearly every other tool) uses DOM to
parse the whole XML document. When importing large XML files, this can lead to
serious memory usage.

Therefor it's more efficient to use SAX parsing in a self-written tool (I couldn't find
an existing one) and writing the data using JDBC to the DB. The downside of SAX
are problems with hierarchical data and the mapping to many different tables. Use
SAX if your XML data is very flat and could be imported into one or two tables.

Another idea would be to transform the XML file using XSL to generate a SQL
import file, which you feed into the DB.

Use BLOB or CLOB to store xml


Author: useng yap (http://www.jguru.com/guru/viewbio.jsp?EID=419669), Jun 7,
2001
U can create a BLOB or CLOB column and insert the whole xml to the column.
May be not a good idea, but is a more direct way to do it. The tricky part is how to
maintain is struct. Try to work it up. II hope this will help u.

Where can I get information about XHMTL? What are some links to sites
which have been developed using xhtml?
Location: http://www.jguru.com/faq/view.jsp?EID=109030
Created: Jul 23, 2000 Modified: 2000-07-30 07:58:15.1
Author: Christoph Krüger (http://www.jguru.com/guru/viewbio.jsp?EID=30208)
Question originally posed by karthik krish
(http://www.jguru.com/guru/viewbio.jsp?EID=104317

Right now XHTML 1.0 is identical to HTML 4.01. This is done for compatibility
reasons. Beginning with the next release of XHTML there will be differences.
Information about those "standards" can be found on the W3C website.

Is there an XML parser (SAX/DOM) in C/C++ for VAX/VMS? Links for both
open source and commercial packages are welcome.
Location: http://www.jguru.com/faq/view.jsp?EID=109450
Created: Jul 24, 2000 Modified: 2000-07-30 07:55:33.39
Author: Dror Harari (http://www.jguru.com/guru/viewbio.jsp?EID=109440) Question
originally posed by Benoit Xhenseval
(http://www.jguru.com/guru/viewbio.jsp?EID=3363

See the expat XML 1.0 parser written in C. It ports easily to VAX/VMS although you
will need to use DECC rather than VAXC as the VAXC compiler does not support the
ANSI token pasting operator #.
expat can be found at this
site.

How can I use JUnit to ensure that my servlets/JSP are producing valid
HTML?
Location: http://www.jguru.com/faq/view.jsp?EID=110644
Created: Jul 25, 2000 Modified: 2000-07-26 15:34:48.697
Author: Tom Copeland (http://www.jguru.com/guru/viewbio.jsp?EID=1396)

There's the traditional, brute force way - write a JUnit test case that opens a
HttpURLConnection to your servlet, reads the content, and does various
String.indexOf() and String.subString() operations until you're satisfied that all is
well (or you're tired of hacking together String operations).

A slightly more elegant method is to use an XML parser. You can open the
connection, read the contents, feed it into your XML parser, get back a document,
and walk the DOM tree checking for elements which should be there. Better, but still
clunky.

A better way is to use HttpUnit. HttpUnit allows the test case to be written using the
same "words" as used in web pages - forms, tables, etc. You can write test cases like
this (this is from the example code):
public void testWelcomePage() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(
"http://www.meterware.com/servlet/TopSecret" );

WebResponse response = conversation.getResponse( request );


WebForm forms[] = response.getForms();
assertEquals( 1, forms.length );
assertEquals( 1, forms[0].getParameterNames().length );
assertEquals( "name", forms[0].getParameterNames()[0] );
}
With a little training, test cases of this sort can be written by an HTML developer and
run against the user interface on a regular basis.
Comments and alternative answers

XPath
Author: Johannes Brodwall (http://www.jguru.com/guru/viewbio.jsp?EID=87292),
May 4, 2001

HttpUnit is indeed powerful. An easy way of checking the structure without mocking
too much around with text strings is to combine it with XPath.

Using Xalan from Apache you can do this:

import org.apache.xpath.XPathAPI;
import org.w3.dom.*;

Node responseRoot = response.getDOM().getDocumentElement();


NodeList tableRows = XPathAPI.selectNodeList(responseRoot,
"//table[@id='mytable']/tr");
assertEquals(10, tableRows.getLength());

This will test if the table in the response with the id attribute 'mytable' contains 10
rows.

Is there any browser which supports XLink/Xpointer? Or are there any


examples which demonstrate the use of XLink/Xpointer.
Location: http://www.jguru.com/faq/view.jsp?EID=117482
Created: Aug 2, 2000 Modified: 2000-08-15 09:46:15.051
Author: Dror Harari (http://www.jguru.com/guru/viewbio.jsp?EID=109440) Question
originally posed by shivkumar nayak
(http://www.jguru.com/guru/viewbio.jsp?EID=104496

Microsoft Internet Explorer 5.5 fully supports XPath. If you want to try it out, you
should download the July 2000 Microsoft XML SDK Beta See this Microsoft site. It has
all the documentation you need to get started. You can try it with C++, VB and
various scripting languages.
If you are the Java type, you might want to look up the Apache Xalan project which
includes, among other things, support for XPath.

Lastly, at the Oasis site, you can find almost anything you want to know about XPath,
XLink, XPointer and other related technologies - including links to software
implementations.

How do I read XML data from String variable in Java?


Location: http://www.jguru.com/faq/view.jsp?EID=117823
Created: Aug 3, 2000 Modified: 2000-08-16 08:16:39.485
Author: Peter Gelderbloem (http://www.jguru.com/guru/viewbio.jsp?EID=91670)
Question originally posed by Pons masigla
(http://www.jguru.com/guru/viewbio.jsp?EID=99079

Most parsers takes an InputStream/Reader so the best way is to construct a


StringReader passing the constructor the string containing the XML. You can then
pass this StringReader to the parser.
Comments and alternative answers

How do I read XML data from String variable in Java?


Author: Prakash Rao Tirumalareddy
(http://www.jguru.com/guru/viewbio.jsp?EID=871705), May 8, 2002
Please give me the souce code...i need very urgent.. tahnks prakash

Re: How do I read XML data from String variable in Java?


Author: Praveen Kumar (http://www.jguru.com/guru/viewbio.jsp?EID=1203598),
Oct 5, 2004

Please give me the source code to read XML data from String variable in Java and
also I need the code to insert the extracted XML data into Oracle database using
java

Re: How do I read XML data from String variable in Java?


Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432),
Mar 29, 2005
Easy! No matter you using DOM (javax.xml.parsers.DocumentBuilder) or SAX
(org.xml.sax.XMLReader), or vendor specific SAXParser/DOMParser, you all can
use the same signature method parse(InputSource input) to parse you XML. Now
the question becomes how to use String to make an InputSource.
Contruct a StringReader by using your XML String.
Contruct an InputSource by using your StringReader
Parse the InputSource by using parse(InputSource input)

How do I save a DOM as a local file using XML4j by IBM?


Location: http://www.jguru.com/faq/view.jsp?EID=121168
Created: Aug 7, 2000 Modified: 2000-08-21 10:16:03.181
Author: Robert Sullivan (http://www.jguru.com/guru/viewbio.jsp?EID=102302)
Question originally posed by Stefano Mastella
(http://www.jguru.com/guru/viewbio.jsp?EID=46606

You might want to modify one of the XML4j samples to write to a file. This assumes
you're using plain server-side code, no bean or servlet issues addressed here.

The XML4J sample dom/DomWriter will write XML to standard output. It comes with
the source package (XML4J-src_3_0_1.zip). Here's what I did:

1. I added the necessary import.


2. I added a constructor that took a filename parameter.
3. I called the new constructor.
4. I also set 'canonical' to false.

---cut import ---


import java.io.PrintWriter;
--- cut ----

-- cut constructor---
public DOMWriter(String filename, String encoding, boolean canonical)
throws Exception {
out = new PrintWriter(new
OutputStreamWriter(new FileOutputStream(filename), encoding), true);
this.canonical = canonical;
}
---cut---

Comments and alternative answers

Is there any other method to save the DOM? If Domwriter...


Author: satheesha shamanna (http://www.jguru.com/guru/viewbio.jsp?EID=335732),
Feb 26, 2001
Is there any other method to save the DOM? If Domwriter is used, comments written
in existing XML file will be lost.

I have 10 different DTD's and one specific part of the DTD repeats in all of
the DTD's. I want to store this repeating DTD as another separate DTD and
use it as a reference in all the other DTD's.
For example, I have an Element with UserId and Password which is common
for all the DTD's.
How can I store the above as a DTD file and use its reference in other DTDs?

Location: http://www.jguru.com/faq/view.jsp?EID=126100
Created: Aug 14, 2000 Modified: 2000-08-17 10:24:42.734
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by Sonu TheKool
(http://www.jguru.com/guru/viewbio.jsp?EID=3468

Yes, this is possible using External Parameter Entities. Here's the sample logon.dtd
<!ELEMENT USERID (#PCDATA)>
<!ELEMENT PASSWORD (#PCDATA)>

Here's another DTD which references the logon.dtd

<!ELEMENT NAME (#PCDATA)>


<!ELEMENT AGE (#PCDATA)>
<!ENTITY % logon SYSTEM "logon.dtd">
%logon;
Here's a URL which explains this mechanism.
http://www.irt.org/articles/js212/#dtd_xhtml_mathml
Comments and alternative answers

Is it allowed to have two elements at the same level...


Author: Aswini Vallampati (http://www.jguru.com/guru/viewbio.jsp?EID=244427),
Feb 14, 2001
Is it allowed to have two elements at the same level with the same name?
1. Person(Name,Address,Name)
2. Person(Name?,Address?,Name*)
I have encountered item #2 above in an OAG spec and being a novice, I wondered if
it was a legal DTD structure.

How can XML data be transferred from one machine to another?


Location: http://www.jguru.com/faq/view.jsp?EID=126110
Created: Aug 14, 2000 Modified: 2000-08-21 10:17:23.037
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by mohammad nauman
(http://www.jguru.com/guru/viewbio.jsp?EID=89895

XML is just an open way to express your data, this means that XML can be
transported with just about any protocol. For an example on using XML over HTTP
can be found at http://developer.java.sun.com/developer/qow/archive/84/index.html

I am currently using the DOM, but I want to use an external DTD with my
XML file. With SAX you can parse the external DTD and associate it wih the
XML file. How do I do that with the DOM?
Location: http://www.jguru.com/faq/view.jsp?EID=126121
Created: Aug 14, 2000 Modified: 2000-08-21 09:52:22.524
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by Nakaeda Benskin
(http://www.jguru.com/guru/viewbio.jsp?EID=46721

If you are using JAXP from Sun there are two things you can do.

First, use the com.sun.xml.tree.XmlDocument's setDoctype method as shown below.

XmlDocument doc = new XmlDocument ();


...
// add a DTD, for fun
doc.setDoctype (
null, // no public identifier
"http://www.example.com/xml/dtd1.dtd",
null // no internal subset
);
...
There is a full example (main.java) at
http://developer.java.sun.com/developer/products/xml/examples/#building

Second, you can use setEntityResolver method of


javax.xml.parsers.DocumentBuilder to customize how the external DTD's are
resolved. Here's an example from the docs.

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

public class MyResolver implements EntityResolver {


public InputSource resolveEntity (String publicId, String systemId)
{
if (systemId.equals("http://www.myhost.com/today")) {
// return a special input source
MyReader reader = new MyReader();
return new InputSource(reader);
} else {
// use the default behaviour
return null;
}
}
}
Comments and alternative answers

what about relative URI's


Author: John Hankermeyer (http://www.jguru.com/guru/viewbio.jsp?EID=1056665),
Mar 4, 2003
I see problems with both of these approaches. First, the method "setDocType" is not
part of the JAXP API and thus somewhat dangerous to use.

Second, using the EntityResolver only seems to work with fully qualified URI's -
"http://www.domain.com/my.dtd". I can't seem to get it to work with a relative URI -
"my.dtd". Any idea's how to resolve a relative URI using DOM?

thanks, John

Re: what about relative URI's


Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432),
Mar 4, 2003
If you use EntityResolver, your relative uri would not work, since the system
translate it to an absolute url which looks like "file:..../my.dtd" .... are the path to
my.dtd. However, you could use the trick such as systemId.indexOf("my.dtd") !=
-1 to resolve the entity.

Hope this helps!


Roseanne

Why is EntityResolver not called?


Author: Frank Leony
(http://www.jguru.com/guru/viewbio.jsp?EID=1065500), Mar 12, 2003
This trick only seems to work when EntityResolver is called.

I am having the same problem as described above. I have an incoming xml


document with a relative URI ("my.dtd"). I set an EntityResolver that is
supposed to resolve my DTD to a local file:

private class SSOResolver implements EntityResolver {

public InputSource resolveEntity (String publicId, String


systemId) {

ServletContext context = getServletContext();

InputStream requestDtd =
context.getResourceAsStream("my.dtd");

InputSource retVal = new InputSource(requestDtd);

return retVal;

The problem is that EntityResolver.resolveEntity() is never called! I get the


same exception: "Relative URI "my.dtd"; can not be resolved without a
document URI.". The EntityResolver is only called when you pass in a fully
qualified URI to begin with? What good is the EntityResolver if you have to
pass in a valid DTD URI to begin with? I have seen many posts on this subject
but noone has given a good answer for resolving the dtd URI with a DOM
parser using JAXP? Any help would be truly appreciated? thank you,
-Frank

Re: what about relative URI's


Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432),
Mar 5, 2003
If you're going to resolve it anyway, you also can use some fake URI like
http://www.myco.com/fake, then use the exact string in your resolveEntity()
method.

What is the difference between URI's and URL's?


Location: http://www.jguru.com/faq/view.jsp?EID=126205
Created: Aug 14, 2000 Modified: 2000-08-14 09:27:52.552
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)

Here is a snippet from the XML Bible written by Elliotte Rusty Harold.

"URI stands for Uniform Resource Identifier. URIs are similar to URLs but allow for
more precise specification of a linked resource. In theory, URIs separate the resource
from the location so a web browser can select the nearest or least congested of
several mirrors without requiring an explicit link to that mirror.

Comments and alternative answers

Can someone give a concrete example of how to specify...


Author: Dan Merkel (http://www.jguru.com/guru/viewbio.jsp?EID=28255), Aug 15,
2000
Can someone give a concrete example of how to specify a working URI? I understand
the theory behind it, but is this actually implemented anywhere? How does the
browser resolve the URI?

How can I map a DOM tree to javax.swing.tree.TreeNode or some object so


that I can view an XML document as JTree?
Location: http://www.jguru.com/faq/view.jsp?EID=127115
Created: Aug 15, 2000 Modified: 2000-08-17 10:31:06.87
Author: Seok-jeong Hwang (http://www.jguru.com/guru/viewbio.jsp?EID=30431)
Question originally posed by Chandra Patni PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=33585

You can transform an XML document to a JTree because XML has a tree structure.
You can get information from Sun's XML tutorial

http://java.sun.com/xml/docs/tutorial/dom/4_tree.html

This page is one of the tutorials about manipulating DOM trees.


Comments and alternative answers
Broken link
Author: Neil Kolban (http://www.jguru.com/guru/viewbio.jsp?EID=89126), Aug 9,
2001
This link appears to be broken

Re: Broken link


Author: frederi lafran (http://www.jguru.com/guru/viewbio.jsp?EID=816075),
Mar 28, 2002
the correct link is :

http://java.sun.com/xml/docs/tutorial/dom/3_tree.html

How is international text used in an XML file?


Location: http://www.jguru.com/faq/view.jsp?EID=128650
Created: Aug 16, 2000 Modified: 2000-08-17 10:35:30.968
Author: Richard Raszka (http://www.jguru.com/guru/viewbio.jsp?EID=7963)
Question originally posed by Steve Crombie
(http://www.jguru.com/guru/viewbio.jsp?EID=1579

International text is used in the file by firstly specifying the encoding of the XML file
that you have specified. This is done setting the <?xml encoding='Shift_JIS'?> at
the beginning of the XML file (for example), which would specify the Microsoft Kanji
font to use for display. The names for these text styles are defined in the Unicode
standard and on the site http://www.isi.edu/in-notes/iana/assignments/character-
sets.

Using these character sets requires the user either to type in the equivalent ASCII
code from the keyboard or via the XML charcater specifier (#) For example:
Ampersand would be #38 (in Decimal) or #x26 (in Hex). If using extended character
sets such as the Japanese and Chinese characters two bytes would be used to
represent the letter/character required to be displayed.

Standard tags such as separators such as < and > and tags such as <?xml ?> would
be typed in as normally though content and tag names could be in the nominated
character set used.

I have to create a HTML page using XML and XSL that will have a button on
it that will open a url. How can I put the url address in an html link? The url
address is in xml tag.
Location: http://www.jguru.com/faq/view.jsp?EID=129654
Created: Aug 17, 2000 Modified: 2000-08-21 09:54:59.744
Author: Seok-jeong Hwang (http://www.jguru.com/guru/viewbio.jsp?EID=30431)
Question originally posed by vineet kumar
(http://www.jguru.com/guru/viewbio.jsp?EID=97463

Let's suppose a document segment like below.


<person>
<name>Blabla</name>
<home>blabla.com</home>
</person>
Then following XSL segment will make above segment an HTML link.

<xsl:template match="person"> <a><xsl:attribute name="href">


<xsl:value-of select="home"/> </xsl:attribute> <xsl:value-of
select="name"/></a> </xsl:template>

Then following will be displayed.

Blabla

This page will provide you more information.


http://metalab.unc.edu/xml/books/bible/updates/14.html

How can I check with the DOM-API, if a tag is empty (<mytag/>) or not
(<mytag>data</mytag>)?
Location: http://www.jguru.com/faq/view.jsp?EID=129842
Created: Aug 18, 2000 Modified: 2000-08-21 09:48:47.492
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by Stephan Blasko
(http://www.jguru.com/guru/viewbio.jsp?EID=81535

Element element = ....;


String nodeValue = element.getNodeValue();
if(nodeValue==null||nodeValue.length()==0)
System.out.println("Element is empty");
else
System.out.println("Element value:" + nodeValue);

There are many implementations of SAX and DOM that allow Java
Developers to manipulate XML documents. But are there any other
alternatives?
Location: http://www.jguru.com/faq/view.jsp?EID=132056
Created: Aug 22, 2000 Modified: 2000-08-28 12:40:25.033
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)

You can use JDOM from http://www.jdom.org. The API is intuitive, Java-centric (e.g.
it uses Java Collections) and is optimized for Java.
Comments and alternative answers

I've checked JDOM release 1.0 beta 5 (which I believe...


Author: Jin Voon (http://www.jguru.com/guru/viewbio.jsp?EID=203572), Dec 4,
2000
I've checked JDOM release 1.0 beta 5 (which I believe is the most current version),
but has anyone used it in "real production" environment yet? I'm curious on how
stable this is and if it's reasonably stable, I would love to use it for my development.

When is it appropriate to use SAX and when is it appropriate to use the


DOM?
Location: http://www.jguru.com/faq/view.jsp?EID=133105
Created: Aug 23, 2000 Modified: 2000-08-28 12:50:51.873
Author: Witt Sparks (http://www.jguru.com/guru/viewbio.jsp?EID=2024) Question
originally posed by jayaprakash ammu
(http://www.jguru.com/guru/viewbio.jsp?EID=99824

The DOM is simpler to implement, but because it reads the entire XML document into
a tree structure in memory, it is not appropriate for extremely large documents or on
memory-constrained devices such as PDAs and cell phones.

SAX uses an event driven model to notify the application of document elements as
it's reading the document. This makes it more suitable if memory is a concern, or for
large documents, because any content your application doesn't care about can be
immediately discarded without consuming memory. Because it is event driven, it can
be more difficult to set up the appropriate callback routines, etc. than to use the
DOM.

Comments and alternative answers

Use SAX when your processing of XML has to be FAST...


Author: Christoph Krüger (http://www.jguru.com/guru/viewbio.jsp?EID=30208), Aug
29, 2000
Use SAX when your processing of XML has to be FAST on dedicated parts of the
XML document. Wwith a SAX-parser you don't have to wait for the DOM to be built
before you can access the data. You just have to wait until the parser reaches the part
of the document which is important for you. Possible examples are: processing of an
XML-stream (stock market data) or building indices.

What is Dynamic XML (DXML)?


Location: http://www.jguru.com/faq/view.jsp?EID=134209
Created: Aug 24, 2000 Modified: 2000-08-28 12:58:32.911
Author: John Mattos (http://www.jguru.com/guru/viewbio.jsp?EID=127056)
Question originally posed by Bansidhar Giri
(http://www.jguru.com/guru/viewbio.jsp?EID=115810

See the following link. Lisa Rein explains it quite well.

What is the difference between a validating XML processor and a non-


validating one?
Location: http://www.jguru.com/faq/view.jsp?EID=135504
Created: Aug 26, 2000 Modified: 2000-08-28 12:47:54.664
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

A validating processor checks the structure of a document against the rules specified
in a DTD. A non-validating processor only checks to make sure that the document
conforms to the rules of XML.

What does SAX stand for?


Location: http://www.jguru.com/faq/view.jsp?EID=135552
Created: Aug 26, 2000 Modified: 2000-08-28 12:49:22.043
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)
The expansion of the SAX acronym is the Simplified API for XML. It provides a
standard interface for event-based XML parsing. It differs from the DOM, or
Document Object Model, which is a tree-based model of parsing.

Trying to build Tomcat, I get a message "No JAXP compliant XML parser
found". Where do I get this parser from and where do I put it?
Location: http://www.jguru.com/faq/view.jsp?EID=135925
Created: Aug 27, 2000 Modified: 2000-08-30 13:15:16.252
Author: Thomas Nagel (http://www.jguru.com/guru/viewbio.jsp?EID=68546)
Question originally posed by gaurav sharma
(http://www.jguru.com/guru/viewbio.jsp?EID=119338

[JAXP stands for "JavaTM API for XML Parsing" -A] you could get one from
http://www.javasoft.com/xml/download.html .

Unpack it somewhere on your system and make sure the parser.jar and the jaxp.jar
are included inside your CLASSPATH enviroment variable. [It should also go in
TOMCAT_HOME/lib/ I think that it's now included in Tomcat 3.2 distribution in that
directory. -Alex]

Then try to rebuild again. It should work now :-)

bye
Thomas

Why is it that the DOM interfaces like Document,Element, Node do not


extend the Java Serializable interface?
Location: http://www.jguru.com/faq/view.jsp?EID=137392
Created: Aug 29, 2000 Modified: 2000-08-29 08:15:59.224
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by Mohan Thomas
(http://www.jguru.com/guru/viewbio.jsp?EID=19527

The DOM interfaces are actually specified in OMG IDL, so that the DOM can be
available in multiple programming languages. Implementations of the DOM in Java
can also implement the Serializable Java interface but they are not part of the DOM
standard.

An alternative is to use JDOM. It is completely Java-centric. Some of the other


features are:

• Implements Serializable, Clonable interfaces for Document, Element etc.


• Uses Java2 Collections API
• Interoperates with DOM and SAX API's

And there is an excellent article in JavaWorld to get you started


http://www.javaworld.com/javaworld/jw-05-2000/jw-0518-jdom.html

Why would I want to use JSP when I could use XML/XSL and a parser
instead to achieve system independent results?
Location: http://www.jguru.com/faq/view.jsp?EID=137933
Created: Aug 29, 2000 Modified: 2000-08-30 06:52:50.87
Author: Software Framework (http://www.jguru.com/guru/viewbio.jsp?EID=61156)
Question originally posed by Dave Richardson
(http://www.jguru.com/guru/viewbio.jsp?EID=94918

Use JSP if:


You have Java objects you want to translate to an XML/HTML document and if you
don't need the structure of the document to change dynamically. JSP is faster
because parsing XML documents can be expensive (cpu and memory).

Use XSL if:


You have data already in XML/HTML format and you don't have to change its content
dynamically. You use XSL to allow major changes to the structure of the document;
ie you have to move items around, remove items, and add items. You use XSL if the
rules for translations are very dynamic and need to be very flexible. You use XSL if
the transactions that require it are relatively rare. My tests using xerces and xalan
show that they are 10X slower than using a JSP. (This is mostly because parsing XML
is relative slow and produces lots of short lived objects.)

I use JSPs to generate the content in a form that I think will be acceptable. I allow
xslt translations to handle the cases I didn't think of in advance. It allows the users
to make the final decission. The translations created by users are private and I don't
write or maintain them. (I do allow users to share their translations with each other).

How can I access XML data from a JSP file? I have many documents in XML.
I need the data to be included in HTML documents using JSP.
Location: http://www.jguru.com/faq/view.jsp?EID=138329
Created: Aug 30, 2000 Modified: 2000-09-01 09:03:33.41
Author: Peter Gelderbloem (http://www.jguru.com/guru/viewbio.jsp?EID=91670)
Question originally posed by Juan Zamakona
(http://www.jguru.com/guru/viewbio.jsp?EID=137327

You could use an XSL stylesheet that translates from your XML format to HTML. The
transformation can be done using something like xalan.

Alternatively, you could parse the XML files using the DOM or SAX APIs and extract
the relevant data.

Is there any way to pass parameters to XSL from outside the XSL, such as
from a JSP?
Location: http://www.jguru.com/faq/view.jsp?EID=138396
Created: Aug 30, 2000 Modified: 2000-09-01 08:58:09.069
Author: Peter Ciuffetti (http://www.jguru.com/guru/viewbio.jsp?EID=103867)
Question originally posed by Aisha Fenton
(http://www.jguru.com/guru/viewbio.jsp?EID=130238

The XSLT spec says that top-level xsl:param's and xsl:variable's can be set by the
processor at run-time, but its up to the processor to provide a mechanism for doing
so. These mechanisms are processor specific.
The Xalan XSLT processor, available at http://xml.apache.org, has a method for
setting top-level parameters using either the command line mode or the Java API it
provides. The following JSP code shows how to access Xalan from within a JSP and
then how to override a top-level parameter at runtime.

So the test would be self-contained, I've stored my XML and XSL inputs as strings.
These would normally be files outside the JSP code accessed via URLs or via file
reads.

In the XSL, you'll notice it has a top-level (that is, it is a direct child of the
xsl:stylesheet element) xsl:param named 'theParam'.

It's default value is a string 'the Default'. The stylesheet has a single match pattern
for the root of the test document and it outputs an HTML heading that echo's the
value of the 'theParam'.

After setting up the inputs to the Xalan processor, and just before doing the
transformation, there's is a call to xp.setStylesheetParameter(..). This sets the value
of the top level parameter 'theParam' to the new value 'the Override'. The processor
places the transformation result in a string and the JSP outputs this string to the
HTML output.

Without the setStylesheetParameter() line, the output would be:

<h1>Our param is: the Default</h1>

With this line, the output from the same stylesheet would be:

<h1>Our param is: the Override</h1>

To run this test, install Xalan and add xalan.jar and xerces.jar to the classpath of
your jsp engine. Then put the jsp file below in the document space of the jsp engine.
Load the JSP on your browser and you should see the heading above.

----- TestXSLT.jsp -----


<html>
<head><title>Test XSLT</title></head>
<body>
<%@ page import="org.apache.xalan.xslt.*"%><%@ page
import="java.io.*"%><%

XSLTProcessor xp = null;
try {
xp = XSLTProcessorFactory.getProcessor();
} catch ( Exception e ) {
%><h1>Unable to obtain processor</h1>
<%=e%><%
}

String inputDoc = new String("<?xml


version=\"1.0\"?><root><test></test></root>");
String styleSheet = new String(
"<xsl:stylesheet
xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:param
name=\"theParam\" select=\"'the default'\"/><xsl:output
method=\"html\"/><xsl:template match=\"/\"><h1>Our param is:
<xsl:value-of
select=\"$theParam\"/></h1></xsl:template></xsl:stylesheet>");
StringWriter resultStr = new StringWriter();

XSLTInputSource xml = new XSLTInputSource(new


StringReader(inputDoc));
XSLTInputSource xsl = new XSLTInputSource(new
StringReader(styleSheet));
XSLTResultTarget rt = new XSLTResultTarget(resultStr);

if ( xp != null && xml != null && xsl != null && rt != null ) {


try {
xp.setStylesheetParam("theParam", "'the Override'");
xp.process(xml, xsl, rt);
} catch ( Exception e ) {
%><h1>Unable to perform transformation</h1>
<%=e%><%
}
}
%>
<%=resultStr.toString()%>
</body>
</html>
Comments and alternative answers

Another solution
Author: Paul Flinton (http://www.jguru.com/guru/viewbio.jsp?EID=423606), Jan 24,
2002
You can also do this:
In the jsp file...
<xsl:style xml='foo.xml' xsl='fooStyle.xsl'>
<xsl:param name='param1' value='<%="theValue">'/>
</xsl:style>

In the xsl file...


<xsl:param name="param1" select="'default'"/>

However, you must pass an object in the value attribute of the param tag.

Are there any XML standards for the hotel industry?


Location: http://www.jguru.com/faq/view.jsp?EID=139302
Created: Aug 31, 2000 Modified: 2000-09-01 09:01:22.684
Author: Arun Bharathan (http://www.jguru.com/guru/viewbio.jsp?EID=106958)
Question originally posed by Mohammed Khan
(http://www.jguru.com/guru/viewbio.jsp?EID=91062

It seems to be not there anywhere. Check out this site for industry based XML
initiatives by particular industry related groups.
I'm trying to output an XmlDocument from the HttpServlet's response
object to another servlet.Code below:

XmlDocument doc = new XmlDocument();


Element e = doc.createElement("Order");
e.appendChild(doc.createElement("Type"));
e.appendChild(doc.createElement("Amount"));
doc.appendChild(e);
doc.write(response.getOutputStream());
But I get an error as below:
java.lang.IllegalStateException: can't mix text and binary input at
com.sun.server.servlet.http.HttpResponse.getOutputStream(HttpResponse.j
ava:465)
Have I missed something very important?
Location: http://www.jguru.com/faq/view.jsp?EID=139387
Created: Aug 31, 2000 Modified: 2000-09-20 16:48:59.568
Author: Gene De Lisa (http://www.jguru.com/guru/viewbio.jsp?EID=38746)
Question originally posed by sharma MR
(http://www.jguru.com/guru/viewbio.jsp?EID=4939
Try setting the content type for the response object.
response.setContentType("text/html");
Here is the second part.
try {
Writer out = new OutputStreamWriter(os);
doc.getDocumentElement().normalize();
doc.write(out);
out.write("\n");
out.flush();
} catch(IOException e) {
System.err.println(e);
}
Comments and alternative answers

I have used "text/xml" in this situation...


Author: Lee Meador (http://www.jguru.com/guru/viewbio.jsp?EID=216382), Sep 26,
2000
I have used "text/xml" in this situation for the content type.

How do I embed eXcel documents or Word documents in XML? I understand


that XML is text format but is there a way to encode the binaries into some
format like how email attach binary image files as a multipart mime
attribute?
Location: http://www.jguru.com/faq/view.jsp?EID=213149
Created: Sep 22, 2000 Modified: 2000-09-28 07:41:43.838
Author: Peter Ciuffetti (http://www.jguru.com/guru/viewbio.jsp?EID=103867)
Question originally posed by wee yap ng
(http://www.jguru.com/guru/viewbio.jsp?EID=35233

The technique I've seen used is to convert the binary file (any type) into Base64.
This converts every three binary bytes into a four-character ascii string. The file
encoded in this fashion is therefore only 25% bigger than the original file. And its
relatively easy to encode and decode.
The ascii characters used by the encoding are all valid XML text node characters so
you don't even need to use CDATA sections. You can simply place the encoded result
inside an element and the file will be well-formed. The encoding is described by RFCs
1421 and RFC 2045.

See http://info.internet.isi.edu/in-notes/rfc/files/rfc2045.txt (section 6.8)

The W3C has a Java tool for encoding and decoding base64 which is part of their
DSig project.

See http://www.w3.org/PICS/refcode/Parser/javadocs/Package-w3c.tools.codec.html
for the API and http://www.w3.org/PICS/refcode/Parser/ for information about
obtaining the tools.

Comments and alternative answers

This entry has been very useful!!!


Author: Luis Vivero (http://www.jguru.com/guru/viewbio.jsp?EID=527042), Oct 22,
2001
Thanks for answering this question, it has been very useful for a developing in a
wireless location application where I need to show a map image that has been pass
through an XML. Thanks a lot and best regards!!!

Are there any mailing lists for general XSL questions?


Location: http://www.jguru.com/faq/view.jsp?EID=215656
Created: Sep 25, 2000 Modified: 2000-09-28 07:36:26.176
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

Mulberry Technologies runs the XSL-list mailing list. Subscription information and
archives are available from their website.

What are some examples of sites which are purely developed in XML and
XSL?
Location: http://www.jguru.com/faq/view.jsp?EID=216194
Created: Sep 26, 2000 Modified: 2000-09-28 07:40:24.808
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by Srinivas Thutika
(http://www.jguru.com/guru/viewbio.jsp?EID=46992

WebSites typically use Publishing Frameworks like the Open-Source framework from
Apache named Cocoon.

http://xml.apache.org/cocoon

A list of sites that use Cocoon can be found here:

http://xml.apache.org/cocoon/livesites.html
I have a document and almost all of the content is in HTML format. It is then
"translated" into an XML-like format by using JCDS. The problem with this
XML-like format is that it contains extraneous tags that hamper the feed
from coming in cleanly and complete. I was wondering if there is any code
or program out there to help me in order to "escape out" these stray tags.
Location: http://www.jguru.com/faq/view.jsp?EID=221520
Created: Oct 3, 2000 Modified: 2000-10-03 09:54:14.178
Author: Software Framework (http://www.jguru.com/guru/viewbio.jsp?EID=61156)
Question originally posed by Javad Shakib
(http://www.jguru.com/guru/viewbio.jsp?EID=112540

If the resulting XML document is well-formed you can use XSLT to "translate" it. See
http://metalab.unc.edu/xml/books/bible/updates/14.html for a relatively quick
explanation of XSLT. You can download a translator at
http://xml.apache.org/xalan/getstarted.html#download.

If the tags are simple enough, you can write a script or program to remove them. For
example:

sed -e 's/<foo>//g' -e 's:</foo>::g' input.xml > output.xml

I am writing an application to synchronize data between Oracle Database on


a server and MS Access on a client machine. Should XML be used in this
situation? If so, then how?
Location: http://www.jguru.com/faq/view.jsp?EID=221985
Created: Oct 3, 2000 Modified: 2000-11-07 12:00:04.939
Author: Surendra Chauhan (http://www.jguru.com/guru/viewbio.jsp?EID=45993)
Question originally posed by gaurav sharma
(http://www.jguru.com/guru/viewbio.jsp?EID=119338

The answer depends upon the structure of the data to be synchronized. Do both the
sides have same data structure? or is there significant difference in the data
structure? For example, different table structure, data types etc.

XML can be used in any data transfer and synchronization. I am synchronizing data
between an SQL server and an Oracle database. The use of XML is more appropriate
if there is significant difference in the database schema and the way the data is
represented between the two sides. Using the XSLT and style sheets it is easy to
process and manipulate the data. But, handling the data through the utility is also a
complex process. While there are good JDBC drivers and libraries available for an
Oracle database, I am not aware if there are good drivers for Access database.
Moreover, the Access database does not support the standard SQL.

The possible steps would be:

Write a data exporter and importer for Oracle side. The applications will connect
to the database using the JDBC drivers, and export/import data in/from XML files.
Oracle XML libraries will be useful here.
Do the transformations using an XSLT processor, such as xalan from Apache.
Import/export data on the Access database side. JDBC/ODBC driver may be
useful. I have no exoperience with XML libraries for Access data, so can not answer
this portion in detail.
If the database schema, or some of the table structures are same, it is better to use
ODBC drivers to connect the client and the server to synchronize the data. In this
case the possible steps would be:

Establish the connection to the Oracle database using the ODBC from the client
side.
Map the tables from oracle database in the Access database using the remote
connection.
Create queries to update/delete data.

What is XSP?
Location: http://www.jguru.com/faq/view.jsp?EID=223224
Created: Oct 5, 2000 Modified: 2000-11-09 11:08:06.645
Author: K V R K Varma (http://www.jguru.com/guru/viewbio.jsp?EID=223223)

XSP(Extensible Server Pages) provides a frontend portion of an XML framework


providing dynamic XML pages that are parsed and transformed by the framework
and allow application interoperability yet are constructed and yet stored as static files
on a file system.

Just as Java is aimed at providing a clear separation of content from application and
business logic, XSP seek to provide the same for XML- based applications. Although
many of the currently available XML frameworks allow this separation of layers within
complied code, changes to the formatting of actual data in an XML document still
requires Java code and subsequent recompilation.

See the Cocoon project for more details.

What is PI ?
Location: http://www.jguru.com/faq/view.jsp?EID=223225
Created: Oct 5, 2000 Modified: 2000-10-10 13:37:59.539
Author: K V R K Varma (http://www.jguru.com/guru/viewbio.jsp?EID=223223)

A PI(Processing Instruction) tells an XML based application to perform some specific


task. A PI is distinguished from other XML data, because it represents a command to
a XML Parser. eg: <? XML Version ="1.0"?>

What is a DTD ?
Location: http://www.jguru.com/faq/view.jsp?EID=223227
Created: Oct 5, 2000 Modified: 2000-10-10 13:40:56.286
Author: K V R K Varma (http://www.jguru.com/guru/viewbio.jsp?EID=223223)

A DTD(Document Type Definition) defines the way an XMLDocument should be


constructed. The DTD added portability to an XML document's extensibility, resulting
not only in flexible data, but data that can be processed and validated by any
machine that can locate the document's DTD.

What is XPath?
Location: http://www.jguru.com/faq/view.jsp?EID=223228
Created: Oct 5, 2000 Modified: 2000-10-10 13:43:55.447
Author: K V R K Varma (http://www.jguru.com/guru/viewbio.jsp?EID=223223)
The XPath(XML Path Language) specification defines how a specific item within an
XML document can be located. It is used extensively with XSLT. This is done by
referencing specific nodes in the XML document. For example:
<JavaXML:Printer>
.
.
<JavaXML:Parts>

</JavaXML:Parts>

<JavaXML:Company> HP </JavaXML:Company>

</JavaXML:Printer>

What is XQL?
Location: http://www.jguru.com/faq/view.jsp?EID=223231
Created: Oct 5, 2000 Modified: 2000-11-08 07:56:39.673
Author: K V R K Varma (http://www.jguru.com/guru/viewbio.jsp?EID=223223)

XQL(Extensible Query Language) is designed to allow XML document formats to


easily represent database queries. The structure of the query is defined using XPath
concepts, and the result set is defined using standard XML with XQL specific tags.
The current proposal for XQL is available here.

What is SAX?
Location: http://www.jguru.com/faq/view.jsp?EID=223233
Created: Oct 5, 2000 Modified: 2000-10-16 08:12:31.607
Author: K V R K Varma (http://www.jguru.com/guru/viewbio.jsp?EID=223223)

SAX, or SimpleAPI for XML, provides a framework for parsing XML data.

SAX define events which applications can be notified of during the parsing process.

SAX defines the org.sax.ContentHandler interface with methods such as


startDocument() and endDocument(). Applications that implement this interface and
are registered with the parser are called back during the parse of the XML document.

For mor information on SAX, visit http://www.megginson.com/SAX

What is JAXP?
Location: http://www.jguru.com/faq/view.jsp?EID=223234
Created: Oct 5, 2000 Modified: 2000-10-10 13:51:17.357
Author: K V R K Varma (http://www.jguru.com/guru/viewbio.jsp?EID=223223)

JAXP(Java API for XML Parsing), does not redefine SAX or DOM but ensures that all
XML-Conformant parsers can be accessed within the Java applications through a
standard pluggable layer.

These three API's (JAXP, SAX and DOM) make up the Java Developers toolkit for
handling XML.
A complete JAXP reference is available here.

How do you relate Java with XML?


Location: http://www.jguru.com/faq/view.jsp?EID=223237
Created: Oct 5, 2000 Modified: 2000-11-07 14:18:32.584
Author: K V R K Varma (http://www.jguru.com/guru/viewbio.jsp?EID=223223)

Java and XML make a good pair because Java provides portable code and XML
provides portable data. Java code can run on any OS and hardware platform with a
Java Virtual Machine. In addition, Java provides the most robust set of API's, parsers,
processors and tools for XML. Both Java and XML have limitations if taken separately.
In Java the developer has to handle network data formats. JavaServer Pages do not
provide a real separation of content and presentation layers. XML tools handle these
tasks for Java.

I want to generate a nice report using XML & XSL. From this schema

<RECORDSET>
<RECORD><CUST>Cust 1</CUST><VALUE>1</VALUE></RECORD>
<RECORD><CUST>Cust 1</CUST><VALUE>2</VALUE></RECORD>
<RECORD><CUST>Cust 2</CUST><VALUE>3</VALUE></RECORD>
<RECORD><CUST>Cust 2</CUST><VALUE>4</VALUE></RECORD>
<RECORD><CUST>Cust 3</CUST><VALUE>5</VALUE></RECORD>
<RECORD><CUST>Cust 3</CUST><VALUE>6</VALUE></RECORD>
</RECORDSET>
I want to generate a report that looks like this

<table>
<tr><td>Cust 1</td><td>1</td></tr>
<tr><td> </td><td>2</td></tr>
<tr><td>Cust 2</td><td>3</td></tr>
<tr><td> </td><td>4</td></tr>
<tr><td>Cust 3</td><td>5</td></tr>
<tr><td> </td><td>6</td></tr>
</table>

How can I access the previous <CUST>?


Location: http://www.jguru.com/faq/view.jsp?EID=223809
Created: Oct 6, 2000 Modified: 2000-11-09 11:12:16.334
Author: Peter Ciuffetti (http://www.jguru.com/guru/viewbio.jsp?EID=103867)
Question originally posed by Alfonso Garcia-Patiño
(http://www.jguru.com/guru/viewbio.jsp?EID=111459

The following stylesheet output rows of customer records in a table where the
customer name is displayed in column 1 in only the first record for a given customer.

It uses the preceding::RECORD axis in the comparison, which gathers all of the
RECORDs before the current RECORD being processed in the for-each loop. The
[last()-1] predicate on the test gets the last one of these.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="RECORDSET">
<table border="1">
<xsl:for-each select="RECORD">
<tr>
<xsl:choose>
<!-- If it's the first record, fill in column 1 -->
<xsl:when test="position()=1">
<td><xsl:value-of select="CUST"/></td>
</xsl:when>
<!-- If the last preceding record has a different
customer, fill in column 1 -->
<xsl:when test="preceding::RECORD[last()-1] and
string(preceding::RECORD[last()-1]/CUST)!=string(CUST)">
<td><xsl:value-of select="CUST"/></td>
</xsl:when>
<!-- Otherwise its the same customer, output an empty
cell in column 1 -->
<xsl:otherwise>
<td></td>
</xsl:otherwise>
</xsl:choose>
<td><xsl:value-of select="VALUE"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>

Given the example test data, the table produced looks like:

Cust 1 1
2
Cust 2 3
4
Cust 3 5
6

What's the origin of CDATA? Does it stand for anything in particular?


Location: http://www.jguru.com/faq/view.jsp?EID=229557
Created: Oct 16, 2000 Modified: 2000-10-20 08:07:17.578
Author: Serge Knystautas (http://www.jguru.com/guru/viewbio.jsp?EID=100012)
Question originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

CDATA is short for character data, and PCDATA is short for parsed character data. It
is important when handling data that contains markup that a parser would other
think as a tag or some other command instead of just data for a particular element.

Does DOM include APIs for diffing and merging XML documents?
Location: http://www.jguru.com/faq/view.jsp?EID=229569
Created: Oct 16, 2000 Modified: 2000-10-23 09:25:52.436
Author: Serge Knystautas (http://www.jguru.com/guru/viewbio.jsp?EID=100012)
Question originally posed by dylan taffe
(http://www.jguru.com/guru/viewbio.jsp?EID=93678

The DOM API does not support diff and merge, although IBM Alphaworks has a Java
tool named xmldiffmerge which you can use for just this purpose.

Note from FAQ Manager: Also see this related FAQ.

In a DTD, how could I specify a set of values that an element (not an


attribute) could contain.
Say the element is #PCDATA but I want it to be one of the following strings:
Hello | Hi.
How could this be done in a "forward-compatible" way with XSD?
Location: http://www.jguru.com/faq/view.jsp?EID=229570
Created: Oct 16, 2000 Modified: 2000-11-09 11:08:51.179
Author: Serge Knystautas (http://www.jguru.com/guru/viewbio.jsp?EID=100012)
Question originally posed by Benoit Xhenseval
(http://www.jguru.com/guru/viewbio.jsp?EID=3363

DTDs do not allow you to specify possible values of data, either for attributes or
elements. It basically only allows you to specify how elements nest and their
attributes.

XML Schema will allow you to do this.

Comments and alternative answers

Attribute values defined in a DTD.


Author: Jeffrey Jennings (http://www.jguru.com/guru/viewbio.jsp?EID=424377),
May 18, 2001
Actually, attribute values can be defined in a DTD:
<!ELEMENT rainbow(#PCDATA)>
<!ATTLIST rainbow color(red |orange | yellow | green| blue | indigo |
violet) #REQUIRED>
This makes only the colors listed in the ATTLIST valid for the attribute value.

Are there any tools to support development of XML-based JSP pages, such
as:

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="community.xsl"?>

<jsp:directive.page import="com.viewlocity.VuCommunity" />


<jsp:useBean id="community" class="com.viewlocity.VuCommunity"
scope="session"/>

<community>
<name-label>Name</name-label>
<name>
<jsp:expression>
community.getName()
</jsp:expression>
</name>
<description-label>Description</description-label>
<description>
<jsp:expression>
community.getDescription()
</jsp:expression>
</description>
<admin-label>Community Administrator</admin-label>
<admin>
<jsp:expression>
community.getAdministrator()
</jsp:expression>
</admin>
</community>

Location: http://www.jguru.com/faq/view.jsp?EID=229571
Created: Oct 16, 2000 Modified: 2000-11-08 08:38:33.556
Author: Serge Knystautas (http://www.jguru.com/guru/viewbio.jsp?EID=100012)
Question originally posed by Mark Lorenz
(http://www.jguru.com/guru/viewbio.jsp?EID=118221
The Cocoon project by the Apache Group is doing something very similar to this.
They call their files XSP instead of JSP as the syntax is different, and they also
leverage some of the flexibility that an XML based publishing system will give you.
Check out the Cocoon web site.

I want to create a Java program that will read an XML file and extract the
information I specify. My question is, what is the best method to read nodes
and extract information out of an XML document whose type is not known
beforehand?
Location: http://www.jguru.com/faq/view.jsp?EID=229580
Created: Oct 16, 2000 Modified: 2000-11-07 13:48:52.92
Author: Serge Knystautas (http://www.jguru.com/guru/viewbio.jsp?EID=100012)
Question originally posed by Rahul Khanna
(http://www.jguru.com/guru/viewbio.jsp?EID=201072

SAX is very simple API to handle extracting information from an XML file. You
register your class to the SAX parser as a listener of "SAX events", which are just
when the parser hits certain different parts of the file. You will get calls when a tag is
started, when a tag stops, and the character data within the tag. In each of these
situations, the event will contain information about what tag was started or stopped
and any attributes of that tag.

Try downloading the XercesJ parser from http://xml.apache.org/ and trying some of
the SAX examples.

What is EDI and how it can be implemented in XML?


Location: http://www.jguru.com/faq/view.jsp?EID=231969
Created: Oct 19, 2000 Modified: 2000-11-07 12:08:41.971
Author: krtek veliky (http://www.jguru.com/guru/viewbio.jsp?EID=230084)
Question originally posed by deepak sharma
(http://www.jguru.com/guru/viewbio.jsp?EID=112835

EDI stands for Electronic Data Interchange. It is form of electronic communication for
interchanging bussiness data (orders, payments etc.) It uses data format that is very
unsimilar to XML so it cannot be implemented in XML.

However, a particular "implementation" can be made as a gateway that tranlates EDI


orders to XML orders, sends it to server and then translates the answer from XML to
EDI and sends it back.

More info about EDI can be found at www.edifact.org

Are there any sites that provide more information or example source code
for Apache's Xerces-J and the DOM?
Location: http://www.jguru.com/faq/view.jsp?EID=231984
Created: Oct 19, 2000 Modified: 2000-10-20 08:08:35.967
Author: krtek veliky (http://www.jguru.com/guru/viewbio.jsp?EID=230084)
Question originally posed by Radhakrishnan R.A.V.
(http://www.jguru.com/guru/viewbio.jsp?EID=10157

Some examples come with Xerces. You can read about them at
xml.apache.org/xerces-j/samples.html

How do I write a DOM document as an XML file?


Location: http://www.jguru.com/faq/view.jsp?EID=233252
Created: Oct 21, 2000 Modified: 2000-10-23 09:14:41.233
Author: Pramod Hirole (http://www.jguru.com/guru/viewbio.jsp?EID=41443)
Question originally posed by Ram Saani
(http://www.jguru.com/guru/viewbio.jsp?EID=211948

Use xerces parser from apache. You should use the XMLSerializer class to serialise
dom to xml file. Here is code sniplet.
// import following package
import org.apache.xml.serialize.*;

// following code serializes


// dom to xml file

OutputFormat of =
new OutputFormat("XML","UTF-8",true);
XMLSerializer serializer =
new XMLSerializer();
serializer.setOutputFormat(of);
serializer.setOutputstream(new
FileOutputstream("xmlfile.xml"));
serializer.serialize(dom);
You can get the xerces parser from Apache.
Comments and alternative answers

You can also use the Java project X classes. Down load...
Author: Mafaz Zafar (http://www.jguru.com/guru/viewbio.jsp?EID=234259), Oct 23,
2000
You can also use the Java project X classes. Down load Java Project X Technology
Release 2 from this site. The XmlDocument class has a few write methods. You can
use one of these methods to write DOM to an output stream. You have to import the
appropriate classes in your code.

The following lines of code are from one of the methods we wrote.

private XmlDocument doc;

fileOutputStream = new FileOutputStream(selectedFile);


doc.write(fileOutputStream);

// Closes this file output stream

fileOutputStream.close();

Re: You can also use the Java project X classes. Down load...
Author: Christophe Demez
(http://www.jguru.com/guru/viewbio.jsp?EID=397288), Sep 5, 2001
Yes, but ...

I already have a "Document" instance.

How can I set it on my XmlDocument.

In the example you give, it will write an empty file, because "doc" is empty.

In my case I have a Document instance, how can I do ?

Thanks

Re: Re: You can also use the Java project X classes. Down load...
Author: Mafaz Zafar (http://www.jguru.com/guru/viewbio.jsp?EID=234259),
Sep 7, 2001
It depends how you create the document.

We are creating the XML document from scratch using the


XmlDocumentBuilder class found in (com.sun.xml.tree package). Then we
build the XML document piece by piece before we store it.

We have written a class which has a bunch of methods in it for creating XML
documents. It can load existing XML documents and search for elements and
attributes. It can add elements and attributes to newly created XML document.
It can load existing XML documents. It can save XML documents to files etc
etc. It is making use of Java Project X Technology Release 2

Here is some of the code from our class which we wrote to process XML
documents.

NOTE: It order to compile the following code I have just changed


ExceptionMethod to Plain Exception.

ExceptionMethod is a class which we created you really do not need it.

import java.io.File;

//import com.ccccccccc.exception.ExceptionMethod;

import com.sun.xml.parser.Resolver;

import com.sun.xml.tree.*;

import org.xml.sax.InputSource;

import java.io.*;

import org.xml.sax.SAXException;

import org.xml.sax.SAXParseException;

import org.w3c.dom.*;

import java.util.*;

public class XMLManager{

// VARIABLES

private static final String TEMPLATE_FILE = "template.xml";

private XmlDocument doc;

private InputSource input;

private Element root = null;


private Vector nameValueVector = new Vector();

// Special content CHARACTERs

static final char AMPERSAND_CHARACTER = '&';

static final char LESS_THAN_CHARACTER = '<';

static final char GREATER_THAN_CHARACTER = '>';

static final char APOSTROPHE_CHARACTER = '\'';

static final char QUOTE_CHARACTER = '"';

// static final String AMPERSAND_CHARACTER_XML = "&";

//static final String LESS_THAN_CHARACTER_XML = "<";

//static final String GREATER_THAN_CHARACTER_XML = ">";

// static final String APOSTROPHE_CHARACTER_XML = "&apos;";

//static final String QUOTE_CHARACTER_XML = """;

public XMLManager(){

this.super();

public XMLManager(XmlDocument document) throws Exception {

this.super();

if (root != null) {

this.doc = document;

try {

// turn it into an in-memory object

doc = XmlDocument.createXmlDocument (input, true);


// normalize text representation

root = doc.getDocumentElement();

root.normalize();

catch (SAXParseException err) {

throw new Exception("** Parsing error" + ", line " + err.getLineNumber () + ",
uri " + err.getSystemId () + " " + err.getMessage ());

catch (SAXException e) {

throw new Exception("SAX Exception occurred" + e.getMessage());

catch (Exception exception) {

throw new Exception("Exception occurred: " + exception.toString());

public void createXML() throws IOException, Exception //,ExceptionMethod

Node replacedNode;

try {

loadXML(TEMPLATE_FILE);

}
catch (IOException ioe) {

throw ioe;

//}catch (ExceptionMethod em) {

catch (Exception em) {

throw em;

public void createXML(String rootName) {

//LOCAL VARIABLES

XmlDocumentBuilder xmlDocumentBuilder;

ElementEx elementEx;

DocumentType docType;

// Create document

xmlDocumentBuilder = new XmlDocumentBuilder();

doc = xmlDocumentBuilder.createDocument();

// Create Root

root = doc.createElementEx(rootName);

doc.appendChild(root);

//Method : saveXML(String selectedFile)

//Note: selected file is the full path and name of the file where the XML should
be stored. (examlpe: "c:\\temp\\myXmlfile.xml")

public void saveXML(String selectedFile) throws IOException {

int index = 0;

FileOutputStream fileOutputStream;

//If selectedFile does not have any extension add .xml as extension.

index = selectedFile.lastIndexOf(".");

if (index == -1) {

selectedFile = selectedFile + ".xml";

fileOutputStream = new FileOutputStream(selectedFile);

//Now call the function saveXML(OutputStream outputStream) to save. //Note


fileOutputStream is inherited from outputStream.

this.saveXML(fileOutputStream);

//Method : saveXML(OutputStream outputStream)

public void saveXML(OutputStream outputStream) throws IOException {

doc.write(outputStream);

// Closes this output stream and releases any system resources //associated with
this stream.

outputStream.close();

//NOTE: Loads an existing XML document from the file. (Note:


configurationFile is the complete path to the XML file to be loaded example
"c:\\temp\\myXmlfile.xml"
public void loadXML (String configurationFile) throws IOException,
Exception //,ExceptionMethod

// turn the passed filename into a fileInputStream.

FileInputStream fileInputStream = new FileInputStream(configurationFile);

//Now call the method loadXML(InputStream) to load this file.

this.loadXML((InputStream)fileInputStream);

/* NOTE: The following method can load XML from an input stream. If you
have XML in form of a string it has to be converted to an input stream first*/

/*NOTE: This method validates the XML when it loads it. If you do not want
it to be validated just change the code and pass false as mentioned in the
code*/

public void loadXML (InputStream inputStream) throws Exception


//ExceptionMethod

try {

// turn the inputStream into an input source

input = Resolver.createInputSource("text/xml", inputStream, true, "utf-8");

// turn it into an in-memory object

// ... the "false" flag says not to validate

doc = XmlDocument.createXmlDocument (input, true);

// normalize text representation

root = doc.getDocumentElement();
root.normalize();

catch (SAXParseException err) {

// throw new ExceptionMethod("** Parsing error"

throw new Exception("** Parsing error" + ", line " + err.getLineNumber () + ",
uri " + err.getSystemId () + " " + err.getMessage ());

catch (SAXException e) {

//throw new ExceptionMethod("SAX Exception occurred" + e.getMessage());

throw new Exception("SAX Exception occurred" + e.getMessage());

catch (Exception exception) {

//throw new ExceptionMethod("Exception occurred: " + exception.toString());

throw new Exception("Exception occurred: " + exception.toString());

public NodeList findElementList(String tagName) throws Exception {

return root.getElementsByTagName(tagName);

Once the document is created with the specified root name we can add
elements and node to the newly created document. We have several other
methods in our code to add elements and attributes and nodes to the XML
document. /*Note: If you do not know how to convert a string which
represents your XML to a input stream use the following code as example.*/

byte[] inputByte = null;

String responseXml = "YOUR STRING REPRESENTING XML";

/*NOTE: XMLManager is our class which we wrote which contains all the
above mentioned methods.*/

XMLManager xmlManager = new XMLManager();

//Convert the responseXml string to Byte array to stream.

inputByte = responseXml.getBytes();

//Create output stream

outputStream = new ByteArrayInputStream(inputByte);

//Load xml stream into DOM (Document Object Model)

xmlManager.loadXML((InputStream)outputStream);

Re: Re: Re: You can also use the Java project X classes. Down load...
Author: Christophe Demez
(http://www.jguru.com/guru/viewbio.jsp?EID=397288), Sep 10, 2001
Thanks, Really good ! Do you know that you use some depreceated methods (
like createXmlDocument ). Have you solve this problem ? Thanks Christophe

I am using SUN's SAX validating parsers(parser.jar ,jaxp.jar). I used the


factory to create the parser. Before creating the parser, I first turn
validation on using the setValidating(true) method. Then I create the
parser. When I try to validate a file the following exception is raised:

org.xml.sax.SAXParseException: White space must not occur between


elements declared
in an external parsed entity with element content in a standalone
document.
What does this mean?
Location: http://www.jguru.com/faq/view.jsp?EID=233256
Created: Oct 21, 2000 Modified: 2000-11-07 14:24:13.015
Author: Pramod Hirole (http://www.jguru.com/guru/viewbio.jsp?EID=41443)
Question originally posed by victor martin
(http://www.jguru.com/guru/viewbio.jsp?EID=202294
You must be having white space in an element name. eg.
Element elm = document.createElement("A B");

which will appear as follows:


<A B>
in your XML file or document. XML does not allow white space in an element name. If
you want white space to occure in an element name you should replace it by an
appropriate entity to replace white space character.
Comments and alternative answers

You can set IgnoreWhiteSpaces for the SAXReader class.


Author: Neeraj Goel (http://www.jguru.com/guru/viewbio.jsp?EID=749732), Feb 6,
2002
If you are using reader factory then just set property of ignoreWhiteSpaces to true.

My solution
Author: Andrew Russell (http://www.jguru.com/guru/viewbio.jsp?EID=508552), Feb
22, 2004
My solution to this problem was to delete a spurious space in one of my tags.
i.e. <invoice-file att1="eeerrr" ...
changed to <invoice-file att1="eeerrr" ...
And that was it. Andrew.

I have to build an XML document from pieces of existing ones, and then
output it as a string. What libraries should I use?
Location: http://www.jguru.com/faq/view.jsp?EID=233260
Created: Oct 21, 2000 Modified: 2000-11-04 20:54:29.121
Author: Pramod Hirole (http://www.jguru.com/guru/viewbio.jsp?EID=41443)
Question originally posed by Stéphane Félix
(http://www.jguru.com/guru/viewbio.jsp?EID=210800

If you have pieces of existing valid documents as nodes then you can import those
into the document you want to build by appending those nodes as child elements to
the document.

The following code will do this:

document.importNode(doc1Elm);
document.importNode(doc2Elm);
document.appendchild(doc1Elm);
docElm1.appendChild(docElm2);
To write document object as a string you need to serialize it. You can use the Apache
Xerces parser available from Apache. You should use XMLSerializer class to serialise
document as String. Here is code sniplet.
// import following package
import org.apache.xml.serialize.*;

// following code serializes


// document to domstr String

OutputFormat of =
new OutputFormat("XML","UTF-8",true);
XMLSerializer serializer =
new XMLSerializer();
ByteArrayOutputStream baos =
new ByteArrayOutputstream()
serializer.setOutputFormat(of);
serializer.setOutputstream(baos);
serializer.serialize(document);
String domstr =
new String(baos.toByteArray())

// domstr contains document as a String

I have to build an XML document from pieces of existing ones, and then
output it as a string. What libraries should I use?
Location: http://www.jguru.com/faq/view.jsp?EID=233959
Created: Oct 23, 2000 Modified: 2000-11-07 12:04:25.629
Author: bob mcwhirter (http://www.jguru.com/guru/viewbio.jsp?EID=41649)
Question originally posed by Stéphane Félix
(http://www.jguru.com/guru/viewbio.jsp?EID=210800

To programatically assemble an XML document, I'd scan in each document you wish
to use as sources, with either DOM, or my favorite, JDOM.

Once you have the documents in a DOM/JDOM tree, you can rather easily pull out
the portions you want, and reconstruct a new document containing the desired
subtrees from the source documents.

While DOM is the 'standard', I find JDOM more useful for the Java programmer for
manipulating XML documents as a structure, instead of a stream of bytes.

I have some data and I want to use next and previous buttons to display
each record. I can accomplish this at the client by using
xmldocument.recordset.moveNext() in a JavaScript event. But how can I
accomplish this when I am using XSL?
Location: http://www.jguru.com/faq/view.jsp?EID=235044
Created: Oct 24, 2000 Modified: 2000-11-07 13:44:34.982
Author: Alexandre Cuva (http://www.jguru.com/guru/viewbio.jsp?EID=235041)
Question originally posed by vishnu agrawal
(http://www.jguru.com/guru/viewbio.jsp?EID=228066

You have two options :

1)You have to load the xmlDocument at the onLoad event:

var source=new ActiveXObject("Microsoft.xmldom");


source.async=false;
source.load("../budgetXML2.xml");
and then use the XMLDom method.

2)[DataBiding] Add the following statement at the start of the page right after the
body:
<XML SRC="budgetGet.xml" ID="xmldso"></XML>
and then use like before the xmldso.recordset.moveNext()

The first version gives you a way to navigate complex XML documents.

The second is quite easy for a simple xmlDocument.

You can find some good source code examples at the Wrox site.

I am looking for a tool to query a DTD. I know IBM's XML4J does it. Anyone
heard of another tool?
Location: http://www.jguru.com/faq/view.jsp?EID=241219
Created: Oct 31, 2000 Modified: 2000-11-07 14:36:13.935
Author: Mark Pollack (http://www.jguru.com/guru/viewbio.jsp?EID=231174)
Question originally posed by Gregory Van Ass
(http://www.jguru.com/guru/viewbio.jsp?EID=34607

The XML parser from Oracle lets you access the parsed DTD with the class
oracle.xml.parser.v2.DTD

See http://technet.oracle.com/tech/xml/parser_java2/index.htm for details.

I am trying to build an XML Document with the DOM that can be parsed from
Xalan XSL Processor.

If I creating the document with an XML editor I have no problems parsing it


with Xalan.

But when creating the document with DOM I get following error message :

person.xml; Line 0; Column 0


XSL Error: Could not parse person.xml document!
XSL Error: SAX Exception
org.apache.xalan.xslt.XSLProcessorException: The encoding "Cp1252" is
not supported.
The XML File looks like this:
<?xml version="1.0" encoding="Cp1252"?>

<document>
<title>Simple PDF Example</title>
<author>Holger Prause</author>
<note>The following text was taken from the FO Example File
readme.fo</note>
<content>Some text</content>
</document>
Why is the DOM creating the document with the default Encoding Cp1252?
How can I set the encoding to "UTF-8"?
Location: http://www.jguru.com/faq/view.jsp?EID=243588
Created: Nov 2, 2000 Modified: 2000-11-07 12:10:57.093
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by Holger Prause
(http://www.jguru.com/guru/viewbio.jsp?EID=224969
Use
<?xml version="1.0" encoding="UTF-8" ?>
instead of
<?xml version="1.0" encoding="Cp1252"?>
Comments and alternative answers

details on specify the encoding


Author: brian surratt (http://www.jguru.com/guru/viewbio.jsp?EID=102917), Aug 22,
2001

This comment is specifically for using the Apache crimson implementation of JAXP.

Using the org.apache.crimson.tree.XmlDocument class, there are a number of


different signatures for the write method to dump the XML document to a file.

• public void write (OutputStream out) - has the encoding hard coded to
"UTF8".
• public void write (Writer out) - uses the system value for encoding.
• public void write (Writer out, String encoding) - inserts the value of the
encoding argument directly into the output.

See the article How do I set a default character encoding for file I/O operations, JDBC
requests and so on? for info on how to change your encoding at the JVM level.

I want to parse HTML documents on the web into something understandable


generically. I was thinking of using an XML parser with the HTML DTD from
W3C. Does this sound sensible or am I missing something?
Location: http://www.jguru.com/faq/view.jsp?EID=243595
Created: Nov 2, 2000 Modified: 2000-11-07 14:31:48.551
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by Ben Holland
(http://www.jguru.com/guru/viewbio.jsp?EID=31566

use JTidy, It can convert HTML documents into XHTML/XML:

http://lempinen.net/sami/jtidy/

Is it possible to create a generic XSLT stylesheet to strip all tags from an


XML file such that only #PCDATA text is output and all long lines are nicely
formatted up to 80 characters keeping existing words together?
Location: http://www.jguru.com/faq/view.jsp?EID=246492
Created: Nov 6, 2000 Modified: 2000-11-07 13:46:58.823
Author: Pavel Tavoda (http://www.jguru.com/guru/viewbio.jsp?EID=19085)
Question originally posed by Michael Mitiaguin
(http://www.jguru.com/guru/viewbio.jsp?EID=4719
This is doing a good job for me:
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()">
MATCH: <xsl:value-of select="."/></xsl:template>
If you need formating then I guess it is better to output HTML. If you really need
pure text output then use the JavaScript extension in XSLT and format your text in
JavaScript.

Using both Sun's and Apache's(Xerces) implentations of the SAX XML paser,
it appears that it requires over 7 MEGS of heap space to parse a 150 kilobyte
XML document. I can understand a DOM parser having this kind of footprint,
but why would a Java SAX parser need such an absurd amount of memory?
Location: http://www.jguru.com/faq/view.jsp?EID=248646
Created: Nov 8, 2000 Modified: 2000-11-09 10:54:18.403
Author: Serge Knystautas (http://www.jguru.com/guru/viewbio.jsp?EID=100012)
Question originally posed by J Majik
(http://www.jguru.com/guru/viewbio.jsp?EID=61152

When writing software, you have three areas you have to try to balance... speed,
memory usage, and size of code. These commercial parsers are generally optimized
purely for performance and require a lot of overhead. Also, assuming they make
extensive use of Strings, in Java this creates a lot of object overhead.

There are XML parsers out there that are optimized for a smaller code footprint and
less memory overhead including MinML at http://www.wilson.co.uk/xml/minml.htm
and NanoXML at http://nanoxml.sourceforge.net/.

The xerces parser does not like XML data containing tags e.g. <start> <--
xml data starts--> <html><head><title>Xmldata
</title></head></html> <--xml data ends--> </start> Is there a way to
parse/store that information without confusing the parser?
Location: http://www.jguru.com/faq/view.jsp?EID=248650
Created: Nov 8, 2000 Modified: 2000-11-08 11:20:27.884
Author: Serge Knystautas (http://www.jguru.com/guru/viewbio.jsp?EID=100012)
Question originally posed by Kedar Aras
(http://www.jguru.com/guru/viewbio.jsp?EID=80325

Data such as <!-- --> and other tags you do not wish to become part of the XML tag
scheme should be properly encoded. Convert & to &amp;, convert < to &lt;, and
convert > to &gt;.

Comments and alternative answers

Another promising way is the <!CDATA[[... tags ......


Author: Niclas Meier (http://www.jguru.com/guru/viewbio.jsp?EID=245132), Nov 9,
2000
Another promising way is the <!CDATA[[... tags ... ]]> directive. It forces the parser
to take the content unseen. So any XML like structures in the CDATA (content-data)
block will not be parsed.

Another way is to use a CDATA section: <![CDATA[...


Author: Alan Moore (http://www.jguru.com/guru/viewbio.jsp?EID=25369), Nov 9,
2000
Another way is to use a CDATA section:

<![CDATA[ <html></html> ]]>

Question: What is the XML Schema equivalent of using "|" in a DTD


specification?

I'm building an XML schema, where I want to specify that an element -


request - can contain a certain element - authentication - OR another -
application, but not both at the same time.

Schema

<element name="request">
<complexType>
<element ref="authentication" minOccurs='0' maxOccurs='1'/>
<element ref="application" minOccurs='0' maxOccurs='1'/>
</complexType>
</element>

DTD
<ELEMENT request (authentication | application)>

Location: http://www.jguru.com/faq/view.jsp?EID=248864
Created: Nov 8, 2000 Modified: 2000-11-09 10:46:06.426
Author: Dane Foster (http://www.jguru.com/guru/viewbio.jsp?EID=228595)
Question originally posed by Stefan Lecho
(http://www.jguru.com/guru/viewbio.jsp?EID=32509
Please note: I'm using the Schema language as specified by the current W3C
Candidate recommendation (...2000/10/XMLSchema).

<xsd:element name="request">
<xsd:complexType>
<xsd:choice>
<xsd:element ref="authentication" minOccurs="0" maxOccurs="1"/>
<xsd:element ref="application" minOccurs="0" maxOccurs="1"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
Is there an interface for working with an XML file directly. From SUN's
technical tips, I know that I can write the whole XML Document to a file.
That is not very efficient if all I want to do is modify an element within an
XML file.
Location: http://www.jguru.com/faq/view.jsp?EID=248869
Created: Nov 8, 2000 Modified: 2000-11-09 10:50:05.244
Author: Dane Foster (http://www.jguru.com/guru/viewbio.jsp?EID=228595)
Question originally posed by Hoi Fai Leung
(http://www.jguru.com/guru/viewbio.jsp?EID=207210

The Document Object Model (DOM). You can find the specification here. Xerces form
Apache supports DOM Level 1 and DOM Level 2

Is it possible to create a new XML document by parsing an existing XML


document and a corresponding XSL file?
Location: http://www.jguru.com/faq/view.jsp?EID=248870
Created: Nov 8, 2000 Modified: 2000-11-09 17:09:22.143
Author: Dane Foster (http://www.jguru.com/guru/viewbio.jsp?EID=228595)
Question originally posed by Pavan Methukupally
(http://www.jguru.com/guru/viewbio.jsp?EID=201134

Yes. That's what XSLT is all about. Check out the specification. There are also tons of
resources on the web that show how to do this, including xml.com

I have to interface 3rd party applications having data in XML format with my
site. I am currently using JSPs and EJBs for my site. How can I take the
instream data (XML format - text) , convert it to an object (DOM/SAX) and
call it from my JSP page. Similarly how do I convert an object back into the
XML format and send it across to the third party, providing an XML interface.
I know the DTDs of the 3rd party applications.
Location: http://www.jguru.com/faq/view.jsp?EID=249891
Created: Nov 9, 2000 Modified: 2000-11-09 11:04:38.758
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12) Question
originally posed by Loveen Advani
(http://www.jguru.com/guru/viewbio.jsp?EID=27396

Simply invoke an XML parser on the server passing it the incoming XML stream. The
parser will produce DOM objects or you can register for SAX events if you prefer an
event based parse. Once you have DOM or SAX access to the XML on the server, you
can process it as needed.

To return XML, you can write XML directly in your Java code to your output stream or
you can create DOM objects and then ask the DOM to write it to your stream.

We're trying to design a system based on a J2EE application server. The


system can have multiple clients: interactive voice response, web, wireless,
PDAs. How can we utilize XML? Should servlets, jsp, session bean, entity
bean, or javabean generate xml output to send to these different clients?
Location: http://www.jguru.com/faq/view.jsp?EID=249993
Created: Nov 9, 2000 Modified: 2000-11-10 14:20:55.166
Author: Shuchi Agarwal (http://www.jguru.com/guru/viewbio.jsp?EID=30635)
Question originally posed by Misak Boulatian
(http://www.jguru.com/guru/viewbio.jsp?EID=249894

XML can be used for both static as well as dynamic content.

For rendering of static data you can probably apply XSL stylesheets on the XML
content files stored on your Web Server. These XSL files can generate HTML or WML
depending on where the response has to be sent. For dymanic content you can pull
out enterprise data or data from a B2B transaction data and generate XML on the fly.
You can still use the XSL stylesheets to send the data to different clients.

How you do it is entirely dependent on the requirements of the application but you
should still utilize the MVC pattern of web application design. So in this case your
JavaBeans/session beans will contain the XML data (static or dynamic) which has to
be rendered, JSP will be the view used to generate/render that data on the client and
servlets as an interaction controller. EJB's are required if the application is highly
transaction oriented.

Comments and alternative answers

What about manipulating the xml document


Author: agus acil (http://www.jguru.com/guru/viewbio.jsp?EID=1061158), Feb 27,
2003
I know that xml can be generated dynamically from database.
But how about manipulating xml as datastore it self ?

How can I make a string appear as it is when retrieved from XML? For
example, string a b c should appear as it is and not a+b+c.
Location: http://www.jguru.com/faq/view.jsp?EID=250744
Created: Nov 10, 2000 Modified: 2000-11-13 08:10:59.283
Author: Surendra Chauhan (http://www.jguru.com/guru/viewbio.jsp?EID=45993)
Question originally posed by Rajesh C
(http://www.jguru.com/guru/viewbio.jsp?EID=45707

A string will appear as it is if you use the right character code for example UTF-8
supported by the operating system and the application.

When you write to an XML file, use the character code the application which is
reading the file is supporting.

How can I generate an XML document from a relational database? This XML
should be in the format specified by a particular DTD.
Location: http://www.jguru.com/faq/view.jsp?EID=251541
Created: Nov 11, 2000 Modified: 2000-11-13 08:07:52.357
Author: Dieter Wimberger (http://www.jguru.com/guru/viewbio.jsp?EID=25708)
Question originally posed by Srinath Mani
(http://www.jguru.com/guru/viewbio.jsp?EID=230075

Well you can either code it by hand, retrieving the values from the database and
writing out an XML file according to the DTD, or you can search for a tool that helps
you to do it. I would suggest to take a look at Castor (http://castor.exolab.org) which
could serve well for this task.
Comments and alternative answers

If you're using an Oracle database, Oracle provide...


Author: Tim Morrow (http://www.jguru.com/guru/viewbio.jsp?EID=48096), Nov 15,
2000
If you're using an Oracle database, Oracle provide a number of useful utilities for
converting SQL query output to XML: XML SQL utility, XSQL servlet etc.
http://technet.oracle.com/tech/xml/index.htm (registration required) provides info.

Another way is to use DOM interface to database. U...


Author: Pavel Tavoda (http://www.jguru.com/guru/viewbio.jsp?EID=19085), Dec 6,
2000
Another way is to use DOM interface to database. Usually DOM parsers are used to
parse files but here are available solutions which implement database access via DOM
interface. Then you can use any standard XSLT transformation engine with database
DOM to produce HTML, text and also XML.

I have written an XML file (test.xml) and an XSLT stylesheet for WML 9test-
wml.xsl). I am trying to combine them with Cocoon. While using the Nokia
WAP Tollkit 2.0 to load the test.xml file I get the error message "Error.
Server returned unsupported content type: text/html" Can anybody help
me? I have to say that I am running the Apache web server and I have
configured it with all the relevant MIME types.
Location: http://www.jguru.com/faq/view.jsp?EID=251542
Created: Nov 11, 2000 Modified: 2000-11-13 08:08:46.563
Author: Dieter Wimberger (http://www.jguru.com/guru/viewbio.jsp?EID=25708)
Question originally posed by Panos Konstantinidis
(http://www.jguru.com/guru/viewbio.jsp?EID=242435

The header of your XML file has to contain following lines:


<?xml-stylesheet href="9test-wml.xsl" type="text/xsl" media="wap"?>
<?cocoon-process type="xslt"?>

The XSL file 9test-wml.xsl should have a line stating:


<xsl:processing-instruction name="cocoon-
format">type="text/wml"</xsl:processing-instruction>
in the main template that matches the root element of your XML document.

What does SGML stand for?


Location: http://www.jguru.com/faq/view.jsp?EID=268111
Created: Dec 2, 2000 Modified: 2000-12-06 08:50:09.796
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

SGML stands for Standard Generalized Markup Language.

What does XSLT stand for?


Location: http://www.jguru.com/faq/view.jsp?EID=268450
Created: Dec 3, 2000 Modified: 2000-12-06 08:50:35.519
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

XSLT stands for Extensible Stylesheet Language Transformations. For more on what
XSLT is, see another FAQ.

What does SVG stand for?


Location: http://www.jguru.com/faq/view.jsp?EID=270702
Created: Dec 6, 2000 Modified: 2000-12-06 08:45:53.415
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

SVG stands for Scalable Vector Graphics.

What is SVG?
Location: http://www.jguru.com/faq/view.jsp?EID=270704
Created: Dec 6, 2000 Modified: 2000-12-06 08:46:11.44
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

SVG is an evolving W3C standard for defining two-dimensional graphics in an XML


grammar.
Comments and alternative answers

check out http://www.adobe.com/svg/


Author: Kyle Dyer (http://www.jguru.com/guru/viewbio.jsp?EID=265465), Dec 11,
2000
check out http://www.adobe.com/svg/

Where can I get a viewer/browser plugin for SVG files?


Location: http://www.jguru.com/faq/view.jsp?EID=270711
Created: Dec 6, 2000 Modified: 2000-12-06 08:46:36.135
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

The W3C maintains a list of viewers. Just grab one like the Adobe or Java-based
Jackaroo viewer.
Comments and alternative answers

The German IN-GmbH wrote a vector graphics editor named...


Author: Christoph Krüger (http://www.jguru.com/guru/viewbio.jsp?EID=30208), Dec
8, 2000
The German IN-GmbH wrote a vector graphics editor named Sphinx which now
features an SVG export. Check out their SVG Demo Page under: http://www.in-
gmbh.de/de/Produkte/in-gmbh/sphinxopen/svg/svg_demos.htm

To get the Adobe SVG plugin for IE jump to:


http://www.adobe.com/svg/viewer/install/main.html

Batik written in Java


Author: Chris Kelly (http://www.jguru.com/guru/viewbio.jsp?EID=408964), Jun 27,
2001

Batik, which is open source and part of the Apache project, is written in Java, and includes an SVG
generator and viewer.

It also includes a Swing component called JSVGCanvas which could be added to an applet.

I think the Batik team are also about to include direct support for SVGs in Mozilla.

Where can I find a repository of custom/user-defined doclets?


Location: http://www.jguru.com/faq/view.jsp?EID=270852
Created: Dec 6, 2000 Modified: 2000-12-06 16:18:31.86
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

Sun maintains a list in the javadoc FAQ.

What is meant by well-formed?


Location: http://www.jguru.com/faq/view.jsp?EID=271037
Created: Dec 6, 2000 Modified: 2000-12-11 09:21:27.592
Author: Seok-jeong Hwang (http://www.jguru.com/guru/viewbio.jsp?EID=30431)
Question originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

An XML document is well-formed if it conforms to the following three rules:


The document must have a single root element.
The elements must be properly nested. This means that the form below is not
allowed.
<a><b></a></b>
Start and end tags must be matched.

I tried to write a simple XML SAX handler, extending the HandlerBase to


parse an XML file. But, if the XML file contains some Chinese or Japanese, it
can't get the Chinese or Japanese text. What should I do ?
Location: http://www.jguru.com/faq/view.jsp?EID=271046
Created: Dec 6, 2000 Modified: 2000-12-11 09:12:59.43
Author: Seok-jeong Hwang (http://www.jguru.com/guru/viewbio.jsp?EID=30431)
Question originally posed by Eric Chow
(http://www.jguru.com/guru/viewbio.jsp?EID=25181

Java can handle unicode like XML.


And if you want handler other than English, a document must declare it.

<?XML version="1.0" encoding="euc-kr"?>

The above statement is for Korean. The Java stream class can be set to some other
character set in its constructor.
Is there a doclet for generating XML output from javadoc comments?
Location: http://www.jguru.com/faq/view.jsp?EID=272527
Created: Dec 8, 2000 Modified: 2000-12-11 09:32:18.561
Author: David Hainlin (http://www.jguru.com/guru/viewbio.jsp?EID=272526)
Question originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

There is a doclet called JDox which produces a single XML file from a single javadoc
session. Additional info can be found at the JDox Home.

I have to design an application that converts XHTML documents to XML


documents using Java. What Java APIs should I use?
Location: http://www.jguru.com/faq/view.jsp?EID=275121
Created: Dec 11, 2000 Modified: 2000-12-12 10:42:00.417
Author: Kyle Dyer (http://www.jguru.com/guru/viewbio.jsp?EID=265465) Question
originally posed by Mugdha Vairagade
(http://www.jguru.com/guru/viewbio.jsp?EID=123868

XHTML is well formed XML, according to the spec. As such you should be able to use
a XSLT Java API and a stylesheet to transform XHTML to the XML you want.

For XSLT API look at:


http://www.jclark.com/xml/xt.html
James Clark helped write XSLT
-or-
http://users.iclway.co.uk/mhkay/saxon/
Saxon is the work of Michael Kay. He also wrote a good book called "XSLT
Programmer's Reference".

The Java part of such an app would be minimal -- just enough to open the XML docs
and apply the XSLT to it. Kay's work includes a standard API for this which abstracts
the XSLT engine. I think saxon and xt include apps that already do this.

Is there a tool to generate a sample XML file from a given valid DTD or
schema?
Location: http://www.jguru.com/faq/view.jsp?EID=277288
Created: Dec 13, 2000 Modified: 2000-12-14 10:14:32.576
Author: Darius Kasad (http://www.jguru.com/guru/viewbio.jsp?EID=45296)
Question originally posed by p shah
(http://www.jguru.com/guru/viewbio.jsp?EID=255775

Check out XML Generator from IBM Alphaworks.


http://www.alphaworks.ibm.com/tech/xmlgenerator
Comments and alternative answers

xmlspy is the tool to generate the XML doc from the...


Author: amit sharma (http://www.jguru.com/guru/viewbio.jsp?EID=25171), Dec 24,
2000
xmlspy is the tool to generate the XML doc from the DTD /Schema You can
download that xmlspy from http://www.xmlspy.com the recent version is 3.5 but is
beta.

What are good, open-sourced, SGML to XML translators or Java based SGML
parsers?
Location: http://www.jguru.com/faq/view.jsp?EID=277317
Created: Dec 13, 2000 Modified: 2000-12-14 10:15:34.495
Author: John Dahle (http://www.jguru.com/guru/viewbio.jsp?EID=75944) Question
originally posed by John Dahle
(http://www.jguru.com/guru/viewbio.jsp?EID=75944

I have discovered a work around that I thought I might share with you all. I was able
to parse SGML by adding end tags to each open SGML tag and parsing it using a
conventional XML parser. It's not pretty, but it worked.

I am using IBM XML Parser 2.0.16. I am parsing an XML document that has
no DTD. But my parser is giving errors related to DTD. How can I parse XML
without DTD?
Location: http://www.jguru.com/faq/view.jsp?EID=283611
Created: Dec 21, 2000 Modified: 2000-12-22 11:06:42.601
Author: Ibrahim Levent (http://www.jguru.com/guru/viewbio.jsp?EID=131172)
Question originally posed by Ibrahim Levent
(http://www.jguru.com/guru/viewbio.jsp?EID=131172

You can't parse because this version is not providing any support for this kind of
parsing. Validity is one of the most important issue in XML structure. Validation is
based on internal or external DTD. If you do not consider validation, you can parse
yourself in your code without needing any XML parser. There are many tools that
checks if your XML is well-formed or valid. You can use these tools.
Comments and alternative answers

In com.ibm.xml.parsers package, there are classes ...


Author: Ibrahim Levent (http://www.jguru.com/guru/viewbio.jsp?EID=131172), Dec
23, 2000
In com.ibm.xml.parsers package, there are classes provides this. These classes are
NonValidatingDOMParser, NonValidatingParser, NonValidatingTXDOMParser.

Re: In com.ibm.xml.parsers package, there are classes ...


Author: Michael Griffith (http://www.jguru.com/guru/viewbio.jsp?EID=748136),
Feb 5, 2002
I am using Xerxes and tried doing this very thing with the NonValidatingParser
class, but still get the same DTD not found error. Any clues?

Are there any GUI editors which would ease the creation of an XSL file for
using it with FO (flow objects for PDF, CSS etc.. ) ?
Location: http://www.jguru.com/faq/view.jsp?EID=283634
Created: Dec 21, 2000 Modified: 2000-12-22 11:01:12.98
Author: Ibrahim Levent (http://www.jguru.com/guru/viewbio.jsp?EID=131172)
Question originally posed by piyush sheth
(http://www.jguru.com/guru/viewbio.jsp?EID=38282
These may help you : http://www.unicorn-enterprises.com/,
http://xml.apache.org/fop/, http://www.antennahouse.com/xslformatter.html ,
http://www.esng.dibe.unige.it/REXP , http://www.renderx.com/FO2PDF.html

Are there any mailing lists covering XML encryption?


Location: http://www.jguru.com/faq/view.jsp?EID=285040
Created: Dec 23, 2000 Modified: 2000-12-23 10:18:00.179
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Yes, check out the W3C's XML Encryption mailing list.

Is there some API using which I can modify a very large XML file? DOM goes
out of memory and SAX just doesn't fit the bill.
Location: http://www.jguru.com/faq/view.jsp?EID=285686
Created: Dec 25, 2000 Modified: 2001-01-02 08:26:48.3
Author: Chandra Patni (http://www.jguru.com/guru/viewbio.jsp?EID=33585)
Question originally posed by Mohit Khurana
(http://www.jguru.com/guru/viewbio.jsp?EID=119823

One of the upcoming polular API for XML parsing is JDOM (www.jdom.org). It is a
sort of hybrid approach between DOM and SAX. JDOM API lets you use either DOM or
SAX parser under the hood and uses the power of Java collection API which makes
XML parsing a lot more easier. JDOM also let's you create your own XML document
from scratch and modify original XML document by using either SAX or DOM API
under the covers. JDOM API's SAXBuilder class is probably fits the bill for parsing a
very large XML document which uses SAX API for parsing and allows you to modify
the document at the same time.

To get started on JDOM, you can refer to articles published at javaworld.com.


Article 1
Article 2

What is the "Hello World" program for VoiceXML?


Location: http://www.jguru.com/faq/view.jsp?EID=286246
Created: Dec 26, 2000 Modified: 2000-12-27 05:49:32.097
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by shams Tabrez
(http://www.jguru.com/guru/viewbio.jsp?EID=264911

XML.COM's Hello, Voice World has an example using IBM's alphaWorks voiceXML
interpreter. One more example is at An Introduction To VoiceXML.
Comments and alternative answers

Here is an example... <?xml version="1.0...


Author: divya nagireddy (http://www.jguru.com/guru/viewbio.jsp?EID=124780), Jan
4, 2001
Here is an example...
<?xml version="1.0"?>
<vxml version="1.0">
<form>
<block>Hello World!</block>
</form>
</vxml>
You can test how it works at www.voicegenie.com. You can get the vxml specs at
www.voicexml.org.

How can I use an IMG tag for including images in XSL stylesheets?
Location: http://www.jguru.com/faq/view.jsp?EID=286361
Created: Dec 26, 2000 Modified: 2001-01-02 17:58:41.159
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by Monica gupta
(http://www.jguru.com/guru/viewbio.jsp?EID=263610

<xsl:element name="IMG" >


<xsl:attribute name="SRC">myimage.gif</xsl:attribute>
</xsl:element>
Comments and alternative answers

IMAGE tag in xsl, xml processing


Author: A K (http://www.jguru.com/guru/viewbio.jsp?EID=472913), Aug 20, 2001
hi there,
shud the code shown below render the image when used in xsl, even when the image
is being generated by the servlet and the http://***** is being entered in the place of
gif name.
as u know, this wrks perfectly fine with html, with browser doin the work..when xalan
transformation is used, the image is not displayed.
code:
<xsl:element name="IMG"> <xsl:attribute
name="src">http://localhost:portnumber/domain/servletname </xsl:attribute>
</xsl:element> Any thots on this..?? Most welcome
Thanx,
AK

Is there some API using which I can modify a very large XML file? DOM goes
out of memory and SAX just doesn't fit the bill.
Location: http://www.jguru.com/faq/view.jsp?EID=287455
Created: Dec 27, 2000 Modified: 2001-01-08 08:33:40.19
Author: Rishi Prakash (http://www.jguru.com/guru/viewbio.jsp?EID=62455)
Question originally posed by Mohit Khurana
(http://www.jguru.com/guru/viewbio.jsp?EID=119823

Try JDOM. See www.jdom.org for details.

What do I need in order to use the msxml.dll parser from IE5 if the IE5 is
not installed on the target system? Our software uses the IE XML parser for
export of data in XML format, but some users do not want to be dependent
on IE to run our software. This has lead to some problems, since the parser
does not appear to work without a complete installation of IE5. What DLLs
need to be included in the installation kit for for the parser to work without
IE? Alternatively what kind of minimum custom installation of IE is needed?
Is there a better well documented XML parser available to use in a WINNT
4.0/Win2000 environment that is not browser dependent?
Location: http://www.jguru.com/faq/view.jsp?EID=288439
Created: Dec 28, 2000 Modified: 2001-01-08 08:31:20.221
Author: Kulwant S. Bhatia (http://www.jguru.com/guru/viewbio.jsp?EID=62359)
Question originally posed by Erik S
(http://www.jguru.com/guru/viewbio.jsp?EID=281605

I've had similar problems. The MSXML parser will only work either with IE5 installed
or ADO2.5 libraries installed which Microsoft didn't bother to document. If you don't
have the DLLs you can download them from the Microsoft site. I'm currently using
Xerces for my XML parsing and the documentation is excellent. Check out
xml.apache.com.

How can I serialize and de-serialize an XML Node? For example to transfer a
Node object over TCP?
Location: http://www.jguru.com/faq/view.jsp?EID=290154
Created: Dec 31, 2000 Modified: 2001-01-02 18:02:07.461
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by Dan Corkum
(http://www.jguru.com/guru/viewbio.jsp?EID=85224

DOM API is not really geared towards serialization/de-serialization. Try JDOM from
http://www.jdom.org/. It has adapters to work with existing DOM documents. A lot
of classes implement java.io.Serializable and java.lang.Cloneable interfaces to make
coding easier.

Where can I find a simple Java example of creating an XML document and
saving it to a file?
Location: http://www.jguru.com/faq/view.jsp?EID=292249
Created: Jan 3, 2001 Modified: 2001-01-08 08:21:29.194
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by Eric Chow
(http://www.jguru.com/guru/viewbio.jsp?EID=25181

The following example is an extract from Intro to JDOM by Elliotte Rusty Harold.
import java.io.*;
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import org.apache.xml.serialize.*;

public class HelloDOM {

public static void main(String[] args) {

try {

DOMImplementationImpl impl = (DOMImplementationImpl)


DOMImplementationImpl.getDOMImplementation();
DocumentType type = impl.createDocumentType("GREETING", null,
null);

// type is supposed to be able to be null,


// but in practice that didn't work
DocumentImpl hello
= (DocumentImpl) impl.createDocument(null, "GREETING", type);

Element root = hello.createElement("GREETING");

// We can't use a raw string. Instead we have to first create


// a text node.
Text text = hello.createTextNode("Hello DOM!");
root.appendChild(text);

// Now that the document is created we need to *serialize* it


try {
OutputFormat format = new OutputFormat(hello);
XMLSerializer serializer = new XMLSerializer(System.out,
format);
serializer.serialize(root);
}
catch (IOException e) {
System.err.println(e);
}
}
catch (DOMException e) {
e.printStackTrace();
}

}
Comments and alternative answers

I couldnt run this program


Author: suresh babu (http://www.jguru.com/guru/viewbio.jsp?EID=503041), Jul 18, 2002
when I execute this program, I'm getting the error
Exception in thread "main" java.lang.NoSuchMethodError at
org.apache.xml.serialize.XMLSerializer.serializeElement(XMLSerializer.java:696)
at
org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(BaseMarkupSerializer.java:1062)
at org.apache.xml.serialize.BaseMarkupSerializer.serialize(BaseMarkupSerializer.java:426)
at HelloDOM.main(HelloDOM.java:28)
could u please help me
thanks in advance....
suresh

Program to create XML File using Java Objects


Author: Vivek Viswanathan (http://www.jguru.com/guru/viewbio.jsp?EID=1090424),
Jun 3, 2003
I am also looking for a sample program which will create an XML File from Java
Objects (entity class). Can anyone help me out? I would like to use the SAX API if
possible. Thanks in advance.

I'm calling an XML script and parsing it in my servlet. Where do I put my


DTD file so that my parser finds it? I am using Tomcat 3.1 and IBM's XML
parser XML4J.
Location: http://www.jguru.com/faq/view.jsp?EID=293229
Created: Jan 4, 2001 Modified: 2001-01-08 08:16:44.24
Author: divya nagireddy (http://www.jguru.com/guru/viewbio.jsp?EID=124780)
Question originally posed by divya nagireddy
(http://www.jguru.com/guru/viewbio.jsp?EID=124780

The DTD file should be placed in the default public_html or htdocs directory on the
webserver. This directory is the public folder where all the html and jsp pages, along
with other files that must be published to the client are placed. The DTD file can be
accessed in the XML script as shown below.

< !DOCTYPE Employee


SYSTEM "http://host:port/Employee.dtd">
Comments and alternative answers

a clear answer, I hope


Author: Stevens Gestin (http://www.jguru.com/guru/viewbio.jsp?EID=458859), Aug
21, 2001
I had the same problem, read my answer here :

http://java.isavvix.com/forums/viewThread.jsp?forum=39&thread=541

Is it possible to use a SAX parser inside a servlet? How do I set the


DocumentHandler?
Location: http://www.jguru.com/faq/view.jsp?EID=293267
Created: Jan 4, 2001 Modified: 2001-01-08 08:29:24.566
Author: divya nagireddy (http://www.jguru.com/guru/viewbio.jsp?EID=124780)
Question originally posed by grant liu
(http://www.jguru.com/guru/viewbio.jsp?EID=140866

Yes it is possible to use a SAX parser inside a servlet.


URL url1 = new URL("http://host:port/yourfile.xml");
BufferedReader in = new BufferedReader( new
InputStreamReader(url1.openStream()));
AppContentHandler myContentHandler = new AppContentHandler();
XMLReader parser = new SAXParser();
parser.setContentHandler (myContentHandler);
where AppContentHandler is a class which implements ContentHandler, hence
implements all the parsing methods in the ContentHandler.

What is cXML and where can I learn more abou it?


Location: http://www.jguru.com/faq/view.jsp?EID=293797
Created: Jan 4, 2001 Modified: 2001-01-08 07:55:53.501
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)
cXML stands for Commerce XML. It is basically a set of standard DTD for doing B2B
electronic commerce. You can learn more about it from http://www.cxml.org/home/.

Where can I get the VoiceXML DTD?


Location: http://www.jguru.com/faq/view.jsp?EID=299391
Created: Jan 10, 2001 Modified: 2001-01-11 06:13:38.566
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

The 1.0 VoiceXML DTD is available from the Voice XML Forum.

I want to parse an XML file stored on a remote server. Can I use the DOM
for the same without downloading the file?
Location: http://www.jguru.com/faq/view.jsp?EID=299965
Created: Jan 11, 2001 Modified: 2001-01-11 12:17:32.305
Author: Surendra Chauhan (http://www.jguru.com/guru/viewbio.jsp?EID=45993)
Question originally posed by Arti W
(http://www.jguru.com/guru/viewbio.jsp?EID=298558

You can use the DOM to parse the XML file without downloading the file from the
remote server. But you have to use the URL to access the file. Here is sample code.

DOMParser parser = new DOMParser();


URL url = createURL(remotefile);
parser.parse(url);
// Obtain the document.
XMLDocument doc = parser.getDocument();

Are there any FAQs available on VoiceXML?


Location: http://www.jguru.com/faq/view.jsp?EID=300142
Created: Jan 11, 2001 Modified: 2001-01-12 07:19:21.777
Author: divya nagireddy (http://www.jguru.com/guru/viewbio.jsp?EID=124780)
Question originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

Here are two:


http://studio.tellme.com/faq/techoverview_voice_faq.html
http://developer.voicegenie.com/faq.php

Where can I find the following packages?

import com.sun.xml.parser;
import com.sun.xml.tree;
import org.w3c.dom;
import org.xml.sax;

Location: http://www.jguru.com/faq/view.jsp?EID=301021
Created: Jan 12, 2001 Modified: 2001-01-12 11:26:21.985
Author: Surendra Chauhan (http://www.jguru.com/guru/viewbio.jsp?EID=45993)
Question originally posed by thulasireddy vakati
(http://www.jguru.com/guru/viewbio.jsp?EID=292782
They can be downloaded from Java Project X
This will include the xml-tr2.zip file. Unzip the file to a folder and include xml.jar in
the classpath for the Java installation on your machine.

What is XML data binding?


Location: http://www.jguru.com/faq/view.jsp?EID=301406
Created: Jan 12, 2001 Modified: 2001-01-14 19:55:24.78
Author: Chandra Patni (http://www.jguru.com/guru/viewbio.jsp?EID=33585)

XML data binding is a concept of generating Java classes (source code) from XML
schema languages. Objects of such generated classes may represent XML document
which validates against the schema. Such mapping of XML schema to Java classes
and XML data to Java objects is called XML data binding.
JSR 00031, also known as project Adelard provides specification of such facility. It is
done by providing by providing a schema in question and a data binding schema and
compiling them into Java source code. The generated code contains getters, setters
and methods for marshalling, unmarshalling and validation.
The Data binding schema can be used to fine-tune the class generation code. Adelard
provides provision for generating a default data binding schema from the schema in
question.

In the three tier architecture, presentation layer, business layer and


database layer, where can I classify the XML?
Location: http://www.jguru.com/faq/view.jsp?EID=301463
Created: Jan 12, 2001 Modified: 2001-01-15 19:40:21.517
Author: Chandra Patni (http://www.jguru.com/guru/viewbio.jsp?EID=33585)
Question originally posed by nitin bhatti
(http://www.jguru.com/guru/viewbio.jsp?EID=299892

XML can be best described in three tier architecture as the portable data which can
be sent across the wire. XML can be thought of as data flowing between these tiers.

How can I validate an XML file with a schema using the Xerces parser?
Location: http://www.jguru.com/faq/view.jsp?EID=301682
Created: Jan 13, 2001 Modified: 2001-01-15 19:31:01.099
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7) Question
originally posed by rahul bose
(http://www.jguru.com/guru/viewbio.jsp?EID=219615

Set the validation feature to true:


DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation",
true);
parser.parse(...);
Comments and alternative answers

Re:How can I validate an XML file with a schema using the Xerces parser?
Author: Arun Iyer (http://www.jguru.com/guru/viewbio.jsp?EID=457780), Jul 19,
2001
How can I validate the XML without specifying an internet address?
I want to develop a web site in XML. What software is required and where
can I find it?
Location: http://www.jguru.com/faq/view.jsp?EID=302114
Created: Jan 14, 2001 Modified: 2001-01-15 19:41:48.63
Author: Brian Ewins (http://www.jguru.com/guru/viewbio.jsp?EID=301660)
Question originally posed by yashwant bandla
(http://www.jguru.com/guru/viewbio.jsp?EID=299485

If you are asking how to develop a dynamic java-based website which uses XML and
stylesheets to produce HTML/WML/SVG etc, then you should look at:

xml.apache.org for the Xerces parser and the Xalan XSL-T engine;

jakarta.apache.org for a servlet engine to build on;

Oracle Technet if you're building a site on top of Oracle (you can get their XSQL
servlet, its very easy to use) - or read this javaworld article if you're using a non-
Oracle DB

Producing useful translations from XML using XSL is half the battle. You should get
hold of the Mulberry reference cards, and I found that Neil Bradleys "The XSL
Companion" was a decent reference.

Finally you can't mention places to get XML tools without alphaworks - lots of IBM
stuff you can download, including an XSL editor.

Comments and alternative answers

Morphis provides a framework for XSLT processing, and...


Author: Thomas LeRoux (http://www.jguru.com/guru/viewbio.jsp?EID=308972), Jan
22, 2001
Morphis provides a framework for XSLT processing, and specializes in wireless
(WML/HDML/HTML/CHTML) markup conversion. Plus, it's open sourced.

What is XLink?
Location: http://www.jguru.com/faq/view.jsp?EID=302116
Created: Jan 14, 2001 Modified: 2001-01-15 19:37:45.076
Author: Brian Ewins (http://www.jguru.com/guru/viewbio.jsp?EID=301660)
Question originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

It's an emerging standard for describing hypertext links in XML. Its not limited to the
one-way links you have in HTML - you can have bidirectional links, annotations, you
can link to any location in a document (using XPointers), and so on. The official
homepage is at W3C and you can find an introduction here.

Is there a Java API which can generate an XPath String from the source
node to the destination node?

For example:
<-- <section>
<titre nom="titre1">
<para>bla</para>
<para>pou</para>
</titre>
</section>-->
source node : titre;
destination node : the second para;

The XPath String I need to be generated : /titre/p[position() = 2]


Location: http://www.jguru.com/faq/view.jsp?EID=302120
Created: Jan 14, 2001 Modified: 2001-01-14 19:56:24.895
Author: Brian Ewins (http://www.jguru.com/guru/viewbio.jsp?EID=301660)
Question originally posed by mehdi lababidi
(http://www.jguru.com/guru/viewbio.jsp?EID=48184

Your best bet is to write a class that extends org.xml.sax.helpers.XMLFilterImpl. If


you use a SAX parser on your documents you can insert this in your app to trap
startElement and endElement events, and use these to maintain a stack of elements.
You would then only need a method to empty the stack (setting your source node to
be the current one) and another method to read the stack as a string (ie reading the
relative position of the destination node). endElements recieved which don't have a
matching startElement in the stack would generate a '../' XPath; you also need to
keep track of closed tag pairs at any element (in order to figure out 'position()').
None of this is very hard; even calculating the reverse path isn't too bad. I believe
there is an IBM tool at http://alphaworks.ibm.com/ which can do the calculations for
you - in an editor - and similarly there's an emacs addon for psgml-mode at
http://www.megginson.com/ . However I've not seen a publicly available API which
will do the task described.

Are there tools which can convert Microsoft Word documents into XML?
Location: http://www.jguru.com/faq/view.jsp?EID=302121
Created: Jan 14, 2001 Modified: 2001-01-14 19:57:15.499
Author: Brian Ewins (http://www.jguru.com/guru/viewbio.jsp?EID=301660)
Question originally posed by mehdi lababidi
(http://www.jguru.com/guru/viewbio.jsp?EID=48184

Yes. wvware is one (free) : http://www.wvware.com/. You should be aware that


Word is really just rich text with embedded documents of other (non-Word) types -
these embedded objects, such as pictures, may not be convertible.
Comments and alternative answers

The XML Spy is another option. You can download a ...


Author: Annapurni Srikar (http://www.jguru.com/guru/viewbio.jsp?EID=250950),
Mar 2, 2001
The XML Spy is another option. You can download a 30-day evaluation version from
www.xmlspy.com

Free MS tool
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7), May 15,
2001
Microsoft has a tool to help you Export a Word Document to XML.

How can I use a Java servlet to output XML and XSLT?


Location: http://www.jguru.com/faq/view.jsp?EID=302123
Created: Jan 14, 2001 Modified: 2001-01-14 19:58:54.605
Author: Brian Ewins (http://www.jguru.com/guru/viewbio.jsp?EID=301660)
Question originally posed by Eric Chow
(http://www.jguru.com/guru/viewbio.jsp?EID=25181

See this page of examples for apache's Xalan XSL-T processor:


http://xml.apache.org/xalan/usagepatterns.html Xalan includes a sample servlet.
Comments and alternative answers

OK, but I can't get the sample set up. Xalan runs ...
Author: Ion Freeman (http://www.jguru.com/guru/viewbio.jsp?EID=331356), Feb 16,
2001
OK, but I can't get the sample set up. Xalan runs great, and do Tomcat's servlet
samples, but I'm needing some help in editing the server.xml and web.xml files, or
even knowing which web.xml file to edit. Should I create a WEB-INF folder? Does it
need a classes subfolder? I'm using the Tomcat ISAPI dll on W2K/IIS5. Tomcat's in
c:\inetpub\tomcat, Xalan's in c:\inetpub\xalan. ion

How can I stop a SAX parser parsing an XML file?


Location: http://www.jguru.com/faq/view.jsp?EID=302125
Created: Jan 14, 2001 Modified: 2001-01-15 19:29:55.757
Author: Brian Ewins (http://www.jguru.com/guru/viewbio.jsp?EID=301660)
Question originally posed by Mauro Gagni
(http://www.jguru.com/guru/viewbio.jsp?EID=138984

This problem arises because org.xml.sax.XMLReader.parse() blocks until parsing is


complete. It could be a problem on huge documents. Run the parser in another
thread. Then kill the thread.

Slightly cleaner is to implement your own ContentHandler, (eg extending


XMLFilterImpl) so that as soon as you tell it to stop listening, all events are ignored
and the parser thread is killed - this prevents stray events making it through.

How can I generate and store an XML file by writing a PL/SQL procedure?
Location: http://www.jguru.com/faq/view.jsp?EID=302129
Created: Jan 14, 2001 Modified: 2001-01-15 19:32:54.367
Author: Brian Ewins (http://www.jguru.com/guru/viewbio.jsp?EID=301660)
Question originally posed by Srini Vasan
(http://www.jguru.com/guru/viewbio.jsp?EID=284085

Oracle provides a package to do this. Download it from Technet. Internally the


PL/SQL package calls the Java implementation.
In an XML file with an element called <file> DATA </file>. DATA can be a
BLOB or a filename with a PATH specification. How do we distinguish
between two types of files while parsing these file types with SAX parser on
IBM xml4j parser? Assume that the handlers are implemented for these
<file> tag and the code is in this handler.
Location: http://www.jguru.com/faq/view.jsp?EID=302133
Created: Jan 14, 2001 Modified: 2001-01-15 19:43:59.049
Author: Brian Ewins (http://www.jguru.com/guru/viewbio.jsp?EID=301660)
Question originally posed by Narayana Kulkarni
(http://www.jguru.com/guru/viewbio.jsp?EID=297709

You can't. What happens when the BLOB just contains a filename? Your DTD isn't too
smart. You should think of doing one of:

- provide an attribute which distinguishes the two cases.

- better still, store them in two different kinds of element.

If this really is a BLOB (and not a CLOB) then the data will - hopefully - only be
included in a CDATA element anyway. The DOM allows you to distinguish between
CDATA and other text nodes.

How can I generate an HTML page containing JavaScript code by applying


an XSL to an XML file? I want JavaScript variables to be populated by values
fetched from the XML file.
Location: http://www.jguru.com/faq/view.jsp?EID=302136
Created: Jan 14, 2001 Modified: 2001-01-15 19:34:34.818
Author: Brian Ewins (http://www.jguru.com/guru/viewbio.jsp?EID=301660)
Question originally posed by Mugdha Vairagade
(http://www.jguru.com/guru/viewbio.jsp?EID=123868

You can include code like:


<script language="javascript" >

foo='<xsl:value-of select="/xpath/for/foovalue"/>'

</script>

in your XSL.

How can I read a paramater passed in a URL to an XML file? For example,
can I get the value of cat1 in
http://www.brainbench.com/xml/bb/common/testcenter/subcat.xml?cat1
=2
Location: http://www.jguru.com/faq/view.jsp?EID=302137
Created: Jan 14, 2001 Modified: 2001-01-15 19:42:57.927
Author: Brian Ewins (http://www.jguru.com/guru/viewbio.jsp?EID=301660)
Question originally posed by Anshul Agarwal
(http://www.jguru.com/guru/viewbio.jsp?EID=98160
XML isn't a language you execute. So something else is processing your XML, right?
Its that something else that can provide you with the values in a number of ways.
One way (if you are generating dynamic XML) is to provide the request as XML
elements. Another way is to pass the parameters to the XSL processor, which can
make them available via extension functions. See here for examples of using Apache
Xalan with extensions.
Comments and alternative answers

The Cocoon technology provided by Apache provides the...


Author: gaurav gupta (http://www.jguru.com/guru/viewbio.jsp?EID=306544), Jan 19,
2001
The Cocoon technology provided by Apache provides the XSP extensible server
language. Using this technology you can pass a parameter to the XML page. In the
XSP backend they are using servlets. In CDATA portion you get parameter values.

Is it VoxML or VoiceXML?
Location: http://www.jguru.com/faq/view.jsp?EID=305408
Created: Jan 17, 2001 Modified: 2001-01-19 07:25:11.003
Author: Kenneth Liu (http://www.jguru.com/guru/viewbio.jsp?EID=304008)
Question originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

It's both. VoiceXML and VoxML are both standards which are used for creating
speech and voice enabled web applications. VoxML was first developed by Motorola in
1998. Later, they were joined by Lucent, AT&T, and IBM to form the VoiceXML Forum
in order to continue the work. VoiceXML 1.0 was accepted by the W3C in May 2000.
Supposedly some of the original authors of the VoxML standard left Motorola to head
up voice application development efforts in the other companies, and so the
VoiceXML Forum was formed.

Each URL on www.brainbench.com has an .xml file extension. However,


when you view the source code for each .xml webpage, HTML tags are
shown. In addition, the FORM ACTION on some of their webpages are being
submitted to another .xml file for processing (i.e. <form method=post
action=/xml/bb/login.xml?variablename=/xml/bb/filename.xml>. What
mechanism(s) are they using to accomplish this?
Location: http://www.jguru.com/faq/view.jsp?EID=305411
Created: Jan 17, 2001 Modified: 2001-01-19 07:27:58.026
Author: Kenneth Liu (http://www.jguru.com/guru/viewbio.jsp?EID=304008)
Question originally posed by John Bowles
(http://www.jguru.com/guru/viewbio.jsp?EID=301092

They are probably using XSLT to transform XML files to HTML format. XSLT is
commonly used to transform XML files which are purely data and content to HTML
files which include both content and presentation information. See the Apache XML
Cocoon project for a good example of this.

What is the difference between VoiceXML and WAP?


Location: http://www.jguru.com/faq/view.jsp?EID=305417
Created: Jan 17, 2001 Modified: 2001-01-19 07:26:13.241
Author: Kenneth Liu (http://www.jguru.com/guru/viewbio.jsp?EID=304008)
Question originally posed by Chandra Patni PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=33585

WML (Wireless Markup Language) is basically a simplified lightweight version of


HTML which is used to display web pages on small mobile phone displays. WAP is to
HTTP as WML is to HTML. A WML/WAP browser is embedded in the phone hardware
to display the pages.

VoiceXML is used to create voice enabled applications for use with any regular phone.
These applications often take the form of phone trees ("press 1 to do this, press 2 to
do that") but can use ASR (automated speech recognition) to recognize spoken
responses in addition to the normal touch tones. VoiceXML can also be used to do
TTS (text to speech) instead of using prerecorded content. VoiceXML documents
require an interpreter which normally runs on a powerful server.

VoiceXML and WAP are often mentioned in the same context because they are both
seen as ways of accessing the Web using a wireless device. With WAP, the screen is
used to display the content; with VoiceXML, TTS is used to play the content aloud.

If I wanted to speech-enable my application, should I use VoiceXML or


JavaSpeech?
Location: http://www.jguru.com/faq/view.jsp?EID=308379
Created: Jan 21, 2001 Modified: 2001-01-22 08:18:48.701
Author: Kenneth Liu (http://www.jguru.com/guru/viewbio.jsp?EID=304008)
Question originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

It depends on what kind of application you are talking about. VoiceXML is really
designed for web applications. It's another markup language - instead of displaying
content on a screen, it uses speech and voice response to interact with the user
(usually) over a phone. If you are writing a desktop application, then VoiceXML
probably won't be suitable. If you want to embed voice commands to control your
app, then JavaSpeech is probably the way to go.

Is there any transformation tool that can convert the XML of one format to
XML of another format?
Location: http://www.jguru.com/faq/view.jsp?EID=311010
Created: Jan 24, 2001 Modified: 2001-01-24 11:27:51.55
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by krithi vasan
(http://www.jguru.com/guru/viewbio.jsp?EID=32927

Once choice is to use XSLT. Here's an extract from XSLT.COM.

"XSLT is a language for transforming XML documents into other XML documents."

"XSLT is designed for use as part of XSL, which is a stylesheet language for XML. In addition to
XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an
XML document by using XSLT to describe how the document is transformed into another XML
document that uses the formatting vocabulary."
"XSLT is also designed to be used independently of XSL. However, XSLT is not intended as a
completely general-purpose XML transformation language. Rather it is designed primarily for the
kinds of transformation that are needed when XSLT is used as part of XSL."

There are various XSLT engines that can perform these transformations. See the
comprehensive list at:
http://www.xslt.com/xslt_tools_engines.htm
Comments and alternative answers

Try using Visual XML Transformation tool called Al...


Author: MANOJ KUMAR (http://www.jguru.com/guru/viewbio.jsp?EID=313370),
Jan 29, 2001
Try using Visual XML Transformation tool called Alphaworks from IBM.

How can I embed Java code in XSL? I want to call a Java object from my
XSL file. This object will return HTML code. This HTML code will then become
part of my XSL before it goes through XSLT. Is this possible or am I asking
for too much?
Location: http://www.jguru.com/faq/view.jsp?EID=311022
Created: Jan 24, 2001 Modified: 2001-01-24 11:24:28.481
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by shiva vithal
(http://www.jguru.com/guru/viewbio.jsp?EID=55743

Yes, it can be done. But is heavily dependent on the XSLT Engine. For example in
Xalan (open source XSLT Engine from Apache) you can use Java or JavaScript in your
XSL's.

http://xml.apache.org/xalan/extensions.html

I am retreiving XML resources from the net and transforming them locally
via XSL with Apache Xalan. Whenever an XML file contains a DOCTYPE
declaration, the parser insists on looking up the remote DOCTYPE URL. How
can I avoid that?
Location: http://www.jguru.com/faq/view.jsp?EID=311028
Created: Jan 24, 2001 Modified: 2001-01-24 11:22:55.72
Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011)
Question originally posed by Christian Sell
(http://www.jguru.com/guru/viewbio.jsp?EID=219526

You need to set a custom EntityResolver as documented in:


http://xml.apache.org/xalan-j/apidocs/org/xml/sax/EntityResolver.html

How can an XML file to be internationalized? Instead of using the


.properties file, I need to use XML and that needs to be internationalized.
Location: http://www.jguru.com/faq/view.jsp?EID=312293
Created: Jan 25, 2001 Modified: 2001-01-26 08:34:16.114
Author: Corneanu Dan (http://www.jguru.com/guru/viewbio.jsp?EID=41082)
Question originally posed by Lalu Haridas
(http://www.jguru.com/guru/viewbio.jsp?EID=29604
Take a look at "Translation Memory eXchange".

The example code which I have working reads XML from a File using:

InputSource input = Resolver.createInputSource(myFile);


then goes on to create an XmlDocument based on this input, eg :
XmlDocument doc = XmlDocument.createXmlDocument(input,true);
How do I parse an XML document that is coming over a socket without
writing it to a file first?
Location: http://www.jguru.com/faq/view.jsp?EID=312297
Created: Jan 25, 2001 Modified: 2001-01-26 08:35:04.334
Author: Corneanu Dan (http://www.jguru.com/guru/viewbio.jsp?EID=41082)
Question originally posed by Lawrence Meakins
(http://www.jguru.com/guru/viewbio.jsp?EID=290889
You can use the socket's input stream(socket.getInputStream()) to create your input
source.
Comments and alternative answers

Multiple XML Documents from the same socket?


Author: Jeremy Hare (http://www.jguru.com/guru/viewbio.jsp?EID=427622), May
24, 2001
I am also trying to read XML off a socket, but more than one document comes
through on the same socket. My question is how does the parser know when one
document is finished so that it will return the Document and start parsing the next
one?

Re: Multiple XML Documents from the same socket?


Author: Patrick Armstrong
(http://www.jguru.com/guru/viewbio.jsp?EID=447905), Jul 6, 2001
I was able to set two flags to ignore errors when I wasn't between the tags of a root
element. When I get to terminating root element I call parser.reset(). Works just
fine with many huge files over a socket.

SAX and Sockets


Author: Patrick Armstrong (http://www.jguru.com/guru/viewbio.jsp?EID=447905),
Jul 5, 2001
I had a problem getting the parser to understand when to quit parsing. I couldn't find
any way to send an EOF that the parser understood. I finally resorted to ignoring any
errors that occurred after I found the terminating root element tag. Don't know what
someone would do to restart the parser on new input over an existing socket.

Re: DOM and Sockets


Author: Rohit Joshi (http://www.jguru.com/guru/viewbio.jsp?EID=1121415), Nov
2, 2003
I have to parse XML Document form Client end to Server End via Java Socket
Programming. So initially i generate the document, But now as i tried to print this
document using System.out.println(document.toString()); it showing
[document=null]; I want to catch and display the created document at client end
which can be parsed through Socket from Server end.

How do I indicate to the Xeres parser the location of my XML schema? Do I


need to provide the location of my XML schema inside the XML file?
Location: http://www.jguru.com/faq/view.jsp?EID=313369
Created: Jan 26, 2001 Modified: 2001-01-29 17:57:49.136
Author: Shuchi Agarwal (http://www.jguru.com/guru/viewbio.jsp?EID=30635)
Question originally posed by Ganesh Balaji
(http://www.jguru.com/guru/viewbio.jsp?EID=308639

If you have built a schema with the name say as contact.xsd and with a root
element of contactdb then inside the xml file you need to

<contactdb xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="contact.xsd">

or if it is a relative URL

<xmlns:xsi ="http://www.w3.org/1999/XMLSchema/instance"
xsi:schemaLocation= "http://www.xmls.com/contact/contact.xsd">

where the url is the relative path or the document root of a web server.The schema
processor should also be in the classpath of your JVM/servlet engine etc.

Comments and alternative answers

validating xml against schema


Author: Srinivas Hanumaiahgari
(http://www.jguru.com/guru/viewbio.jsp?EID=511541), Oct 4, 2001

hi Shuchi,

I was desparately going thru resources to solve my prob and happened to find u. I am
facing probs in validating xml document against schema.

I think u can help me with my problem.

I got xml file and schema (which is tested against couple of schema validators)..but
having difficulties in implementing it.

Using xerces 1.4.3 (SAX) which supports Schema implementation.

XMLFILE:

<?xml version='1.0' encoding='utf-8'?> <SupplierInvoice


xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNameSpaceSchemaLocation="ibmSchema.xsd">

SCHEMA:

<?xml version="1.0" encoding="utf-8"?> <xsi:schema


xmlns:xsi="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

CODE SAMPLE:

public Vector CheckFile(String xmlFilePath)

Vector vector = null;

xmlFilePath= "F:\\Raptor\\Development\\xml\\SupplierInvoice.xml";

org.apache.xerces.parsers.SAXParser parser = new


org.apache.xerces.parsers.SAXParser();

handler = new ValHandler();

parser.setDocumentHandler(handler);

parser.setErrorHandler(handler);

try {

//this succesfully validates xml against dtd

parser.setFeature( "http://xml.org/sax/features/validation", true);

//this is supposed to validate schema

parser.setFeature("http://apache.org/xml/features/validation/schema",true);

parser.setFeature("http://apache.org/xml/features/validation/schema-full-
checking",true);

parser.parse(xmlFilePath);

the basic prob is not with validating but program doesnt pick up my .xsd file at all. if
u have any clue, plz give me feedback at the earliest possible as this checked my
progress with the project.

thanx in advance,

srinivas

Re: validating xml against schema


Author: Sarath Chithirala
(http://www.jguru.com/guru/viewbio.jsp?EID=1021679), Nov 5, 2002
Hi, It might help you. There is something called Entity Resolver. With this, you
can mention the path of your xsd/dtd files. 1) saxBuilder.setEntityResolver(new
CustomResolver()); public static class CustomResolver implements
EntityResolver { public InputSource resolveEntity(String publicId, String
systemId) { try { if (systemId != null) { // return a special input source //You
specify the schema location whereever your xsd/dtd files are located. But the
systemId is the dtd that is mentioned //in the xml file/xml string String filePath =
schemaLocation + systemId; System.out.println("The abs path " + new
File(filePath).getAbsoluteFile()); System.out.println("The abs path " + filePath);
FileInputStream fis = new FileInputStream(new File(filePath)); return new
InputSource(fis); } else { // use the default behaviour return null; } } catch
(Exception e) { return null; } } }

How does one install the jdom-b5 winzip file from www.jdom.org?
Location: http://www.jguru.com/faq/view.jsp?EID=314605
Created: Jan 28, 2001 Modified: 2001-01-29 18:01:58.926
Author: Chandra Patni (http://www.jguru.com/guru/viewbio.jsp?EID=33585)
Question originally posed by jarsha vardhan avvari
(http://www.jguru.com/guru/viewbio.jsp?EID=312987

If you are running JDK1.2 or higher versions, you can simply unzip the zip file and
copy the jdom.jar into your JAVA_HOME/jre/lib/ext dir. If you are using JDK1.1 then
you need to copy 1.1 version of jar file and collections.jar file to
JAVA_HOME/jre/lib/ext dir. Also you need to have xerces parser file into your
classpath. You can simply also copy the jar xerces jar file(s) in
JAVA_HOME/jre/lib/ext dir. If you don't have access to ext dir then simply include
these jar files into your classpath.

I've got two files, an XML and XSL file. The XML file contains my data and
the XSL file contains the layout of the HTML. How do I transform these files
to produce the HTML (as specified in my XSL). I did these in MS
environment using .asp so I'll like to know how to do the same in Java
either using .jsp or servlet or anything.
Location: http://www.jguru.com/faq/view.jsp?EID=314892
Created: Jan 29, 2001 Modified: 2001-01-29 18:00:08.398
Author: KIRAN KUMAR (http://www.jguru.com/guru/viewbio.jsp?EID=314877)
Question originally posed by Jukha Maseko
(http://www.jguru.com/guru/viewbio.jsp?EID=311955

For converting the XML to HTML using XSL, you can make use of the Xalan XSLT
engine.
Comments and alternative answers

You can make use of the following example for your...


Author: krishnakanth Rengarajan (http://www.jguru.com/guru/viewbio.jsp?EID=315782), Jan 29, 2001
You can make use of the following example for your question used with Xalan in JSP. If Test.xml,Test.xsl
<HTML>
<HEAD>
<%@ page language="java" %>
<%@ page
import="java.io.*,org.xml.sax.SAXException,org.apache.xalan.xslt.XSLTProcessorFactory
%>
<%! public java.io.OutputStream parse()
throws java.io.IOException,
java.net.MalformedURLException,
org.xml.sax.SAXException
{
java.io.ByteArrayOutputStream b=new java.io.ByteArrayOutputStream();
XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
processor.process(new XSLTInputSource("Test.xml"),
new XSLTInputSource("Test.xsl"),
new XSLTResultTarget(b));
return b;
}%>
</HEAD>
<BODY>
<%=parse() %>
<H1>
</H1>
</BODY>
</HTML>

Re: You can make use of the following example for your...
Author: Foo Shi Hao (http://www.jguru.com/guru/viewbio.jsp?EID=436481), Jul
23, 2001
Hi, your code is to display the XML data formatted by XSL right? And Xalan is
used to parse them? I tried out the exact code, changing the XML and XSL to my
own filenames. But there are errors... I just recently installed Xalan-Java 2. And i
use tomcat 3.2.2 Can you help? <<Autoexec.bat>> SET
CATALINA_HOME=C:\jakarta-tomcat-3.2.2 SET JAVA_HOME=C:\jdk1.3.1
SET PATH=C:\PROGRA~1\ULTRAE~1;c:\jdk1.3.1\bin;%PATH% SET
CLASSPATH=C:\jdk1.3.1\lib;c:\xalan-j_2_1_0\bin;C:\jdk1.3.1\jre\lib\ext
<<Errors occurred>> Error: 500 Internal Servlet Error:
javax.servlet.ServletException: SAX Exception Root cause:
javax.xml.transform.TransformerException: SAX Exception
Re: Re: You can make use of the following example for your...
Author: Kumar Sachwani
(http://www.jguru.com/guru/viewbio.jsp?EID=523774), Oct 18, 2001
HI
If you have found the correct solution then please please please tell me
i am facing similar problem
Bye
Kumar

Re: Re: Re: You can make use of the following example for your...
Author: Kumar Sachwani
(http://www.jguru.com/guru/viewbio.jsp?EID=523774), Oct 18, 2001
hi all
i finally found the solution
problem is in Tomcat version 3.2.1 and 3.2.2 the problem is with jaxp.jar
edit tomcat.bat and change the classpath settings to
set CLASSPATH=%CP%;%CLASSPATH%
to
set CLASSPATH=%CLASSPATH%;%CP%
now run the tomcat.bat file again ans start tomcat

vola your code works !!!!!!!!!


Bye
Kumar

I am using Tomcat v 4.0


Author: Bing Geeng
(http://www.jguru.com/guru/viewbio.jsp?EID=719536), Jan 14, 2002
And I had exactly same problem above. However, I could not find
tomcat.bat and jaxp.jar anywhere in my Directory folder, could you help?
thanks Bing

How can I embed a space in text in an XSL? Using "& nbsp;" treats it as an
entity.
Location: http://www.jguru.com/faq/view.jsp?EID=315231
Created: Jan 29, 2001 Modified: 2001-01-29 17:52:38.424
Author: Renato Michielin (http://www.jguru.com/guru/viewbio.jsp?EID=255654)
Question originally posed by Shiv Dutt
(http://www.jguru.com/guru/viewbio.jsp?EID=286598

Tell the XSLT processor not escape the symbol &. Put the escape sequence for the
symbol, "& amp;", in a xsl:text element, and set the disable-output-escaping
attribute to yes.
<xsl:text disable-output-escaping="yes">
& amp;nbsp;</xsl:text>
Comments and alternative answers
If you want some white space in the output tree, e...
Author: Thomas BROYER (http://www.jguru.com/guru/viewbio.jsp?EID=319084),
Feb 2, 2001

If you want some white space in the output tree, enclose it in an xsl:text element or
some element with xml:space="preserve".

<xsl:text> </xsl:text>

There is no clean way of outputting an entity reference (xsl:text with disable-output-


escaping isn't clean)

References
XSLT Recommendation, especially the Whitespace stripping section

Much easier is to use & #160; instead of &...


Author: Michael Rohde (http://www.jguru.com/guru/viewbio.jsp?EID=320107), Feb
3, 2001
Much easier is to use & #160; instead of & nbsp;

We have developed an application that uses the DOMBuilder class to


generate a JDOM document from a DOM document. The application works
very good on NT/Weblogic Server but in Solaris/TomCat returns the
message:

java.lang.NoSuchMethodError
at org.jdom.input.DOMBuilder.buildTree(DOMBuilder.java:416)
We are almost sure that this could be a configuration problem. What do you
think about?
Location: http://www.jguru.com/faq/view.jsp?EID=315879
Created: Jan 30, 2001 Modified: 2001-02-01 05:57:29.931
Author: Chandra Patni (http://www.jguru.com/guru/viewbio.jsp?EID=33585)
Question originally posed by Isaac Lopez
(http://www.jguru.com/guru/viewbio.jsp?EID=266468
This error is encountered because of incompatiable version of XML jar files. You
might have some other XML jar file (say xml.jar shipped with tomcat) either in your
classpath or in JAVA_HOME/jre/lib/ext dir when you are running your application in
Solaris.

You need to make sure that you don't have some old version of XML jar files in your
classpath or in ext dir. Replace such files by xerces.jar file and you should not get
this error. Note that the jar files which are dropped into ext dir are searched before
the jar files in classpath.

Also please refer to similar problem while installing cocoon in tomcat setup.

What is RELAX schema language?


Location: http://www.jguru.com/faq/view.jsp?EID=317814
Created: Feb 1, 2001 Modified: 2001-02-01 05:46:27.701
Author: Chandra Patni (http://www.jguru.com/guru/viewbio.jsp?EID=33585)

RELAX stands for REgular LAnguage description for XML. It can be thought of as a
namspace aware DTD written in xml syntax. RELAX also borrows datatypes from XML
Schema Part 2. For more info, please refer to RELAX Tutorial and FAQ

How can I use XML in EJB for data storage and management?
Location: http://www.jguru.com/faq/view.jsp?EID=317835
Created: Feb 1, 2001 Modified: 2001-02-01 05:49:44.515
Author: KIRAN KUMAR (http://www.jguru.com/guru/viewbio.jsp?EID=314877)
Question originally posed by Risheng Lin
(http://www.jguru.com/guru/viewbio.jsp?EID=46809

Using an XML repository like "TAMINO" you can store all your XML files and query
them through your EJBs.

How can I embed XML code in my JSP? Where can I give a link to my XSL
file?
Location: http://www.jguru.com/faq/view.jsp?EID=318016
Created: Feb 1, 2001 Modified: 2001-02-01 18:00:40.771
Author: Renato Michielin (http://www.jguru.com/guru/viewbio.jsp?EID=255654)
Question originally posed by Gopi chand
(http://www.jguru.com/guru/viewbio.jsp?EID=259984

To generate XML set the contentType of the page to text/xml:


<?xml version="1.0" ?>

<%@ page contentType="text/xml" %>

.........xml............

(Put the page directive on the second line, otherwise the compiler leaves a blank
first-line and this may cause an error when XML document is parsed).
You may link to an XSL style sheet from the page inserting an xml-stylesheet
processing-instruction after the xml declaration:
<?xml version="1.0" ?>

<?xml-stylesheet type="text/xsl" href="myxsl.xsl" ?>

<%@ page contentType="text/xml" %>

.........xml............

To code XML dynamically, add a bean to the page and call a method to populate an
element:
<jsp:useBean id="mybean" class="mypackage.myclass" />

<mytag><%= mybean.getSomeValue() %></mytag>

I have a set of XML documents. I want to search these documents.


How can I do efficient indexing on these XML documents? Does anybody
have any indexing mechanism? Any and all information, documents, tools,
free or commercial would be appreciated.
Location: http://www.jguru.com/faq/view.jsp?EID=318947
Created: Feb 1, 2001 Modified: 2001-02-03 09:01:06.522
Author: KIRAN KUMAR (http://www.jguru.com/guru/viewbio.jsp?EID=314877)
Question originally posed by haresh gujarathi
(http://www.jguru.com/guru/viewbio.jsp?EID=224766

You can make use of commercially available repositories like tamino and bladerunner.
You can deploy all your XML files to that and make searches in the repository.
Comments and alternative answers

Look at XSet
Author: Ramkumar Natarajan (http://www.jguru.com/guru/viewbio.jsp?EID=52141),
May 28, 2001
http://www.cs.berkeley.edu/~ravenben/xset/ XSet is a fast in-memory (backed up on
disk) database for storing and querying XML documents which supports limited
ACID semantics. I've used it and found it to be extremely fast and very usable.

What is the difference between the XPath and XPointers?


Location: http://www.jguru.com/faq/view.jsp?EID=319087
Created: Feb 2, 2001 Modified: 2001-02-12 07:31:17.705
Author: Thomas BROYER (http://www.jguru.com/guru/viewbio.jsp?EID=319084)
Question originally posed by java sai
(http://www.jguru.com/guru/viewbio.jsp?EID=313006

XPath

The XPath language allows you to retrieve nodes from an XML tree. It can be seen as
the SQL for XML. (It has in fact many limitations which prevent such an analogy and
have lead the creation of XML Query).

XPath is somewhat "abstract". It's designed to be used within contexts. These


contexts are for the moment XSLT and XPointer.

XPointer

XPointer allows retrieving of data from other documents. Its goal is to be used in URI
fragment identifiers.

It extends XPath with the selection of regions (ranges) or points (whereas XPath only
selects nodes).

It also has other syntaxes, not based on XPath.

In brief :
• XPath isn't self-sufficient. Some application has to provide an evaluation
context.
• XPointer makes use of and extends XPath while providing an evaluation
context.

How can I link two XML Files (e.g. using MS IE 5.0)?


Location: http://www.jguru.com/faq/view.jsp?EID=319122
Created: Feb 2, 2001 Modified: 2001-02-03 08:44:35.599
Author: Thomas BROYER (http://www.jguru.com/guru/viewbio.jsp?EID=319084)
Question originally posed by Rainer Midderhoff
(http://www.jguru.com/guru/viewbio.jsp?EID=10286

To make links, you can use XLink.

Currently only Mozilla, Amaya and MSIE6 have XLink support (and only for simple
links).

Another solution is to transform your XML to (X)HTML using XSLT.

Comments and alternative answers

How do you use XSLT to link two or more XML files?


Author: Calvin Hill (http://www.jguru.com/guru/viewbio.jsp?EID=787450), Mar 15,
2002
I know it can be done but cannot find any recources on how to do it. Thanks, Calvin

Is it possible to include one XML document within another XML document?


Please give some samples.
Location: http://www.jguru.com/faq/view.jsp?EID=319148
Created: Feb 2, 2001 Modified: 2001-02-03 08:45:27.814
Author: Thomas BROYER (http://www.jguru.com/guru/viewbio.jsp?EID=319084)
Question originally posed by Asit Bhattacharya
(http://www.jguru.com/guru/viewbio.jsp?EID=132138

Many possibilities:

• Using external parsed entities:


• <!DOCTYPE wrapper [
• <!ENTITY wrapped SYSTEM "wrapped.xml">
• ]>
• <wrapper>
• &wrapped;
• </wrapper>
• Using XInclude:
• <wrapper xmlns:xincl="http://www.w3.org/1999/XML/xinclude">
• <xincl:include href="wrapped.xml"/>
• </wrapper>

You'll need an XInclude processor.


• Using an XSLT stylesheet and the document() function.
• Using an XLink with xlink:show="embed".
• etc.

How can an XML fragment be returned with XSL?

XML:

<?xml version="1.0"?>
<Numbers>
<Number>
<Digit>1</Digit>
<Name>One</Name>
<Other>First</Other>
</Number>
</Numbers>
Desired output:
<Number>
<Digit>1</Digit>
<Name>One</Name>
<Other>First</Other>
</Number>

In other words, the desired output is a partial XML fragment. I have


attempted to use this XML, but it does not work:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version ="1.0">
<xsl:output method="xml"/>
<xsl:template match="/Numbers">
<xsl:value-of select="Number" disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>

Location: http://www.jguru.com/faq/view.jsp?EID=319171
Created: Feb 2, 2001 Modified: 2001-02-12 07:27:47.49
Author: Thomas BROYER (http://www.jguru.com/guru/viewbio.jsp?EID=319084)
Question originally posed by Stefan Lecho
(http://www.jguru.com/guru/viewbio.jsp?EID=32509
What you need is xsl:copy-of.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Numbers">
<xsl:copy-of select="Number[1]">
</xsl:template>
</xsl:stylesheet>

I am using Xalan 1.2.1 and Sun JDK 1.2.2_06. I am trying to use an XSL
stylesheet to transform one XML document into an external XML API
request. I am trying to dynamically generate the DocType element so that it
builds the system URL using an element contained in the document I am
transforming. How do I do this? I tried:
<xsl:output doctype-
system="http://{/KPRequest/AppURL}/xmldocs/xmldtd/CommonRequest.dtd"
doctype-public="-//KP2000//DTD - KP Common IDL API XML Request//EN"/>
and this:
<xsl:output doctype-
system="http://{/KPRequest/AppURL[text()]}/xmldocs/xmldtd/CommonRequest
.dtd" doctype-public="-//KP2000//DTD - KP Common IDL API XML
Request//EN"/>
And in both cases, the Xalan XSL processor did not alter the attempts to
reference the AppURL element at all. It just output the select and the curly-
braces right into the DOCTYPE element. Is there any way to do this? Is this
limitation in a spec somewhere that I haven't read?
Location: http://www.jguru.com/faq/view.jsp?EID=319173
Created: Feb 2, 2001 Modified: 2001-02-12 07:44:51.857
Author: Thomas BROYER (http://www.jguru.com/guru/viewbio.jsp?EID=319084)
Question originally posed by Jonathan Morgan
(http://www.jguru.com/guru/viewbio.jsp?EID=281937
This is a limitation of XSLT1.0. Top-level elements cannot have attribute-value-
templates. XSLT1.1 will allow AVTs in xsl:output. With XSLT1.0, you have to modify
the output tree after the transformation is done.

Is there a way to generate the expressions for select attribute


"dynamically"? For example, consider the following simple document:

<?xml version="1.0"?>
<doc>

<data>
<person>
<name>John Smith</name>
<age>26</age>
<phone>(416)123-2345</phone>
<city>Toronto</city>
</person>
<person>
<name>Mary Lynn</name>
<age>16</age>
<phone>(416)133-2333</phone>
<city>New York</city>
</person>
<person>
<name>Don John</name>
<age>16</age>
<phone>(456)143-2225</phone>
<city>San Francisco</city>
</person>
<data>

<format>
<field>age</field>
<field>name</field>
</format>
</doc>
How can I write a stylesheet which will output the information from the
<data> section according to the format (i.e. field selection and order)
specified in <format> section, i.e. for the example above the output would
be:
26
John Smith
16
Mary Lynn
16
Don John
What I was trying to do is to run through the <data> section with xsl:for-
each and for each <person> entry run through the <format> section
selecting the elements from <person> with the names specified in
<format> section, i.e.:
<xsl:for-each select="/doc/data/person">
<xsl:for-each select="/doc/data/format/field">

<xsl:value-of select="Now, how do i tell it to select


the element from /doc/data/person
with the name specified in
/doc/data/format/field ????>
</xsl:for-each>
</xsl:for-each>
I tried to use variables, but if I do this:
<xsl:for-each select="/doc/data/format/field">
<xsl:variable name="field-name" select="."/>
<xsl:value-of select="$field-name"/>
I get the field names printed out, and if I do this:
select="/doc/data/person/$field-name" the processor doesn't like the
select syntax. Is this possible at all to do? Or is there another way to
approach this problem?
Location: http://www.jguru.com/faq/view.jsp?EID=319182
Created: Feb 2, 2001 Modified: 2001-02-13 07:26:25.863
Author: Thomas BROYER (http://www.jguru.com/guru/viewbio.jsp?EID=319084)
Question originally posed by Anton Belov
(http://www.jguru.com/guru/viewbio.jsp?EID=75477
First, always prefer templates to for-each loops when possible. Also, be very careful
with XPath object types and XPath context.
<xsl:template match="person">
<xsl:variable name="person" select="."/>
<xsl:for-each select="/doc/format/field">
<variable name="field-name">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:value-of select="$person/*[name() = $field-name]"/>
</xsl:for-each>
<xsl:template>
Note you can use current() instead of $field-name.

I am using the Apache xerces-classes for generating XML documents. How


can I add the location of the DTD, which is located on a server? I want the
generated document to have the second line like

"Doctype klausur System "klausur.dtd"


Location: http://www.jguru.com/faq/view.jsp?EID=320110
Created: Feb 3, 2001 Modified: 2001-02-12 07:26:54.49
Author: Michael Rohde (http://www.jguru.com/guru/viewbio.jsp?EID=320107)
Question originally posed by oliver toma
(http://www.jguru.com/guru/viewbio.jsp?EID=109998
Just add a Doctype node to your Document object. The Doctype provides methods to
set the filename and whether it's SYSTEM or PUBLIC.
Comments and alternative answers

Can you give an example on how to do this?


Author: John Hankermeyer (http://www.jguru.com/guru/viewbio.jsp?EID=1056665),
Mar 4, 2003
The JAXP API says:
"The DOM Level 2 doesn't support editing DocumentType nodes. "

It appears that the API won't let you just add a DocumentType node? Any ideas or
work arounds for this?
thanks, John

How can I validate an XML file against a DTD in Java?


Location: http://www.jguru.com/faq/view.jsp?EID=320115
Created: Feb 3, 2001 Modified: 2001-02-12 07:35:57.732
Author: Michael Rohde (http://www.jguru.com/guru/viewbio.jsp?EID=320107)
Question originally posed by Asha Balasubramanyan
(http://www.jguru.com/guru/viewbio.jsp?EID=268995

Most XML parsers support validation. In Apache Xerces, for example, the property for
validation is set to true by default. Every XML file which contains a "doctype"
reference to a DTD gets validated.
Comments and alternative answers

What happens when a validating parser encounters an...


Author: Chris Brown (http://www.jguru.com/guru/viewbio.jsp?EID=317273), Feb 13,
2001
What happens when a validating parser encounters an invalid document? I tried a
simple example, using Apache Xerces, with an embedded DTD: despite deliberate
errors in the document (non-conformance to DTD), nothing "went wrong": no
exception, nothing. What should happen? How can I detect problems with the DTD?

In Xerces, an error handler must be defined for va...


Author: David Smith (http://www.jguru.com/guru/viewbio.jsp?EID=105814), Feb 15,
2001
In Xerces, an error handler must be defined for validation to occur. Have a class
implement org.xml.sax.ErrorHandler and use the setErrorHandler method of the
parser to set the error handler up. Your ErrorHandler class will be called when the
parser encounters errors. This should work with both the SAX and DOM parsers.
Re: In Xerces, an error handler must be defined for va...
Author: Ratan Korlam (http://www.jguru.com/guru/viewbio.jsp?EID=469559),
Aug 8, 2001
I did the same and could not get any exceptions thrown when I parse invalid-xml
document. I had my code as : String DEFAULT_PARSER_NAME =
"org.apache.xerces.parsers.SAXParser"; Parser parser =
ParserFactory.makeParser(DEFAULT_PARSER_NAME);
((XMLReader)parser).setFeature( "http://xml.org/sax/features/validation", true);
parser.setDocumentHandler(rootHandler); parser.setDTDHandler(rootHandler);
parser.setErrorHandler(rootHandler); Am I on the right track? Please suggest.
Thanks Ratan Korlam

I'm using a typical model 2 MVC architecture for my servlet-based


applications. I want to use an XML file to contain the associations between
components (e.g., servlets, processing classes, beans, and JSPs) but several
associates have argued that this information should be placed in the
database. I feel it belongs with the application since it is configuration
information. Which is the better approach?
Location: http://www.jguru.com/faq/view.jsp?EID=320283
Created: Feb 3, 2001 Modified: 2001-02-13 07:37:27.908
Author: Subrahmanyam Allamaraju
(http://www.jguru.com/guru/viewbio.jsp?EID=265492) Question originally posed by
Mark Will (http://www.jguru.com/guru/viewbio.jsp?EID=304891

Your solution is more appropriate under the following conditions:

1. The data is static, and is not being concurrently updated by other applications.

2. You would replicate (or make it accessible) for all instances in case of clustering.

I presume that this is the case, and so, storing it outside a database makes your
application more flexible.

On the otherhand, if this data is shared (read and written) across multiple
applications, and therefore is subjected to transactions, you should consider a
database.

How to convert a DOM Document object into an XML file using JAXP?
Location: http://www.jguru.com/faq/view.jsp?EID=320363
Created: Feb 4, 2001 Modified: 2001-02-12 07:39:43.438
Author: Chandra Patni (http://www.jguru.com/guru/viewbio.jsp?EID=33585)
Question originally posed by Tarun Chopra
(http://www.jguru.com/guru/viewbio.jsp?EID=315456

Using JAXP1.1 (See JSR 0063) javax.xml.transform.Transformer class can be


used to convert a DOM Document object into an XML file. For example,
writeToFile() method in the following snippet of code writes a Document object to
a specified file. Note that javax.xml.transform is a new package defined in
JAXP1.1.
import java.io.*;
import org.w3c.dom.Document;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

.......

public void writeToFile(Document doc, String fileNameToWrite) throws


Exception {
DOMSource domSource = new DOMSource(doc);
PrintWriter out = new PrintWriter(new BufferedWriter(new
FileWriter(fileNameToWrite)));
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, streamResult);
}
Comments and alternative answers

A less desirable way involves downcasting to Sun's...


Author: Otto Perdeck (http://www.jguru.com/guru/viewbio.jsp?EID=325105), Feb 13,
2001
A less desirable way involves downcasting to Sun's XmlDocument class. But it works
with JAXP 1.01, unless Chandra's method, that only works with JAXP 1.1:
Document doc;

...

XmlDocument tdoc = (XmlDocument)doc;


tdoc.write(new PrintWriter(System.out));
XmlDocument is in the package com.sun.xml.tree

Where can I download the com.sun.xml.tree package?...


Author: Mehdi Elaoufir (http://www.jguru.com/guru/viewbio.jsp?EID=328822), Feb
14, 2001
Where can I download the com.sun.xml.tree package?

output in one line


Author: Christian Dürken (http://www.jguru.com/guru/viewbio.jsp?EID=305852),
Jun 11, 2001
as i used the transformer class and the output was in one line. how can it be formatted
in a nicer looking way?

it looked like this:


<tag0><tag1>text</tag1><tag0>

..and it should look like this


<tag0>
. <tag1>text</tag1>
</tag0>

Re: output in one line


Author: captain strobe (http://www.jguru.com/guru/viewbio.jsp?EID=496245),
Sep 13, 2001
As a clue, when you read in the second example into a dom, you have Text
Elements in beween your *real* elements. I guess you would have to add your
own text elements with the indenting in. ;-(

rog

Re: output in one line


Author: Carmela * (http://www.jguru.com/guru/viewbio.jsp?EID=756506), Mar
13, 2002
This is what you need to do :

DOMSource domSource = new DOMSource(document);

String tempo = (String) entries.elementAt(1); p int i = tempo.lastIndexOf("\\");

String name = tempo.substring(i+1);

tempo = tempo + "\\JB" + name +".jpx";

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(tempo)));

StreamResult streamResult = new StreamResult(out);

TransformerFactory tf = TransformerFactory.newInstance();

Transformer transformer = tf.newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT,"yes"); // so that the output


XML file is not all in one line only

transformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount",
"4" );

transformer.transform(domSource, streamResult);
COnverting DOM Document Object into XML file removes the DTD
Author: Ashit Shetty (http://www.jguru.com/guru/viewbio.jsp?EID=749998), Feb 6,
2002
I have a wellformed XML with a DTD written in it . I am parsing this XML and
manipulating some information in it and by using the
javax.xml.transform.Transformer class , trying to write to another XML file. The
problem is even though the XML is written properly my original DTD is lost(not seen
in the new XML file) . Is there anyway of retaining the DTD ?

My copy of your code


Author: Carmela * (http://www.jguru.com/guru/viewbio.jsp?EID=756506), Feb 12,
2002
import java.io.*; import org.w3c.dom.Document; import
javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import
javax.xml.transform.stream.StreamResult; class class1 { void writeToFile(Document
doc, String fileNameToWrite) throws Exception { DOMSource domSource = new
DOMSource(doc); PrintWriter out = new PrintWriter(new BufferedWriter(new
FileWriter(fileNameToWrite))); StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer
= tf.newTransformer(); transformer.transform(domSource, streamResult); } } class ex
{ public static void main(String a[]) { class1 object1 = new class1();
object1.writeToFile(); } }

How to setup Cocoon in Tomcat?


Location: http://www.jguru.com/faq/view.jsp?EID=324703
Created: Feb 9, 2001 Modified: 2001-02-11 11:00:44.659
Author: Rob Blanco (http://www.jguru.com/guru/viewbio.jsp?EID=69158) Question
originally posed by Eric Chow (http://www.jguru.com/guru/viewbio.jsp?EID=25181

It's all there: http://xml.apache.org/cocoon/install.html#tomcat

How can I pass XML data from one web server to another web server and
get the reponse back from it?
Location: http://www.jguru.com/faq/view.jsp?EID=329362
Created: Feb 14, 2001 Modified: 2001-02-28 10:55:51.484
Author: Ravi Van (http://www.jguru.com/guru/viewbio.jsp?EID=329336) Question
originally posed by Madhu Menon
(http://www.jguru.com/guru/viewbio.jsp?EID=29982

Sure you can! Each of the web servers should be capable of understanding and
processing XML and in turn generating XML.

You can achieve this as follows: Set the mime-type in your webserver configuration
file so that the request is routed to the appropriate application. From then on, it is
the responsibility of that application to understand/consume/generate XML.

Comments and alternative answers


Create a java.net.URL, open the connection and cast...
Author: Nathan Phelps (http://www.jguru.com/guru/viewbio.jsp?EID=3382), Feb 15,
2001
Create a java.net.URL, open the connection and cast it to a
java.net.HttpURLConnection. Set doOutput to true and the method to post. Then open
the OutputStream and you can simply serialize the org.w3c.dom.Document to the
output. Create a general receive servlet/JSP/ASP that reads the posted request on the
web server, evaluates it however you wish, and responds to the request. Verify that
everything went as planned by either checking the response status or opening an
InputStream and reading the response document.

Re: Create a java.net.URL, open the connection and cast...


Author: Bala Paranj (http://www.jguru.com/guru/viewbio.jsp?EID=505521), Sep
27, 2001
Is it possible to use https to send and receive XML document ? If yes, what
changes do I need to make ?

How can I import nodes recursively from one document object to another
using Java's J.A.X.P ?
Location: http://www.jguru.com/faq/view.jsp?EID=330284
Created: Feb 15, 2001 Modified: 2001-02-16 07:35:38.554
Author: Brian Ewins (http://www.jguru.com/guru/viewbio.jsp?EID=301660)
Question originally posed by navin bhaskar
(http://www.jguru.com/guru/viewbio.jsp?EID=83923

You can't. JAXP is an API for getting hold of a document object or a parser. Beyond
that, you're into the DOM, SAX or JDOM APIs instead. The easiest approach is to use
JDOM, which has FAQ examples on how to reparent an element recursively. JDOM
does not have a Node abstraction. Neither does JAXP for that matter, its part of DOM.
If you are committed to using the DOM you need a DOM Level-2 capable parser. You
need to look up the documentation for importNode() . If it's 'deep' parameter is true
it will import recursively.
Comments and alternative answers

Using JAXP for importing nodes from one document to another


Author: Dinesh Ramachandran
(http://www.jguru.com/guru/viewbio.jsp?EID=383187), Mar 26, 2001
It is possible. You create a document object using DOM parser of JAXP. Then create
an empty document. You could then burrow into the first document using recursive
code, while creating new tags in the second document with the names of the tags you
have retrieved from the first document. all methods required for this work are
available in JAXP

Re: Using JAXP for importing nodes from one document to another
Author: João Montenegro (http://www.jguru.com/guru/viewbio.jsp?EID=794596),
May 15, 2002
What about Document.importNode() method?
João

Sun have had SAX and DOM APIs for XML Parsing for some time. Now, there
are a number of new standards, namely JAXM and JAXP, for XML messaging
and parsing respectively. What exactly do these new interface standards
achieve, and where do they fit with the APIs that Sun has already?
Location: http://www.jguru.com/faq/view.jsp?EID=330288
Created: Feb 15, 2001 Modified: 2001-02-16 07:39:42.271
Author: Brian Ewins (http://www.jguru.com/guru/viewbio.jsp?EID=301660)
Question originally posed by james murphy
(http://www.jguru.com/guru/viewbio.jsp?EID=278791

Sun never had SAX and DOM. They are xml.org and W3C parser specs, respectively.
JAXP is a layer above these which allows parsers to become 'pluggable'. SAX and
DOM left a couple of issues open which led to vendor-specific extensions, e.g. to get
the parser in the first place. If you work with JAXP you can switch parser without
changing your code, just replace the parser JAR. JAXM is not a standard yet, its a
working draft of the 'M Project'. It will standardise later when ebXML solidifies later
this year, and is meant to provide an API for reliable XML messaging for business
transactions. This is a different layer of abstraction from JAXP, SAX, and DOM (level
1), which are all about parsing. JAXM fits in with JAXP in the same way that your own
application would: you can switch parsers and JAXM will still work, because it uses
JAXP.

When should XML be used? I know how to create an XML file, a DTD and
parse data from an XML page. But I am still unable to find the right
application of XML in out site (an insurance company). We have Oracle 8i as
our database and use JSPs and EJBs 1.0. We have a set of questions asked
and based on that we quote a premium. We also have searches done on
data.
Location: http://www.jguru.com/faq/view.jsp?EID=330704
Created: Feb 15, 2001 Modified: 2001-02-16 07:38:17.769
Author: David Smith (http://www.jguru.com/guru/viewbio.jsp?EID=105814)
Question originally posed by Ashwin Chathuruthy
(http://www.jguru.com/guru/viewbio.jsp?EID=83111

Sometimes a tool just isn't appropriate. Just because you know how to do something
doesn't mean that you should.

I have found XML to be useful for the following things:

Generating web pages from applications - the application can output XML and pass it
through an XSL stylesheet to add all the pretty HTML eye-candy.

Creating "standardized" web pages/information that can then be formatted in


different ways easily using XSL

Moving data between different parts of the system


Configuration files

There are lots of other good uses for XML, I'm sure. Rather than looking for uses for
the new tool you've learned, you should be looking for solutions to the problems you
have.

Comments and alternative answers

There is a non-profit organization among the insurance...


Author: Arun Bharathan (http://www.jguru.com/guru/viewbio.jsp?EID=106958), Feb
18, 2001
There is a non-profit organization among the insurance Companies named ACORD
which develop standards in insurance industry. They have set of XML documents for
various functions in the insurance business. These XML and DTDs covers rating &
quoting for different lines of business. XMLS are used in the industry for exchange of
data between agencies, producers, carriers, rating bureaus, service providers, and
much more.

How does one store text from multiple natural languages, such as Japanese
and Chinese, in one XML file which has 'English' as the main language?
Location: http://www.jguru.com/faq/view.jsp?EID=330724
Created: Feb 15, 2001 Modified: 2001-02-16 07:37:10.584
Author: David Smith (http://www.jguru.com/guru/viewbio.jsp?EID=105814)
Question originally posed by rajesh vachepally
(http://www.jguru.com/guru/viewbio.jsp?EID=50666

XML's "natural" encoding is Unicode UTF-8. This means that you can mix characters
from different languages freely within an XML document. You will, however, need a
UTF-8 editor.

In a project I'm currently working on we mix English and Japanese text in the same
documents with English sections enclosed within <english> </english> tags and
Japanese within <japanese> </japanese> tags. The only reason we tag the
languages is so that we can choose which to display - it does not make any
difference to the XML parser. It is perfectly valid to mix any characters together while
using Unicode

It is also possible to encode XML documents using a character set other than UTF-8.
In Japan there are pre-existing character sets such as EUC and JIS which are in
common use. These character sets also encompass ASCII so it is easy to mix English
and Japanese, however if we needed a third language we could not do it within a
single document since XML only allows one encoding for the entire document. For
that reason, it's best to convert to Unicode as soon as you can. Using Java and
Xerces it is fairly easy to convert character sets into/out of Unicode.

Comments and alternative answers

You should use xml:lang to "tag" languages...


Author: Thomas BROYER (http://www.jguru.com/guru/viewbio.jsp?EID=319084),
Feb 16, 2001

• You should use xml:lang to "tag" languages instead of or in addition to some


tags that only have sense for you, not any application.
• To convert from one encoding to another, you can use the GNU recode tool. It
has been ported to many platforms and supports a wide range of encodings.

Re: You should use xml:lang to "tag" languages...


Author: Arunkumar Murthy
(http://www.jguru.com/guru/viewbio.jsp?EID=472111), Aug 30, 2001
Hello,
Can u spare some time to explain how to display characters in czech in the
browser.
I am reading an XML file which has data in Czech using javax.xml.parser's
document.parse method.
On tryin to write the data back to another file, it is corrupted.
Can you explain on how to solve this.?
Is it a limitation on jaxp/crimson parser or prob is in code somewhere
thanks

Can we make a database connection from an XML document? If so, please


explain the procedure.
Location: http://www.jguru.com/faq/view.jsp?EID=331803
Created: Feb 16, 2001 Modified: 2001-02-17 07:43:54.478
Author: Subrahmanyam Allamaraju
(http://www.jguru.com/guru/viewbio.jsp?EID=265492) Question originally posed by
telu srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=287694

This question does indicate that the author of this question considers a XML
document as a runtime entity.

Although APIs such as DOM provide runtime representions of documents, a


document is a still a document. At run time, the DOM is just a data structure without
any behavior. If the author wants to execute some logic (such as database logic)
while dealing with a document, he should do it in the code that is manipulating it.

Comments and alternative answers

RE: Can we make a database connection from a XML document?


Author: Carlos A. H. Landero
(http://www.jguru.com/guru/viewbio.jsp?EID=434853), Jun 6, 2001
Yes, It's possible to develop webapplications and their elements with XML.
There's a XML based markup language called AppML, Application Markup
Language, that allows you to describe Internet applications.
That is, you don't need any programming, just XML to describe your application, and
your application will be executed by Web services such as ASP's or JPS's.

For example, you can define the properties for a database connection and the query in
a XML file, and this XML file is passed as parameter to a jsp or asp that executes the
query, showing you the results in html.

Go to http://www.w3appml.com/ if you want to know what AppML is.

Every XML parser I've seen comes with the "standard" packages (such as
org.w3c.dom, org.xml.sax, javax.xml.parsers etc). The unusual and weird
thing is that each parser I've seen comes with a *different* version of those
packages.

I've checked out Xerces 1.3.0, JAXP 1.01 and IBM XML4J which comes with
VAJ 3.5.

How come there are many versions of a package? Where can I get the latest
versions of those packages?
Location: http://www.jguru.com/faq/view.jsp?EID=331808
Created: Feb 16, 2001 Modified: 2001-02-17 07:44:44.61
Author: Subrahmanyam Allamaraju
(http://www.jguru.com/guru/viewbio.jsp?EID=265492) Question originally posed by
Isaac Shabtay (http://www.jguru.com/guru/viewbio.jsp?EID=325037

This is because there are two sets of DOM and SAX APIs around - SAX 1 and SAX 2,
and DOM Level 1 and Level 2.

• Xerces 1.3 supports SAX 2 and DOM Level 2. So, you'll find those versions of
SAX and DOM APIs.
• JAXP 1.01 supports SAX 1 and DOM Level 1. So, you'll find the older versions
of SAX and DOM APIs.
• I'm not sure of what is includes in VAJ3.5. But it could be one of the above
two.

The ones shipped with Xerces 1.3 are the latest.

Comments and alternative answers

VAJ3.5 has the older one.


Author: Petr Gasparik (http://www.jguru.com/guru/viewbio.jsp?EID=327445), Feb
18, 2001
VAJ3.5 has the older one.
I think latest version should be found at following...
Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985), Feb
19, 2001
I think latest version should be found at following addresses:

DOM

SAX

JAXP

Is it possible to have more than one DTD in an XML file?


Location: http://www.jguru.com/faq/view.jsp?EID=331974
Created: Feb 16, 2001 Modified: 2001-02-17 07:45:50.666
Author: Thomas BROYER (http://www.jguru.com/guru/viewbio.jsp?EID=319084)
Question originally posed by king hw
(http://www.jguru.com/guru/viewbio.jsp?EID=132496

You cannot have more than one DOCTYPE declaration in an XML document, but one
document can "call" several DTD's using external parameter entities.

Example:

Let's say you have some document with a «svg:svg» root element and its associated
DTD, and another document with a «mml:math» root element and its associated
DTD.

Now you want to have a single document with, say, a «bar» root element with
allowed children any of «svg:svg» or «mml:math» and their subtrees as declared in
the two DTD's mentionned above.

The solution is to "import" the DTD's in the "bar DTD":

<!ELEMENT bar (svg:svg | mml:math)+>


<!ENTITY % svg SYSTEM "svg.dtd">
%svg;
<!ENTITY % mml SYSTEM "mml.svg">
%mml;

I wrote a servlet. It is sending XML data as response object. The XML file is
displayed fine. But now I want to add a style sheet to this XML file. For that
I copy the XSL file to public_html directory and in servlet I added this file to
the response object. Now whenever I request for this servlet the brower
produces an error

"This page is accessing information that is not under its control. This poses
a security risk. Do you what to continue?".
If I agree, it works fine. But I don't what that message before displaying.
How to eliminate that?
Location: http://www.jguru.com/faq/view.jsp?EID=332552
Created: Feb 18, 2001 Modified: 2001-02-20 20:28:03.884
Author: Arun Bharathan (http://www.jguru.com/guru/viewbio.jsp?EID=106958)
Question originally posed by Rama Arja
(http://www.jguru.com/guru/viewbio.jsp?EID=318080
It seems that you are trying to access your XSL through HTTP where as the request
for the XML was generated in HTTPS and a warning as such is generated by the
browser.

There are two ways to solve this. Move your XSL into a secure area. (ie, XSL should
be served through secure layer) Or do the transforms using a XSLT (like xalan) on
the server side itself and serve the generated page. (This works in all browsers and
does not depend on XML aware browsers.)
Comments and alternative answers

error : "This page is accessing information that is not under its control. This
poses a security risk. Do you what to continue?"
Author: Kevin McCoy (http://www.jguru.com/guru/viewbio.jsp?EID=797510), Mar
14, 2002
Actually, this message is caused by an Internet Explorer option, not HTTP/HTTPS.
Last time I saw it (a "yes" answer produced desired results) the suspected cause was
the Security / Miscellaneous / Access data across domains setting (I haven't yet
verified that this was the setting).

Re: error : "This page is accessing information that is not under its control.
This poses a security risk. Do you what to continue?"
Author: Carlos Camacho (http://www.jguru.com/guru/viewbio.jsp?EID=824608),
Apr 4, 2002

It seems Kevin is right. I was receiving this message constantly and now it's gone,
after I changed the Security / Miscellaneous / Access data across domains setting
to "enable" instead of "prompt".

Thanks.

Re: error : "This page is accessing information that is not under its control.
This poses a security risk. Do you what to continue?"
Author: Hans Hillewaert (http://www.jguru.com/guru/viewbio.jsp?EID=892348),
May 25, 2002

Thanks a lot for you suggestion. This really did the trick. I've been bothered with
that annoying popup for weeks, and now it's finally gone.

Great!!
How can I validate an XML file against a DTD without including the DTD file
in the XML file?
Location: http://www.jguru.com/faq/view.jsp?EID=332561
Created: Feb 18, 2001 Modified: 2001-02-20 20:13:18.488
Author: Arun Bharathan (http://www.jguru.com/guru/viewbio.jsp?EID=106958)
Question originally posed by Rama Arja
(http://www.jguru.com/guru/viewbio.jsp?EID=318080

As per the XML1.0 specification the DTD could be declared inside the XML, or
externally as a seperate URI or combination of both.

So you would have a processing Instruction inside XML as follows:

<!DOCTYPE myelement SYSTEM "http://www.myserver.com/dtds/myelement.dtd"


>
Read the XML1.0 Specification for more details.
Comments and alternative answers

Validating xml again dtd


Author: Amit Chauhan (http://www.jguru.com/guru/viewbio.jsp?EID=408110), Apr
23, 2001
What if I dont include dtd in the xml file and neither use DOCTYPE tag to point to an
external DTD, is there some api provided in Xerces parse to validate the xml against a
dtd, something like validate (xmlString, dtdFileLocation) ? Thanks Amit

Re: Validating xml again dtd


Author: Eusiri Arsekularatne
(http://www.jguru.com/guru/viewbio.jsp?EID=425084), May 20, 2001
Hi, Have you resolved this one?? I have come accross a similar problem and have
been playing with xerces API to get a workable solution. There are millions of
classes/methods but it appears something as basic as your/my problem is not
available!! It appears the answer is in the StringPool class which xerces uses to
store dtd names,etc. I have tried sub classing the DOMParser and then overriding
method startDTD to get/change some of the info on the StringPool class and this
appears to work very well, although I have still to figure out some aspects of this
process and how it can be improved. Hope this info helps... Rgds Eusiri Some
example code follows: public class NewDomParser extends DOMParser { static
void main(String[] args) { try { NewDomParser d = new NewDomParser();
d.parse("test.xml"); } catch (Exception e) { e.printStackTrace(); } } public void
startDTD(QName r, int p, int s) { // the following needs to be changed for dtd
manipulation look at StringPool documentation
System.out.println(this.fStringPool.toString(s)); } }

Re: Re: Validating xml again dtd


Author: ercan karadeniz
(http://www.jguru.com/guru/viewbio.jsp?EID=526206), Oct 21, 2001
can you give example how can I change the value of the DTD filename. I can
read out the DTD filename value, but how can I set fStringPool value? Thanks
How to convert XML to a flat text file? Is there a reusable class available for
extracting specific tag values and its attributes and to form a text file with
the data.
Location: http://www.jguru.com/faq/view.jsp?EID=332918
Created: Feb 19, 2001 Modified: 2001-02-20 20:26:49.947
Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985)
Question originally posed by Natesan Kannaiyan
(http://www.jguru.com/guru/viewbio.jsp?EID=331142

I think the better way to do that is to use an XSLT processor that lets you to
transform an XML document to any format included html, pdf, or of course simple
text. I suggest you to learn more about XSL/XSLT.

Where can I get information and sample code for using JAXP and JAXM?
Location: http://www.jguru.com/faq/view.jsp?EID=332919
Created: Feb 19, 2001 Modified: 2001-02-20 20:25:06.495
Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985)
Question originally posed by Rajesh Sawant
(http://www.jguru.com/guru/viewbio.jsp?EID=331010

Check out SUN's XML site.


Comments and alternative answers

All about JAXP article unravels myths of JAXP, SAX,...


Author: Chandra Patni (http://www.jguru.com/guru/viewbio.jsp?EID=33585), Feb 21,
2001
All about JAXP article unravels myths of JAXP, SAX, DOM, JDOM, etc. It also
provides a good starting point for learning XML parsing.
<name>Hello</name>
<att1 static="name"></att1>
Here name is an element, Hello is the content and static is an attribute. I
can display this attribute value using xsl
<xsl:value-of select="att1/@static" />
This will output the string "name". But I want to display the value of name
by using the static attribute. In other words, I want the value of static to be
the element that is displayed. Is it possible?
Location: http://www.jguru.com/faq/view.jsp?EID=339380
Created: Feb 26, 2001 Modified: 2001-02-27 07:57:46.538
Author: Shamith Hamid (http://www.jguru.com/guru/viewbio.jsp?EID=339378)
Question originally posed by sriman v
(http://www.jguru.com/guru/viewbio.jsp?EID=334671
Assume that you have the follwoing XML:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<name>Hello</name>
<att1 static="name"/>
</xml>
then the following XSL will produce the required output:

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="xml/*[name() = /xml/att1/@static]" />
</xsl:template>

</xsl:stylesheet>

Can we make a generalised schema file by which we can validate only the
hierarchy of the target XML file, not the tag names in that XML file?
Location: http://www.jguru.com/faq/view.jsp?EID=339807
Created: Feb 27, 2001 Modified: 2001-02-27 08:22:33.98
Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985)
Question originally posed by Sankar Gandhi
(http://www.jguru.com/guru/viewbio.jsp?EID=141972

Speaking about hierarchy it means that you have defined father elements and child
elements. How's possible to check hierarchy without checking tag names? Hierarchy
implies tag name checking.

If you don't specify any DTD or XMLSchema the only check done is about the well
formedness of the XML. In some way, this can be assimilated to a basic hierarchy
check.

Xerces implementation of a Node is a Serializable Implementation

(public abstract class NodeImpl implements Node, NodeList, EventTarget,


Cloneable, Serializable )
When serializing a Node (for example passing Over RMI) the whole
Document is serialized which proves to be really poor on Performance.
Is there any way to Optimize Xerces Node serialization ?
Location: http://www.jguru.com/faq/view.jsp?EID=341544
Created: Mar 1, 2001 Modified: 2001-03-01 07:29:36.005
Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985)
Question originally posed by David Dankwerth
(http://www.jguru.com/guru/viewbio.jsp?EID=27063
In Xerces there's a class called org.apache.xml.serialize.XMLSerializer that is
an implementation of different XML handlers (ContentHandler, DeclHandler,
DocumentHandler, DTDHandler, LexicalHandler) to be used with SAX. If you want you
can use it also alone, whitout SAX.

With this you don't serialize the DOM Object but you transform everything to an XML
String (easier to serialize, and obviously faster to transmit). The disadvance is that,
from the other side you get a String to be parsed to a Node. Anyway you can try to
check performance improvements/loss. (I think should be an improvement.)

Comments and alternative answers

XMLSerializer does answer it for Elements because...


Author: David Dankwerth (http://www.jguru.com/guru/viewbio.jsp?EID=27063),
Mar 6, 2001
XMLSerializer does answer it for Elements because a DocumentFragment is a valid
Node that one can serialize. On the other hand, trying to parse the passed string back
to a node/element/document fails because a DocumentFragment is not a valid
document. One could wrap the fragment with a root element and then strip it out after
parsing.

I want a servlet generated XML document to be displayed in an applet. But


when I am using the HandlerBase class of org.xml.sax package in the applet
it throws a ClassNotFoundException even after importing the proper
packages. This happens only in the browser, in the applet viewer the applet
displays the parsed XML. How do I solve the problem?
Location: http://www.jguru.com/faq/view.jsp?EID=341596
Created: Mar 1, 2001 Modified: 2001-03-01 07:20:34.074
Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985)
Question originally posed by rajeev pai
(http://www.jguru.com/guru/viewbio.jsp?EID=12278

This should be because some jar's class is missing; add all the needed jars
(xerces.jar for example) to the classpath of the servlet-engine.

Check the SAX version and all xml jars too. Maybe your servlet engine is using an
older one (maybe because the order of the jars in the classpath is wrong).

Try to open the jar of the servlet engine and check if it contains SAX classes. If yes,
put the jar containing your (updated) sax classes before the jar of the servlet engine
on its startup command. Note that inside xerces.jar, sax classes are included. So...
try to put xerces.jar before the servlet engine's jars.
If the problems persists, try to put xerces as the first jar in the classpath.

Example

before
java -cp servletEngine.jar;...;xerces.jar;... servletEngine.Server

after...
java -cp xerces.jar;servletEngine.jar;... servletEngine.Server

Can I use a Generic SAX/DOM XML parser for Tomcat instead of parser.jar?
I tried using Xerces and get class not found errors for sun.xxxx parser
during Tomcat initialization.
Location: http://www.jguru.com/faq/view.jsp?EID=341619
Created: Mar 1, 2001 Modified: 2001-03-01 07:25:10.775
Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985)
Question originally posed by bruce carson
(http://www.jguru.com/guru/viewbio.jsp?EID=217941

Try to configure Xerces as customized SAX/DOM implementation by system


properties. By default jaxp uses com.sun.xml.parser.DocumentBuilderFactoryImpl
and com.sun.xml.parser.SAXParserFactoryImpl (this is why you get
ClassNotFoundException if you miss parser.jar from the classpath).
Example:

System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFacto
ryImpl");
System.setProperty("javax.xml.parsers.SAXParserFactory",
"org.apache.xerces.jaxp.SAXParserFactoryImpl
");
Comments and alternative answers

I haven't been able to run the SimpleTransform example...


Author: Jorge Jordão (http://www.jguru.com/guru/viewbio.jsp?EID=275762), Mar 5,
2001

I haven't been able to run the SimpleTransform example included with JAXP 1.1 as a
JSP under Tomcat 3.2.1

Initially I got a javax.xml.parsers.ParserConfigurationException: Namespace not


supported by SAXParser .

Then I saw this entry and defined both system properties (as well as
javax.xml.transform.TransformerFactory, just in case).

Now I get a javax.xml.parsers.FactoryConfigurationError:


org.apache.crimson.jaxp.SAXParserFactoryImpl

I know I have access to the classes, because I tried a Class.forName and got no
ClassNotFoundException

Anybody got a clue ?

Re: I haven't been able to run the SimpleTransform example...


Author: siva veerappan (http://www.jguru.com/guru/viewbio.jsp?EID=426484),
May 22, 2001
The solution provided by Fabio Moratti is working, when we had the same
problem as yours. Siva

The problem is in the way the Tomcat startup script...


Author: Fabio Moratti (http://www.jguru.com/guru/viewbio.jsp?EID=63024), Mar 6,
2001
The problem is in the way the Tomcat startup script sets the CLASSPATH. Parser.jar
is found before xerces.jar. Modifying the tomcat.bat and or tomcat.sh to include
xerces.jar at the beginning of the generated CLASSPATH worked fine for me.

JAXP1.1 uses org.apache.xml.tree.* and org.apache....


Author: Saravanan Jayachandran
(http://www.jguru.com/guru/viewbio.jsp?EID=91462), Mar 6, 2001
JAXP1.1 uses org.apache.xml.tree.* and org.apache.xml.parser.* found in
crimson.jar. This jar is packaged in JAXP1.1 itself. Actually Sun has donated their
parser implemetations (i.e com.sun.xml.* ) to Apache under the name "Crimson". So
try by including crimson.jar in CLASSPATH.

I am designing a database to store XML elements and attributes. Can


anybody suggest how I can store a hierachy of elements? The design should
be such that I can retrive the full tree when I submit a root element.
Location: http://www.jguru.com/faq/view.jsp?EID=341669
Created: Mar 1, 2001 Modified: 2001-03-01 07:27:33.468
Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985)
Question originally posed by nitin shinde
(http://www.jguru.com/guru/viewbio.jsp?EID=280770

Create a table as this:

table ELEMENT
ELEM-ID number(9) primary key,
ELEM-NAME char(30),
ELEM-CONTENTS char(30),
PARENT-ID number(9), (references another ELEM-ID)

table ATTRIBUTE
ATTR-ID number(9) primary key,
ATTR-NAME char(30),
ATTR-VALUE char(30),
ELEM-OWNER-ID number(9), (refrences an ELEM-ID)

Elements with null in PARENT-ID are root's elements, so you can make a query
selecting null in PARENT-ID to get the list of root's elements.

To get all the children of an element you have to select all elements having parent-id
= your elem-id.

Attributes are referenced 1 to many with Elements.

Comments and alternative answers

Also, look at http://www.ozone-db.org, an open source...


Author: phillip rhodes (http://www.jguru.com/guru/viewbio.jsp?EID=341878), Mar 1,
2001
Also, look at http://www.ozone-db.org, an open source java xml data store.

Where can I find source code examples or tutorials relating to JAXP 1.1?
Location: http://www.jguru.com/faq/view.jsp?EID=347493
Created: Mar 8, 2001 Modified: 2001-03-09 07:08:11.356
Author: Chandra Patni (http://www.jguru.com/guru/viewbio.jsp?EID=33585)
Question originally posed by Tracey Doyle
(http://www.jguru.com/guru/viewbio.jsp?EID=347176

JAXP revisited at IBM developerWorks gives an introduction to JAXP 1.1 features and
usage code snippets. Also, section 3 of JAXP 1.1 Specification Review contains some
examples of JAXP1.1 features.

I'm looking for a DOM-style XML parser that doesn't load the entire
document. Rather, it would let you get an element, then iterate over the
element's children, presenting each child as a DOM node but only loading
one child at a time (to conserve memory when there's a large number of
children. Where I can get such a parser?
Location: http://www.jguru.com/faq/view.jsp?EID=347543
Created: Mar 8, 2001 Modified: 2001-03-09 07:07:43.7
Author: Chandra Patni (http://www.jguru.com/guru/viewbio.jsp?EID=33585)
Question originally posed by Bobby Woolf
(http://www.jguru.com/guru/viewbio.jsp?EID=346655

You can use JDOM API (See also JSR0102) to achieve this effect. The following
snippet of code shows how JDOM can be used to get the java.util.List of
child(ren) of a node. In JDOM, a node is said to be org.jdom.Element.
org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
org.jdom.Element root = builder.build(new
java.io.File("file.xml")).getRootElement();
java.util.List children = root.getChildren();
//...
An org.jdom.Element can be convert to org.w3c.dom.Element by using
org.jdom.output.DOMOutputter class. Moreover, JDOM lets you change the modify
the document without having to worry about underlying parser. See also: Parsing a
very large XML file FAQ.
Comments and alternative answers

I think even JDOM loads the entire document in memory,...


Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985), Mar
17, 2001
I think even JDOM loads the entire document in memory, but it's lightweighter than
DOM because the tree structure is smarter and more "java-centric".

JDOM does load the entire document into memory...


Author: Lee Lichtenstein (http://www.jguru.com/guru/viewbio.jsp?EID=405341), Apr
18, 2001
In order to use the SAXOutputter, which fires the SAX events, you must still build a
JDOM Document first.

Selective loading
Author: Subrahmanyam Allamaraju
(http://www.jguru.com/guru/viewbio.jsp?EID=265492), Jun 3, 2001
I original answer to the question is incorrect and is misleading.

There were a few attempts ("pull-style" parsers) to solve the problem of selectively
loading contents of XML into memory. Refer to
http://www.ltg.ed.ac.uk/software/xml/ for one such attempts. However there are no
standard efforts to build parsers meeting this requirement.

After setting a variable using the <xsl:variable> tag, how can I change the
value of that variable somewhere else in the xsl document?
Location: http://www.jguru.com/faq/view.jsp?EID=348065
Created: Mar 9, 2001 Modified: 2001-03-12 06:57:33.276
Author: JP Moresmau (http://www.jguru.com/guru/viewbio.jsp?EID=127279)
Question originally posed by Aseel Ali
(http://www.jguru.com/guru/viewbio.jsp?EID=339840

You can't. Once set, a variable is constant in XSLT. You'll need to create a new
variable.

Question : Can XSL XSLT be used with SAX? This is what we want to do: We
have live tick data stream, (an XML data stream). So we cannot use DOM.

<.td>20000929-11:30:00.000 EDT<./td> ->convert to (Xdata)tag

<.td>111.412917<./td> ->convert to (Y1data)tag

<.td>111.101185<./td> ->convert to (Y2data)tag

<.td>1034500.000000<./td> ->convert to (Y3data)tag

How can we translate the tags without using DOM?


Location: http://www.jguru.com/faq/view.jsp?EID=348066
Created: Mar 9, 2001 Modified: 2001-03-12 07:05:32.412
Author: JP Moresmau (http://www.jguru.com/guru/viewbio.jsp?EID=127279)
Question originally posed by S M
(http://www.jguru.com/guru/viewbio.jsp?EID=346304
Look at JAXP (the Java Api for Processing XML) on the Java Sun site: it comes with
Xalan as an XSLT engine, and the transform method words with DOM, SAX or direct
Streams. This is probably what you want. JAXP is very clear and easy to use as well,
I'd recommend it!!
Comments and alternative answers

In my knowledge XSL Transformations can be applyed...


Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985), Mar
17, 2001
In my knowledge XSL Transformations can be applyed only on DOM structures (and
maybe with JDOM in future), so you have to cache the "live" stream, then split it to
independent elements to parse with DOM, then process them one by one.

The SAX' limitation is because with SAX there's no concept of hierarchy, and this is a
requirement for XSL processing.

I don't think JAXP can solve your problem

Re: In my knowledge XSL Transformations can be applyed...


Author: John Williamson (http://www.jguru.com/guru/viewbio.jsp?EID=24309),
Mar 21, 2001
Apache's Xalan XSLT processor has classes that can take XML data from either
DOM or SAX. Check out the apache web site. http://xml.apache.org/xalan-
j/overview.html I can post a simple java example on request.

Re: Re: In my knowledge XSL Transformations can be applyed...


Author: Doug Erickson (http://www.jguru.com/guru/viewbio.jsp?EID=1718),
Mar 31, 2001

Xalan can use a SAX source, but no transformation is done until the document
is complete. The parser will throw an exception if more than one document
appears on a stream.

According to the original question, elements are streaming in, and transformed
output is to be streamed out. There is no EOF after each document.

In order to achieve the desired result, there must be some pre-parser chunking
the continuous stream into many streams, each containing a single XML
document and terminated by EOF. These individual streams can then be
handled by XSLT.

Re: Re: In my knowledge XSL Transformations can be applyed...


Author: bhiku mhatre (http://www.jguru.com/guru/viewbio.jsp?EID=31426),
Apr 5, 2001
Hi John, kindly send a simple example.

I want to use Xerces1.3 to validate a DOM using a DTD. The syntax is:
parser.setFeature("http://xml.org/sax/features/validation", true); It
seems I have to have access to internet.But how about if I can't get access
to internet? Is there any parser can do validation without having access to
internet?
Location: http://www.jguru.com/faq/view.jsp?EID=348068
Created: Mar 9, 2001 Modified: 2001-03-12 06:58:20.592
Author: JP Moresmau (http://www.jguru.com/guru/viewbio.jsp?EID=127279)
Question originally posed by Jeff C
(http://www.jguru.com/guru/viewbio.jsp?EID=346504

Yes, you need to have access to the Internet if your DTD references a URL on the
NET. The alternative is to provide your parser with a custom EntityResolver (a class
that implements the EntityResolver interface) using the
parser.setEntityResolver(EntityResolver) method. In this class, define a method :
public InputSource resolveEntity (String publicId, String systemId) throws
SAXException, IOException and return an InputSource pointing to a local DTD. The
parser will use that DTD to validate your document. Have a look at the Javadoc API
documentation for more details.
Comments and alternative answers

Feature names
Author: Doug Erickson (http://www.jguru.com/guru/viewbio.jsp?EID=1718), Mar 31,
2001

It's important to note that when setting this validation feature, the URL
"http://xml.org/sax/features/validation" is NOT opened. This string is simply used as a
name that uniquely identifies the validation feature. The name could have been simply
"validation," but using this kind of URI decreases the chance of feature name
collision.

If your document contains its own DTD or refers to a SYSTEMID accessible locally,
neither Internet connectivity nor a custom EntityResolver is required for validation.

You don't need internet access, but concrete implementation of ErrorHandler.


Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432),
Aug 26, 2001
See you detailed answer here:
My xerces/crimson parser does not validate for me? The following code does not
work, why?

How can we use MSXML parser from a JSP in order to... How can we use
MSXML parser from a JSP in order to transform XML file using a stylesheet
without having to use Javascript for the same?
Location: http://www.jguru.com/faq/view.jsp?EID=385296
Created: Mar 22, 2001
Author: Arun Bharathan (http://www.jguru.com/guru/viewbio.jsp?EID=106958)
Question originally posed by duds d
(http://www.jguru.com/guru/viewbio.jsp?EID=349341

Re: How can we use MSXML parser from a JSP in order to... If you are using IE 5.0
or above, return the XML to the browser with a Processing Instruction (PI) as follows
and the Browser would automatically use MSXML to generate HTML.
<?xml:stylesheet type="text/xsl" href="thexsltobeused.xsl"?>
Comments and alternative answers
msxml in java servlet
Author: Kumar Sachwani (http://www.jguru.com/guru/viewbio.jsp?EID=523774),
Oct 31, 2001
hi dude nice reply but i want to do something different i want to parse the xml
document thru my servlet and return html output to the browser can u tell me a way
thru which i can access the msxml parser just the way i access saxon or xerces in
servlets thanks kumar

Re: msxml in java servlet


Author: Arun Bharathan (http://www.jguru.com/guru/viewbio.jsp?EID=106958),
Dec 5, 2001
Well, MSXML doesn't have a Java implementation. You would have to write JNI to
get to the MSXML parser.

BTW, who wants MSXML, use the Xerces parser from apache

How Xalan and Xerces pronounced? How Xalan and Xerces pronounced?
Location: http://www.jguru.com/faq/view.jsp?EID=403612
Created: Apr 16, 2001
Author: swarraj kulkarni (http://www.jguru.com/guru/viewbio.jsp?EID=121306)
Question originally posed by Tiger Smith
(http://www.jguru.com/guru/viewbio.jsp?EID=325564

Re: Re: How Xalan and Xerces pronounced? Put 'z' in the place of 'X' :O) Xalan -
zalan Xerces - zerces
Comments and alternative answers

Pronunciation
Author: Stephen Oakes (http://www.jguru.com/guru/viewbio.jsp?EID=406487), Apr
20, 2001
How the heck is "zerces" pronounced? :-)
It could be as in purses or as in mercies. It could even have a hard "c". I have heard
exersies and zersies.

Including of special character in XML File If a text containing special


character such as control key characters, how that character can be
bypassed while parsing.
Location: http://www.jguru.com/faq/view.jsp?EID=403614
Created: Apr 16, 2001
Author: vivek r.c (http://www.jguru.com/guru/viewbio.jsp?EID=67381) Question
originally posed by Joseph George
(http://www.jguru.com/guru/viewbio.jsp?EID=302027

CDATA sections are used to escape blocks of text containing characters that would
otherwise be regarded as markup. The only delimiter that is recognized in a CDATA
section is the "]]>" string that ends the CDATA section. CDATA sections can not be
nested. The primary purpose is for including material such as XML fragments,
without needing to escape all the delimiters.
Comments and alternative answers

Control characters in CDATA section


Author: Paul Vogel (http://www.jguru.com/guru/viewbio.jsp?EID=216851), May 31,
2001
When I put control key charcters ( /u0000 ../u001F) in a CDATA section , then the
XML file is not well formed (XML Spy). The XML specification from W3C do not
say anything about that!

I have the same problem, and I don't have to use CDATA section.
Author: Oscar Herrero (http://www.jguru.com/guru/viewbio.jsp?EID=1124879), Oct
30, 2003
In example, I receive this XML document. <?xml version="1.0" encoding="ISO-
8859-1"?> <xml> <nodo>...&...,<...</nodo> </xml> When I parse It with Document
doc = builder.parse ( theInputSource), the parser converts nodo to ...&...<... and I
don't want it. What can I do?

What is the difference between #PCDATA and #CDATA in XML?


Location: http://www.jguru.com/faq/view.jsp?EID=414480
Created: May 3, 2001
Author: Karthikeyan B (http://www.jguru.com/guru/viewbio.jsp?EID=203250)
Question originally posed by shine rajan
(http://www.jguru.com/guru/viewbio.jsp?EID=414345

PCDATA is Parsed character data and CDATA is just character data.

In PCDATA text, the tags inside the text will be treated as markup and entities will be
expanded, where as in CDATA, text will NOT be parsed by the XML parser.

Comments and alternative answers

CDATA and PCDATA confusion.


Author: Omar Khan (http://www.jguru.com/guru/viewbio.jsp?EID=232689), Jul 11,
2001
There is a lot of confusion regarding CDATA and PCDATA.
First of all #CDATA is not a keyword.
CDATA and PCDATA are concepts while CDATA and #PCDATA are keywords.
CDATA keyword is an attribute type and may also be used in element sections, but it
is not considered wise. #PCDATA keyword can just be used in element section. As far
as I undestand both CDATA and #PCDATA keywords denote just plain text. See also
Clarification about CDATA and CDATA Confusion.

XML Query Language Where can I learn about the efforts to create an XML-
based query language?
Location: http://www.jguru.com/faq/view.jsp?EID=414493
Created: May 3, 2001
Author: Alessandro A. Garbagnati
(http://www.jguru.com/guru/viewbio.jsp?EID=32727) Question originally posed by
John Zukowski PREMIUM (http://www.jguru.com/guru/viewbio.jsp?EID=7

There are two different XML based query languages. One is the W3C language XML-
QL (http://www.w3.org/TR/1998/NOTE-xml-ql-19980819/.
The second one is XQL (http://www.ibiblio.org/xql/).
In addition, there are few different opinions if XPath (http://www.w3.org/TR/xpath)
can be considered, or not, an XML Query language. This is mainly based on the fact
that being "[...] a language for addressing parts of an XML document. [...]", it does
much of what a query language should (and would) do.

An interesting reading, anyway, is this article (from the W3Consortium): Ten


Features Necessary for an XML Query Language.
Comments and alternative answers

XML Query Language


Author: Howard Katz (http://www.jguru.com/guru/viewbio.jsp?EID=249143), May 4,
2001
I don't think XML-QL was ever implemented in as large a degree as XQL. There are
probably a good dozen implementations of the latter.

More interestingly, the W3C announced a working draft for "XQuery: A Query
Language for XML" on February 15, 2001. XQuery is an interesting amalgam of a
SQL-like syntax grafted on to XPath. The working draft is at
www.w3.org/TR/xquery. There are some other supporting documents at
www.w3.org/XML/Query.

How can I query my xml file? For example I have this xml file:

<?xml version="1.0"?>
<bib>
<book>
<title>Harold</title>
<author>Johnson</author>
</book>
<book>
<title>Harold's Fairy Tale</title>
<author>Crockett</author>
</book>
</bib>
and I want to display the result of "author where title = 'Harold'". How
should I write the query and where I should put it, in another xml file? or
html file?
Location: http://www.jguru.com/faq/view.jsp?EID=414509
Created: May 3, 2001
Author: Chris Leonardi (http://www.jguru.com/guru/viewbio.jsp?EID=412964)
Question originally posed by Corina Stefan
(http://www.jguru.com/guru/viewbio.jsp?EID=410600
XPath provides a means of querying down into XML documents:
http://www.w3.org/TR/xpath Look at 'Location Paths' for the syntax relevant to your
particular query. Apache Xalan implements the XPath 1.0 recommendation:
http://xml.apache.org/xalan-j/index.html Typically, you'd write a XSL styelsheet that
uses an XPath expression to traverse your document tree and select the appropriate
nodes. You can also use xpath programatically from a Java class; Xalan provides an
API for that, too.
Comments and alternative answers

XSLT
Author: Kenneth Liu (http://www.jguru.com/guru/viewbio.jsp?EID=304008), May 3,
2001
It's going be hard to find books dedicated to XPath. The best information is going to
be found in books on XSLT, since XSLT and XPath are closely related.

XPath
Author: Dane Foster (http://www.jguru.com/guru/viewbio.jsp?EID=224441), Jun 25,
2001
There is a Java Library for working with XML call dom4j. It has built in support for
XPath in its DOM, so you can query any XML data-structure using XPath. Check it
out at http://www.dom4j.org

Sample code for querying XML file using XPath


Author: M.Kumar Sankar (http://www.jguru.com/guru/viewbio.jsp?EID=31021), Dec
3, 2002
Do any one suggest a sample code to query an XML file
using XPath expression. Better if using apache Xalan...

Do SAX and DOM validating parsers take the same amount of memory while
validating a document?
Location: http://www.jguru.com/faq/view.jsp?EID=414518
Created: May 3, 2001
Author: Frank Nestel (http://www.jguru.com/guru/viewbio.jsp?EID=410382)
Question originally posed by jeetendra kumar
(http://www.jguru.com/guru/viewbio.jsp?EID=410361

If you only need to validate a document and not do anything else with it, SAX does it
without actually taking memory for the document. Just implement an empty handler
which captures all SAX-events and ignores them.

All existing DOM parsers actually seem to be built on top of SAX parsers and a DOM
is a very costly (but also valuable at times) data structure in memory. DOM comes in
handy if you have to manipulate documents in memory.

Where can I learn (more) about Application Servers?


Location: http://www.jguru.com/faq/view.jsp?EID=431183
Created: May 30, 2001
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru AppServer FAQ.


Where can I learn (more) about Java's suport for internationalization
(I18N)?
Location: http://www.jguru.com/faq/view.jsp?EID=431191
Created: May 30, 2001 Modified: 2001-08-18 19:04:05.76
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru I18N FAQ.

Where can I learn (more) about Java's I/O (input/output, IO) capabilities?
Location: http://www.jguru.com/faq/view.jsp?EID=431192
Created: May 30, 2001
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru IO FAQ.

Where can I learn (more) about Java's reusable software components,


JavaBeans?
Location: http://www.jguru.com/faq/view.jsp?EID=431197
Created: May 30, 2001 Modified: 2001-06-16 16:04:06.477
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru JavaBeans FAQ.

Comments and alternative answers

Advanced Java Programming


Author: Naveen Madenhalli (http://www.jguru.com/guru/viewbio.jsp?EID=1253053),
Jul 13, 2005
If a applet client wants to send a request to a server, what are the various steps to be
followed?

Where can I learn (more) about Java JSP (JavaServer Pages)?


Location: http://www.jguru.com/faq/view.jsp?EID=431214
Created: May 30, 2001
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru JSP FAQ.

Comments and alternative answers

JSP Tutorial
Author: Steve Erbert (http://www.jguru.com/guru/viewbio.jsp?EID=291279), Jun 26,
2001
www.jsptut.com has a very good beginners introduction to JSP.

Re: JSP Tutorial


Author: D.Narasimham Dhurjati
(http://www.jguru.com/guru/viewbio.jsp?EID=460448), Jul 24, 2001
Yes. I need jsp tutorial

Where can I learn (more) about Java Serialization?


Location: http://www.jguru.com/faq/view.jsp?EID=431245
Created: May 30, 2001
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru Serialization FAQ.

Where can I learn (more) about Java Servlets?


Location: http://www.jguru.com/faq/view.jsp?EID=431246
Created: May 30, 2001
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru Servlets FAQ.

Comments and alternative answers

Advanced Java Programming


Author: Naveen Madenhalli (http://www.jguru.com/guru/viewbio.jsp?EID=1253053),
Jul 13, 2005
Explain the various steps involved in building your own bean with suitable example

Where can I learn (more) about JCA (Java Connector Architecture)?


Location: http://www.jguru.com/faq/view.jsp?EID=431958
Created: May 31, 2001
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the Sun's Java Connector Architecture homepage.

Are there any XML viewers written in JAVA that use CSS?
Location: http://www.jguru.com/faq/view.jsp?EID=434729
Created: Jun 6, 2001
Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985)
Question originally posed by puneet suri
(http://www.jguru.com/guru/viewbio.jsp?EID=58907

There are some good editors:

• Merlot XML Editor


• LeanEdit
• treenotes

I want to do is start using XML, but I cannot seem to find any of the classes
in my HP JDK.

1) To use XML, say with DOM objects, do I have to hunt around the net for
the various Java pieces, or can they be obtained from Sun directly?
2) Does SUN require other vendors to supply the XML class files, etc?
3) Is XML, a standard part of a JDK distribution?
Location: http://www.jguru.com/faq/view.jsp?EID=434732
Created: Jun 6, 2001
Author: Alessandro A. Garbagnati
(http://www.jguru.com/guru/viewbio.jsp?EID=32727) Question originally posed by
Peter Rupp (http://www.jguru.com/guru/viewbio.jsp?EID=324926

The Java API for XML Processing it's not part of the standard Jdk distribution. The
package can be downloaded from the above link (http://java.sun.com/xml/) and it
will contain the basics classes for SAX or DOM parsing of any XML compliant
document.
Comments and alternative answers

JDK 1.4
Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985), Jun
6, 2001
Correct. And a good news: JDK 1.4 comes with JAXP and other APIs to manage
XML easily (like mapping between XML and JavaBeans etc...).

Re: JDK 1.4


Author: tim lok (http://www.jguru.com/guru/viewbio.jsp?EID=74952), Jun 11,
2001
Where can I get more info?

Re: Re: JDK 1.4


Author: Luigi Viggiano
(http://www.jguru.com/guru/viewbio.jsp?EID=101985), Jun 12, 2001
You can download it at http://java.sun.com/j2se/1.4/; happy coding...

Is there a way to influence indentation when trans... Is there a way to


influence indentation when transforming a DOM to an output stream using
the JAXP Transform API?
Location: http://www.jguru.com/faq/view.jsp?EID=442323
Created: Jun 20, 2001
Author: Doug Erickson (http://www.jguru.com/guru/viewbio.jsp?EID=1718)
Question originally posed by dedi shindler
(http://www.jguru.com/guru/viewbio.jsp?EID=341789

Use <xsl:output indent=yes/> as a top-level element in your stylesheet.


Comments and alternative answers

Indent level varies by processor


Author: Shane Curcuru (http://www.jguru.com/guru/viewbio.jsp?EID=543803), Nov
20, 2001
Note that the XSLT spec does not strictly say what format indenting should use, so
results with indent="yes" will vary.
Xalan by default indents each new output element by putting it on a new line, but for
performance reasons does not actually indent a number of spaces. You can change
this in Xalan by seeing http://xml.apache.org/xalan-j/usagepatterns.html#outputprops
and setting something like:

xalan:indent-amount="2"

I have a set of import dependencies between XSL files and I would like to
package them inside a jar file. If I refer to the main XSL through the
packaging directory structure so that I can use getResourceAsStream() it
fails to get the imported ones. Apparently set systemId does not work for
XSL files as resources inside a Jar file. I am using XERCES/XALAN.
Location: http://www.jguru.com/faq/view.jsp?EID=442328
Created: Jun 20, 2001 Modified: 2001-11-15 16:09:12.977
Author: Doug Erickson (http://www.jguru.com/guru/viewbio.jsp?EID=1718)
Question originally posed by Lemao Lemao
(http://www.jguru.com/guru/viewbio.jsp?EID=37814

Rather than getting the resource as a stream, use

URL url = getClass().getResource(name);


Source xsl = new StreamSource(url.toExternalForm());
Transformer xform = factory.newTransformer(xsl);

When you supply the system ID, relative references will be resolved with respect to
it. When only the stream is provided, the processor has no idea where the data is
coming from and can't resolve relative paths.

What exactly are OASIS and ebXML? And if they are alternatives which one
should be followed?
Location: http://www.jguru.com/faq/view.jsp?EID=442329
Created: Jun 20, 2001
Author: Rahul kumar Gupta (http://www.jguru.com/guru/viewbio.jsp?EID=4809)
Question originally posed by kishan bisht
(http://www.jguru.com/guru/viewbio.jsp?EID=303016

ebXML(Electronic Business XML) is replacement for the EDI and used for the
secure exchange of buiness data. As it is an XML based so it now slowly and steadiliy
becoming an de facto for the industries

for more visit http://www.ebxml.org/white_papers/whitepaper.htm

OASIS (Organization for Structured Information Standards) is a nonprofit,


international consortium whose goal is to promote the adoption of product-
independent standards

for more visit http://www.oasis-open.org/


Comments and alternative answers
ebXML
Author: Vijay Kumar (http://www.jguru.com/guru/viewbio.jsp?EID=264226), Sep 14,
2001
Has anybody implemented ebXML in java. Plese let me know?

Using a SAX parser, is there a way to include a local DTD file when parsing
an XML message that doesn't contain a DOCTYPE tag?
Location: http://www.jguru.com/faq/view.jsp?EID=445798
Created: Jun 26, 2001 Modified: 2001-11-15 16:07:45.98
Author: Alessandro A. Garbagnati
(http://www.jguru.com/guru/viewbio.jsp?EID=32727) Question originally posed by
Thomas Liebmann (http://www.jguru.com/guru/viewbio.jsp?EID=315871

Hi,
There is a way to achieve that.
Check the org.xml.sax.EntityResolver interface:
If a SAX application needs to implement customized handling for external entities, it
must implement this interface and register an instance with the SAX parser using the
parser's setEntityResolver method.

The parser will then allow the application to intercept any external entities (including
the external DTD subset and external parameter entities, if any) before including
them.

Many SAX applications will not need to implement this interface, but it will be
especially useful for applications that build XML documents from databases or other
specialised input sources, or for applications that use URI types other than URLs.
Comments and alternative answers

And????
Author: Sukhbir Bassi (http://www.jguru.com/guru/viewbio.jsp?EID=986685), Aug
21, 2002
I am working on a project that has encountered this problem, and l am no further in
getting it resolved from this "answer". How would you implement the
EntityResolver? Sample code would be good.

What is the difference between XML Parsing and XML Processing?


Location: http://www.jguru.com/faq/view.jsp?EID=445800
Created: Jun 26, 2001
Author: Alessandro A. Garbagnati
(http://www.jguru.com/guru/viewbio.jsp?EID=32727) Question originally posed by
Saravanan Jayachandran (http://www.jguru.com/guru/viewbio.jsp?EID=91462

When you talk about "parsing" you are just talking of an operation that, for example,
breaks down a text into recognized strings of characters for further analysis. With
the previous version of JAXP 1.0.x (Java API for XML Parsing), in fact, you were only
able to open and parse an XML document.
When you talk about "processing", you are talking of operations that will allow you
not just to parse, but to apply some kind of transformation to the text. Sun's has
changed the name of their API to Java Api for XML Processing, maintaining the same
acronym JAXP. The reason of this change is because the new API contains not only
the javax.xml.parser, but also the javax.xml.transform package, that are used for, by
Sun definition, "[...]processing transformation instructions, and performing a
transformation from source to result.[...]".
If you need further information, take a look at the Section 4 of JAXP 1.1
Specification

How can I pass XML generated by JSP to Cocoon for further processing?
Location: http://www.jguru.com/faq/view.jsp?EID=445943
Created: Jun 26, 2001
Author: Ethan Winograd (http://www.jguru.com/guru/viewbio.jsp?EID=295214)
Question originally posed by Radha G
(http://www.jguru.com/guru/viewbio.jsp?EID=415336

[ I am using tomcat 3.2.1 and cocoon 1.8.2. and I am using JSP's for dynamic
generation of XML. How can i give this XML output to cocoon to apply XSLT (based on
the requested client) and to give the appropriate output.]

See The Apache XML Project: How To Get Read All Over .

Cocoon allows you to take content in XML, tweak it using Java code in a "logicsheet"
and apply styling from an XSL stylesheet. This makes it easy to transform into any
SGML-like markup language; with the addition of Apache Formatting Objects
Processor (FOP), you can go right to printable PDFs.
Comments and alternative answers

JSP and XML, XSL


Author: Khmer Tootz Reyes (http://www.jguru.com/guru/viewbio.jsp?EID=513281),
Oct 7, 2001
You can also use Xalan and Xerces directly instead of Cocoon. This will give you
more flexibility in tranforming your XML files especially when passing parameters to
the XSL file.

How do I read data from a stream as it arrives in Xerces (SAX2) using


XMLReader? It does not have a setReaderFactory method.
Location: http://www.jguru.com/faq/view.jsp?EID=454152
Created: Jul 12, 2001 Modified: 2001-11-15 16:06:13.229
Author: John Williamson (http://www.jguru.com/guru/viewbio.jsp?EID=24309)
Question originally posed by Siddhartha Subramanian
(http://www.jguru.com/guru/viewbio.jsp?EID=345641

Check out...

http://xml.apache.org/xerces-j/faq-write.html#faq-11

I need to design a database in which the columns have to be assigned


dynamically whenever a user defines a new attribute. Is it possible to have
a flexible database column design using XML, and an Oracle/SQL Server?
How?
Location: http://www.jguru.com/faq/view.jsp?EID=496159
Created: Sep 13, 2001 Modified: 2001-11-15 16:04:42.444
Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985)
Question originally posed by Amit Jain
(http://www.jguru.com/guru/viewbio.jsp?EID=111841

About the database, you can design it like this:

CREATE TABLE "MY_OBJECT"


(
"ID" INTEGER NOT NULL,
"PROPERTY_1" CHAR(30),
"PROPERTY_2" CHAR(30),
PRIMARY KEY ("ID")
);

CREATE TABLE "EXTERNAL_ATTRIBUTE"


(
"ID" INTEGER NOT NULL,
"OBJECT_ID" INTEGER NOT NULL,
"NAME" CHAR(30) NOT NULL,
"VALUE" CHAR(255),
PRIMARY KEY ("ID")
);

ALTER TABLE "EXTERNAL_ATTRIBUTE"


ADD FOREIGN KEY ("OBJECT_ID")
REFERENCES MY_OBJECT ("ID");
your XML can be similar to this:

<?xml version="1.0"?>
<my_object>
<property_1>prop1value</property_1>
<property_2>prop2value</property_2>
<external_attribute name="att1Name" value="att1Value" />
<external_attribute name="att2Name" value="att2Value" />
...
</my_object>
Each "my_object" instance is stored in a record of MY_OBJECT table, and each
"external_attribute" is stored in a record of EXTERNAL_ATTRIBUTE table with an
OBJECT_ID field that chain it to the referenced object. IDs are not present in the
XML because them should be managed by the application.

By using XSLT transformation technique how do I map an XML file to


another XML file? I have data in a particular format. How can I
automatically generate XSL mapping file, so that it can become input to
XSLT?
Location: http://www.jguru.com/faq/view.jsp?EID=496195
Created: Sep 13, 2001
Author: Michael Szlapa (http://www.jguru.com/guru/viewbio.jsp?EID=241392)
Question originally posed by Naga A
(http://www.jguru.com/guru/viewbio.jsp?EID=331085

This tool claims to do it:


www.StylusStudio.com

You can also try some other tools mentioned here: http://www.sys-
con.com/xml/wbg/open.cfm?BType=XSLT+Utilities

Unfortunately to create XSL this way you would have to have examples of XML files
that fully exercise all allowed elements of your DTD. To come up with complete XML
that utilizes all the elements described in DTD maybe fairly difficult.

I am trying to generate an XML file using the JAXP API. From a Java
application it works correctly but when used in a JSP it gives a
SAXException. I am running JSP page on the Java Web Server. Why is this
happening?
Location: http://www.jguru.com/faq/view.jsp?EID=496270
Created: Sep 13, 2001 Modified: 2001-11-15 16:02:56.421
Author: Suresh Rangan (http://www.jguru.com/guru/viewbio.jsp?EID=308330)
Question originally posed by ramesh gudipati
(http://www.jguru.com/guru/viewbio.jsp?EID=266045

It looks like your JSP file in the webserver is not able to reach the SAXparser jar file.
Make sure the classpath is set properly before running the webserver and also
inclusion of jar file is made to the appropriate lib directory of java webserver.
Comments and alternative answers

Probably older JAXP


Author: Roshan Shrestha (http://www.jguru.com/guru/viewbio.jsp?EID=130068), Oct
18, 2001
You probably need to replace the JAXP.JAR in TOMCAT_HOME/lib directory with a
newer version, or delete it, in which case it will use the one you put in your web
applications lib directory.

What is VoiceXML? How does it relate to J2ME?


Location: http://www.jguru.com/faq/view.jsp?EID=507383
Created: Sep 30, 2001
Author: Bill Day (http://www.jguru.com/guru/viewbio.jsp?EID=135825) Question
originally posed by Bill Day PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=135825

Voice eXtensible Markup Language (VoiceXML) specifies an XML-based markup


language for speech recognition and synthesis applications using landline and mobile
phone systems. Many J2ME-enabled devices will support a voice channel, and as
such may also be used to interact with VoiceXML-based services over the phone voice
connection.

VoiceXML also enables markup-based approaches to recognizing DTMF key input (the
tones made when you press a key on a touch tone phone handset) and recording
spoken input.
For more information on VoiceXML, please visit the related jGuru entries (search for
"VoiceXML" in all FAQs) and the VoiceXML Forum, at:
www.voicexml.org

Is there any ready made parser avaialble to parse HTML or to convert HTML
to XML or XHTML ? Basically I want to extract data from HTML.
Location: http://www.jguru.com/faq/view.jsp?EID=512157
Created: Oct 5, 2001 Modified: 2001-11-15 16:01:40.835
Author: Isaac Lopez (http://www.jguru.com/guru/viewbio.jsp?EID=460064)
Question originally posed by parul dholakia
(http://www.jguru.com/guru/viewbio.jsp?EID=451179

Try using Tidy http://www.w3.org/People/Raggett/tidy/ to clean up the HTML.


Comments and alternative answers

Try jTidy
Author: Brett Knights (http://www.jguru.com/guru/viewbio.jsp?EID=513989), Oct 8,
2001
This is available on SourceForge and includes a mode that returns an
org.w3c.dom.Document.

Perhaps a misunderstanding
Author: Bret McDanel (http://www.jguru.com/guru/viewbio.jsp?EID=704325), Dec
28, 2001
I dont think that he/she said they wanted to clean up the HTML, but instead extract
data from it. I have used the docuverse set, which takes an HTML page and allows
you to process it like an XML document. This has worked well for me in my projects,
but I am unsure if this is what you want. The URL for more information on this is
available at http://www.docuverse.com/domsdk/ I have used that to extract
information from a variety of web pages (including a custom news page where I fetch
stories from many different news sites - saves me the trouble of going to 20000 sites
to read 5 or so articles :)

Re: Perhaps a misunderstanding


Author: Walter Elly (http://www.jguru.com/guru/viewbio.jsp?EID=841024), Apr
16, 2002
Thank you for providing a good answer, I found this topic buried in a google
search and its just what I'm looking for. THank you.

Re[2]: Perhaps a misunderstanding


Author: Scott Trafford (http://www.jguru.com/guru/viewbio.jsp?EID=467858),
Jun 21, 2002
Does anyone have any basic java code that runs Docuverse DOM SDK? Very
new to java and would like to see what Docuverse DOM SDK can do. Thanks

BretMcdanel help me about converting HTML to XML using docuverse


Author: krishna kishore (http://www.jguru.com/guru/viewbio.jsp?EID=971378),
Jul 31, 2002
How can i use docuverse to convert html to xml

Docuverse DOM SDK


Author: Scott Trafford (http://www.jguru.com/guru/viewbio.jsp?EID=467858), Jun
21, 2002
Does anyone have any basic java code that runs Docuverse DOM SDK? Very new to
java and would like to see what Docuverse DOM SDK can do. Thanks

How to make Tomcat use the Xerces XML parser instead of the Sun JAXP
XML parser?
Location: http://www.jguru.com/faq/view.jsp?EID=525561
Created: Oct 19, 2001 Modified: 2001-11-15 15:27:44.912
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3) Question
originally posed by suraj R (http://www.jguru.com/guru/viewbio.jsp?EID=497669

You can do one of two things:

1. Replace the Tomcat system files jaxp.jar and parser.jar (located in


TOMCAT_HOME/lib) -- that is, delete them and install the Xerces jars in their
place
2. Include your preferred XML parser jars (xerces.jar and/or xalan.jar) inside
your web application's WEB-INF/lib

The second method is preferred, since then you can run other web applications --
and Tomcat itself -- without worrying that they're incompatible with Xalan/Xerces (or
whatever your favorite XML parser is). However, it has been known to fail, since
Classloader munging is something of a black art and hasn't always been
implemented correctly in Tomcat (or Java).

So try #2 first, then if that fails, try #1.

Comments and alternative answers

Jar Priority loading


Author: Gabriel Kohen (http://www.jguru.com/guru/viewbio.jsp?EID=325088), Oct
30, 2001
Well, you can always change the name of the xereces.jar and xalan.jar to axereces.jar
and axalan to be loaded first, or the parser.jar and jaxp.jar to zparser.jar and zjaxp.jar
so they will be loaded last.

Bug in tomcat 4.0.x


Author: Nikolay Ivanov (http://www.jguru.com/guru/viewbio.jsp?EID=1012756), Oct
16, 2002
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=11752

What is the relationship between the Sun Projet X library and JAXP? Is
there any other library aside from Project X which allows regestering my
own classes which are created when certain XML elements are encountered
(like factory.addMapping (props, classloader) in Project X)?
Location: http://www.jguru.com/faq/view.jsp?EID=548446
Created: Nov 15, 2001
Author: Shane Curcuru (http://www.jguru.com/guru/viewbio.jsp?EID=543803)
Question originally posed by Boris Liberman
(http://www.jguru.com/guru/viewbio.jsp?EID=333995

Project X was a parser originally available from Sun that has now been donated to
the Apache Software Foundation. See http://xml.apache.org/crimson/ for more
information on Crimson. Current (Nov-2001) releases of Crimson are used as the
reference implementation of the javax.xml.parsers package that forms half of the
JAXP 1.1 specification. Xerces is another popular Apache parser that also implements
the parsing half of JAXP 1.1.

I am trying to run this XALAN sample and I have a JSP page whicn performs
transformations, I am getting this exception
"javax.xml.transform.TransformerConfigurationException: Namespace not
supported by SAXParserNamespace " Does anybody know why??
Location: http://www.jguru.com/faq/view.jsp?EID=548448
Created: Nov 15, 2001
Author: Shane Curcuru (http://www.jguru.com/guru/viewbio.jsp?EID=543803)
Question originally posed by Amad Fida
(http://www.jguru.com/guru/viewbio.jsp?EID=19191

Please see http://xml.apache.org/xalan-j/faq.html#faq-15 which addresses this


specifically. This is (essentally) because your servlet or JSP container is giving your
application the wrong parser.

Is it possible to use XSLT to remove elements with duplicate values from an


XML file?

For example, given the following:

<example>
<element>A</element>
<element>B</element>
<element>A</element>
<element>C</element>
<element>B</element>
</example>
And produce the output (order of elements is not important, but uniqueness
is):
<example>
<element>A</element>
<element>B</element>
<element>C</element>
</example>

Location: http://www.jguru.com/faq/view.jsp?EID=548449
Created: Nov 15, 2001
Author: Nicolás Lichtmaier (http://www.jguru.com/guru/viewbio.jsp?EID=507049)
Question originally posed by Peter Mularien
(http://www.jguru.com/guru/viewbio.jsp?EID=82909
XSLT does not currently provide a direct way of extracting unique nodes from a
nodeset but that feature can be emulated. The trick would be to ask at each node if
there has been another one. This would work:
<xsl:template match="@*|node()">
<xsl:if test="not(node()) or not(preceding-
sibling::node()[.=string(current())])">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>

Comments and alternative answers

unique nodes
Author: Andrei Melejik (http://www.jguru.com/guru/viewbio.jsp?EID=525058), Dec
3, 2001
Another way is to use generate-id() function. Ids can be compared as strings. So that
you can filter out all the nodes but the one with the highest (lowest) id.

duplicate nodes
Author: Marian Olteanu (http://www.jguru.com/guru/viewbio.jsp?EID=453227), Jan
20, 2002
The code that you post looks only for the string value of the nodes. If you want to
look for the name, too:
<xsl:template match="@*|node()">
<xsl:if test="not(node()) or not(preceding-
sibling::node()[.=string(current()) and name()=name(current()])">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
Anyway, "duplicate" must be defined better.

What are Xbeans?


Location: http://www.jguru.com/faq/view.jsp?EID=585356
Created: Dec 14, 2001
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12) Question
originally posed by Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12

An Xbean is a software component that takes XML as input, processes it in some


fashion and then passes XML on to the next Xbean.

Xbeans are Java Beans. Java Bean technology supports the packaging, reuse,
connection and customization of Java code. With the appropriate set of Xbeans and a
Java Bean design tool, it is possible to build useful distributed applications with little
or no programming!

What else do I need to use Xbeans?


Location: http://www.jguru.com/faq/view.jsp?EID=585358
Created: Dec 14, 2001
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12) Question
originally posed by Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12

You need an implementation of the DOM and XSLT. Most Xbeans are users of the
DOM interface. They do not depend on a particular DOM implementation. The
serializer Xbean currently depends on an extension in Xerces.

We included the Xerces and Xalan implementations of the DOM and XSLT with the
Xbeans release. You can try others.

Why do most Xbeans only support a single listener?


Location: http://www.jguru.com/faq/view.jsp?EID=585360
Created: Dec 14, 2001
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12) Question
originally posed by Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12

The DOMSource interfaces defines two operations, getDOMListener() and


setDOMListener(DOMListener next), for getting and setting the next Xbean, i.e. the
Xbean that will receive the output of the Xbean. While one could imagine operations
to set multiple listeners, most Xbeans only directly support the registration of a
single listener.

The reason for this is if an Xbean has multiple listeners for its resulting document,
the document can either be a shared, concurrently accessed document or it can be
copied for each listener. The appropriate behavior is usually not dictated by the
source Xbean but by the application itself. For example, if the multiple listeners all
read the document without modifying it, they can easily share the resulting
document. Only the application knows that the Xbeans were configured this way.

To make this explicit in the chaining of Xbeans into an application, the parallelizer
Xbean supports multiple listeners. It contains properties to indicate whether the
document should be concurrently shared among the multiple listeners or whether the
document should be copied. In case the document is concurrently shared, the
synchronizer Xbean can be used to wait until all concurrent Xbeans are done.

How can I create a distributed application out of Xbeans?


Location: http://www.jguru.com/faq/view.jsp?EID=585361
Created: Dec 14, 2001
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12) Question
originally posed by Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12

The sender and receiver Xbeans support the passing of XML documents across
process boundaries. There are currently three implementations of sender and
receiver using different approaches for the transport of XML. Included are an HTTP
implementation, a CORBA implementation and a RMI implementation.
How can I create an application out of Xbeans?
Location: http://www.jguru.com/faq/view.jsp?EID=585362
Created: Dec 14, 2001
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12) Question
originally posed by Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12

Source Xbeans have a DOMListener property, as specified in the DOMSource


interface. Thus connecting Xbeans simply means setting the property to be the next
Xbean. Source Xbeans invoke documentReady on the next Xbean.

Xbeans are configured using the standard Java Bean mechanisms of property editors
and customizers. Using a Java Bean design tool, such as IBM's Visual Age for Java,
Borland's JBuilder, Symantec's Visual Cafe, FreeBuilder, NetBeans or the planned
Xbean.org configuration tool, a developer can visually instantiate, customize and
connect Xbeans. Complete applications can be created, often without writing any
code.

How can I contribute to the Xbeans project?


Location: http://www.jguru.com/faq/view.jsp?EID=585363
Created: Dec 14, 2001
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12) Question
originally posed by Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12

There are basically three ways: you can implement a useful Xbean and contribute it
to the project, you can improve an existing Xbean and contribute the changes and
you can provide us with feedback. See xbeans.org for more information.

What Xbeans are currently available for download?


Location: http://www.jguru.com/faq/view.jsp?EID=585418
Created: Dec 14, 2001
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12) Question
originally posed by Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12

The second release is now available at xbeans.org. It includes the following Xbeans:

translator

viewer

logger

serializer

parser

synchronizer

HTTP sender-receiver

RMI sender-receiver
CORBA sender-receiver

timer

memory meter

sample

Are there any web sites that will validate the well-formedness of an XML
document?
Location: http://www.jguru.com/faq/view.jsp?EID=747502
Created: Feb 5, 2002
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7) Question
originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

The Scholarly Technology Group (STG) has an online validatform format at


http://www.stg.brown.edu/service/xmlvalid/. You can paste the document in the
form or provide a URL/URI to validate the online document.
Comments and alternative answers

another link
Author: Anil Terli (http://www.jguru.com/guru/viewbio.jsp?EID=756495), Feb 12,
2002
There is another link from the ibm developer works that does the validation of an
XML. http://www.networking.ibm.com/xml/XmlValidatorForm.html

Dynamic DOCTYPE generation I want to generate an XML using JAXP


parsers and i want to include a DOCTYPE tag that references a dtd. how can
i do that? can anyone please help me out?
Location: http://www.jguru.com/faq/view.jsp?EID=768296
Created: Feb 22, 2002
Author: Bruce Martin (http://www.jguru.com/guru/viewbio.jsp?EID=12) Question
originally posed by Ravi Kiran
(http://www.jguru.com/guru/viewbio.jsp?EID=762215

The DOM API allows for the creation of a DOCTYPE. The DOMImplementation
interface defines a createDocumentType() method. The DOMImplementation
interface can be obtained from a Document.
Comments and alternative answers

But it does not work.


Author: John Chen (http://www.jguru.com/guru/viewbio.jsp?EID=977680), Aug 6,
2002
I find a example to Dynamic DOCTYPE generation in
http://cafeconleche.org/books/xmljava/chapters/ch10s03.html.

Example 10.8. A DOM program that outputs the Fibonacci numbers as a MathML
document

But the out put does'nt include what I want,ie.

<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN"


"http://www.w3.org/TR/MathML2/dtd/mathml2.dtd">

Could someone help me to include the doctype in the generated xml? Thanks in
advace.

Re: But it does not work.


Author: mitesh Jani (http://www.jguru.com/guru/viewbio.jsp?EID=1045329), Jan
10, 2003
Hi John Chen,

While trying to find a solution for the exact mentioned problem I came upon this
site. Site URL is :
http://www.javazoom.net/services/newsletter/xmlgeneration.html

Try running the (4) example. I think it will solve your problem of including the
<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN"
"http://www.w3.org/TR/MathML2/dtd/mathml2.dtd">

ALL THE VERY BEST!

What is the best way to generate a universally unique object ID? Do I need
to use an external resource like a file or database, or can I do it all in
memory?
Location: http://www.jguru.com/faq/view.jsp?EID=1030397
Created: Nov 25, 2002 Modified: 2003-02-28 08:01:34.258
Author: Alessandro A. Garbagnati
(http://www.jguru.com/guru/viewbio.jsp?EID=32727) Question originally posed by
Andy Brown (http://www.jguru.com/guru/viewbio.jsp?EID=1027054

[I need to generate unique id's that will be used for node 'ID' attribute values within
XML documents. This id must be unique system-wide. The generator must be
available to a number of servlets that add various node structures to my XML docs as
a service. What is the best way to tackle this? The 'possible' ways I can see:

• Keep the maximum ID value in a flat-file where the service would read it upon
start-up and increment it. Upon shutdown or failure, it would write the latest
max id to the file.
• Calculate the max id by searching the XML itself. This will be tougher since
XML requires an alpha-numeric value (not strictly numeric).
• Use a database (MySQL) with a two-field table where one field is the
incremental counter.
I just have this feeling that none of the above are the most efficient ways of doing
this.

Regards, -Andy]

There is an additional way to do that that doesn't rely on an external file (or
database) like the one you have presentred. If has been presented in the EJB Design
Patterns book, written by Floyd Marinescu, and available in a pdf format for free from
the given link.

The suggested solution is based on the UUID for EJB pattern, that comes out from
this question:

How can universally unique primary keys can be generated in menory without
requiring a database or a singleton?

Without enetring in the specifics (you can fully check out the pattern by reading the
appropriate chapter), the solution is to generate a 32 digit key, encoded in
hexadecimal composed as follows:

1: Unique down to the millisecond. Digits 1-8 are are the hex encoded lower 32 bits
of the System.currentTimeMillis() call.

2: Unique across a cluster. Digits 9-16 are the encoded representation of the 32 bit
integer of the underlying IP address.

3: Unique down to the object in a JVM. Digits 17-24 are the hex representation of
the call to System.identityHashCode(), which is guaranteed to return distinct
integers for distinct objects within a JVM.

4: Unique within an object within a millisecond. Finally digits 25-32 represent a


random 32 bit integer generated on every method call using the cryptographically
strong java.security.SecureRandom class.

[See also the following FAQs:

• I need to generate a GUID and have seen suggestions about using an RMI
server but nothing about how to actually generate the GUID itself.
• What is the best way to provide a unique identifier as a primary key that will
work in a database independent manner? I'm looking for functionality similar
to Oracle's proprietary MY_SEQ.NEXTVAL.
• How do I automatically generate primary keys?
• and the original thread: What is the best way to implement a system-wide
object ID generator?

- Alex Chaffee]
Comments and alternative answers

Random class
Author: P Manchanda (http://www.jguru.com/guru/viewbio.jsp?EID=344357), Feb
28, 2003
Hi,
Try using the java.util.Random class to generate random numbers that can be used as
IDs.

Re: Random class


Author: Alessandro A. Garbagnati
(http://www.jguru.com/guru/viewbio.jsp?EID=32727), Feb 28, 2003
Hi,
java.util.Random will generate random numbers, not unique numbers.

Re[2]: Random class


Author: Prashanth Nandavanam
(http://www.jguru.com/guru/viewbio.jsp?EID=818802), Apr 24, 2003
Look at the JUG class by Tatu Saloranta:
http://www.doomdark.org/doomdark/proj/jug/index.html It is based on the
IETF UUID drafts, and works wonderfully.

Re: Random class


Author: Nathan Ciliberto (http://www.jguru.com/guru/viewbio.jsp?EID=1139136),
Jan 14, 2004
You can also use some built-in java classes:
java.rmi.server.UID
or
java.rmi.dgc.VMID
I'm guessing the UID class is the one you want though.

(BTW,you don't need to use rmi nor know anything about rmi to use these classes)

-Nathan

Making it Faster
Author: Kimbo Mundy (http://www.jguru.com/guru/viewbio.jsp?EID=1120338), Oct
8, 2003
It seems to me that you could speed up the algorithm above by modifying step #4.
Instead of computing the random number every time, just compute it the first time,
and then increment it after that. 2 different JVMs should still just have a 1 in 4 billion
chance of overlap.

Re: Making it Faster


Author: Scott Carlson (http://www.jguru.com/guru/viewbio.jsp?EID=1085622),
Oct 8, 2003
There are two reasons to use the random number instead of incrementing your
last. 1. The number would be predictable and, depending on what this is used for,
you could be opening up a potential security issue. This is why ProcessIDs are
randomized on some OSes (AIX for one). 2. You must synchronize on that counter
to guarantee that your number isn't reused. Your random number generator need
not be synchronized, (though its implementation may be).

There are Three possible ways


Author: Narayana Prasad (http://www.jguru.com/guru/viewbio.jsp?EID=1142103),
Jan 29, 2004
1) If ur using Oracle You can create a sequence ,by which u can generate unique
primary key or universal primary key. 2) U can generate by using random nunmbers
but u may have to check the range and check for unique id. ie random number
generate 0.0 to 1.0 u may have to make some logic which suits ur unique id 3) Set the
maximum value into an XML file and read that file at the time of loding ur
application from xml . thanks and regards prasad

UUID and Random IDs


Author: Wesley Theobalds (http://www.jguru.com/guru/viewbio.jsp?EID=1165144),
Apr 22, 2004
Hi, I've just finished implementing the Sequence Block pattern a la Marinescu using a
Session bean and an entity bean representing the sequence, and its working well.
Using a UUID makes life harder for any Data analysis further down the line, having
to enter a 32 bit number for the key for any ad hoc SQL queries you may need to
write, and I've often found it helpful to have an incremental key as it indicates when
the record was created in the absence of a timestamp.

hashcode isn't unique


Author: x x (http://www.jguru.com/guru/viewbio.jsp?EID=1174058), May 27, 2004
> System.identityHashCode(), which is guaranteed to return > distinct integers for
distinct objects within a JVM. Sorry, but it isn't true. See
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4990451 Tomas

Millisecond overlap
Author: Thomas Paré (http://www.jguru.com/guru/viewbio.jsp?EID=1238395), Apr
14, 2005

Thanks for the very interesting pointer on 'Ejb design patterns'. I've read the
implementation details and found something troublesome.

In step 1 : "Unique down to the millisecond. Digits 1-8 are are the hex encoded lower
32 bits of the System.currentTimeMillis() call". Only the lower 32 bits of time are
considered, which makes the uniqueness of that part only valuable for a period of 50
days. Looks like a serious issue.

JAXB, should one start using the API in a live project considering the fact
that it is still in its beta? JAXB, should one start using the API in a live
project considering the fact that it is still in its beta? This will help me avoid
a lot of unnecessary code.
Location: http://www.jguru.com/faq/view.jsp?EID=1061579
Created: Feb 27, 2003
Author: Ivo Limmen (http://www.jguru.com/guru/viewbio.jsp?EID=327483)
Question originally posed by Mahendra Goyal
(http://www.jguru.com/guru/viewbio.jsp?EID=331192

JAXB is no longer a beta product. The latest version can be downloaded from Sun
packed within the Java Web Services Developer Pack 1.1. If it is a package from Sun
you can always trust the fact that the API of the early access version will be the very
much the same as the API of the official release. A lot of open source packages to
not maintain that principle but there are exceptions: Struts, Tomcat, ect.

How do I produce documents with HTML embedded in XML I have XML


elements that have HTML tags embedded inside.

I need to produce documents that carry those tags from the XML to the
output document.

I am using MSXML for processing.

Thanks

Andy
Location: http://www.jguru.com/faq/view.jsp?EID=1061580
Created: Feb 27, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Andy Miscuk
(http://www.jguru.com/guru/viewbio.jsp?EID=1058721

If you don't need to validate it, you have at least two choices

• Use xhtml tags only, which are valid XML.


• Use CDATA section

Populating documents with xml I want convert on a group of legal


documents (plain text) into xml documents that can be populated with with
user information (names, addresses, etc.). From what i have read, XML
Schema is the way to go. any suggestions?
Location: http://www.jguru.com/faq/view.jsp?EID=1062062
Created: Mar 1, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Bryan thomas
(http://www.jguru.com/guru/viewbio.jsp?EID=1060020

You can design/constrain your upcoming XML files with DTD or schema. However,
you still need to write your own text file parser, to simply read line by line to convert
your legal documents to the desired XML documents.

This is because only you know your text file structure, and what content you want to
put where in your XML and under which tag.

Comments and alternative answers


Converting text files to XML
Author: Valery Bedenko (http://www.jguru.com/guru/viewbio.jsp?EID=978757), Mar
4, 2003
Check out the tool, CredibleXML, available at http://www.crediblexml.com . This
tool lets you define the format of your input text documents, and generates java parser
/ text-to-XML conversion library that can be used to convert the documents into XML
programmatically.

Re: Converting text files to XML


Author: Valery Bedenko (http://www.jguru.com/guru/viewbio.jsp?EID=978757),
Mar 4, 2003
Sorry, correct link is http://www.crediblexml.com

Performing conditional checks when doing XSLT.


Q. How to add element hasTagA to the result XML at the condition one or
more tagA present in the source XML?
Location: http://www.jguru.com/faq/view.jsp?EID=1064257
Created: Mar 7, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Juha Siltanen
(http://www.jguru.com/guru/viewbio.jsp?EID=1064087

In XSLT, we has xsl:if xsl:choose xsl:when xsl:otherwise to perform conditional


check. However, we can also use template with match attribute to perform
conditional check. With the help of xpath predicate, it can do very powerful
conditional check too. Here is an example.

XML sourece

<root>
<tagA />
<tagB />
<tagC />
<tagA />
</root>
XSLT sourece
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<newroot>
<xsl:apply-templates />
</newroot>
</xsl:template>
<xsl:template match="*">
<xsl:copy-of select="." />
</xsl:template>
<!-- do something only on the first one -->
<xsl:template match="tagA[1]" >
<hasTagA>
Do something here
</hasTagA>
</xsl:template>
<!-- do nothing on the rest -->
<xsl:template match="tagA[position() != 1]" />
</xsl:stylesheet>
XML result
<newroot>
<hasTagA>
Do something here
</hasTagA>
<tagB/>
<tagC/>
</newroot>
HTH! :)

Can I used a arbitrary defined DTD to generate all possible XML templates?
Location: http://www.jguru.com/faq/view.jsp?EID=1064578
Created: Mar 9, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by layout crazy
(http://www.jguru.com/guru/viewbio.jsp?EID=1064506

The answer is NO unless it is an extreme trivial case. See the really trivial DTD
bellow. We already have 4 possible "templates". Here we assume templates is the
right word here. I use one line for each XML file for easy listing. For a little less
trivial DTD, it is easy to imagine that all the combination and permutation will cause
an exponential explosion. I don't think you want the result, let alone having the
space to store the result or writing an application to use that result.

Trivial DTD file

<!ELEMENT person (name,age,sex)>


<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
Four possible XML "templates"

1. <name>some_name</name>
2. <age>some_number</age>
3. <sex>one_of_the_two</sex>
4. <person><name>some_name</name><age>some_number</age><sex>one_of_t
he_two</sex></person>

If you don't believe all four are valid against the DTD, try to validate them by
yourself .

Defining SQL statements in the DTD How do I delcare my XML embeded SQL
statements in my DTD? See original question here
Location: http://www.jguru.com/faq/view.jsp?EID=1064704
Created: Mar 10, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Richard Cunliffe
(http://www.jguru.com/guru/viewbio.jsp?EID=1064616

Good question! However, it is actually a question about how to incorporate


namespace into the namespace-unaware-dtd file. The w3c was/is very smart, and
made dtd can work with namespace, even it does not know anything about it. The
secret is the ":" is a legal character of XML name.

I've a example about this topic, take a look, play with it a little. I believe you will get
it! That was also how I got it.

Q. Can I specify namespace in DTD validation? If yes, give me an example.

How to add existing JDOM/DOM elements to a document object? See


original question here.
Location: http://www.jguru.com/faq/view.jsp?EID=1065559
Created: Mar 12, 2003 Modified: 2003-03-12 15:09:34.327
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Brett Astleford
(http://www.jguru.com/guru/viewbio.jsp?EID=1065453

In JDOM, use (org.jdom.Element)org.jdom.Element.clone() will solve your


problems. JDOM clone is a deep clone and it will forget its parent etc.

In DOM, you need to use

org.w3c.dom.Document.importNode(node, true)
to make the node forget its parent and switch the container Document.

Create multiple HTML files using one source XML and XSLT I would like to
generate different HTML files for 'pages' that are described in a single XML
source. Is there any way to create a new result tree in XSL for a new file? Or
is there some other technique? Thanks in advance, Brian
Location: http://www.jguru.com/faq/view.jsp?EID=1066373
Created: Mar 14, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Brian Barran
(http://www.jguru.com/guru/viewbio.jsp?EID=1065124

I've a sample code available here.

Q. Can I tranform one single XML to several html files? For example, I want to
transfer each html node to an individual html file.

The opposite direction example is also available on the same page.

Keyword xsl:script may not be used in namespace Hey, Im not an expert in


XSLT but hope that somebody may have come accross this issue before. I
try to use this utility file which contains the following fragment:
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:svgu="http://www.ora.com/XSLTCookbook/ns/svg-utils"
xmlns:emath="http://www.exslt.org/math"
xmlns:Math="java:java.lang.Math" extension-element-prefixes="Math"
exclude-result-prefixes="svgu"> <xsl:script implements-prefix="Math"
xmlns:Math="java:java.lang.Math" language="java"
src="java:java.lang.Math"/> When I try to incorporate into XML and display
in IE 6.0 I gett the following error.... Keyword xsl:script may not be used in
namespace http://www.w3.org/1999/XSL/Transform. Error occurred
during compilation of included or imported stylesheet
'file:///C:/disks/XSLT Tutorials and Code Samples/XSLT
Cookbook/XSLTCkBkCode/svg/svg-utils.xslt'. Thank you, Rafal
Location: http://www.jguru.com/faq/view.jsp?EID=1067162
Created: Mar 17, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Rafal Kulinski
(http://www.jguru.com/guru/viewbio.jsp?EID=874564

msxml:script is a Microsoft msxml extension, and should not be put into xsl
namespace. Someone might manipulate it into their own defined xsl namespace does
not mean you can use it generally.

w3c was try to add script as a XSL1.1 element, but there is a petition to withdraw it.
What is the final status for that? I don't know.

Namespaces, SOAP and XSLT I am performing XSLT to a SOAP message and


the output is another SOAP meage, slightly different in content. I'll include
here the relevant parts concerning my question. In the result SOAP, some
tag's have a namesoace declared even though no namespace is defined in
the source documetn for those specific tags.

The source:

<?xml version="1.0" encoding="UTF-8"?>


<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header>
<parameters xmlns="http://www.sonera.com/portal/pcs/v3.1">
<transactionId>12345</transactionId>
<origin>Somewhere</origin>
<destination>Somewhere else</destination>
</parameters>
</env:Header>

<env:Body>
<update xmlns="http://why.is.this.url.repeated.in.the.result">
<products>
<SomeProduct>
<ID>1</ID>
<status>active</status>
<username>someUser</username>
<password>myPassword</password>
</SomeProduct>
</products>
</update>
</env:Body>
</env:Envelope>

The XSL:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" encoding="utf-8" />
<xsl:template match="/">
<env:Envelope xsl:version="1.0"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

<env:Header>
<xsl:element name="parameters">
<xsl:copy-of select="//*/*['Header'][1]/parameters/*"/>
</xsl:element>
</env:Header>

<env:Body>
<xsl:element name="update">
<xsl:element name="Product">
<xsl:copy-of select="//*/*['Header'][1]/transactionId"/>
<xsl:copy-of select="
//*/*['Body']/*/*['Products']/*[1]/*[not(name()='password' or
name()='username')]"/>
</xsl:element>
</xsl:element>
</env:Body>
</env:Envelope>
</xsl:template>
</xsl:stylesheet>

The result:

<?xml version="1.0" encoding="utf-8" ?>


<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header>
<parameters />
</env:Header>
<env:Body>
<update>
<Product>
<ID xmlns="http://why.is.this.url.repeated.in.the.result">12345
</ID>
<status xmlns="http://why.is.this.url.repeated.in.the.result">active
</status>

As it is depicted in the presented codes, why is this


url presented in the output? I haven't defined it to
appear there (or have I?).

Thanks, I really apprechiate all the help I get :-)


-Juha-
Location: http://www.jguru.com/faq/view.jsp?EID=1067836
Created: Mar 19, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Juha Siltanen
(http://www.jguru.com/guru/viewbio.jsp?EID=1064087

Because XSLT is doing exact the right thing! In your source, everything in your
<update> tag belong to "http://why.is.this.url.repeated.in.the.result" name space.
Therefore, those tags in your result should belong to the same namespace.

How do I change it to be as it is specified in my specification document,


where the namespace appears only in the update-tag and not in the tags
under it?

Do one of the following two will reach you "goal". The point is make those tags do
not belong to the same namespace. However, I highly suspect that is what you really
"want".

• Put xmlns="" in all tags under <update xmlns="http:..."> in you soap XML
source
• Change it to:
• <mytag:update
xmlns:mytag="http://why.is.this.url.repeated.in.the.result">
• <products>
• <SomeProduct>
• <ID>1</ID>
• <status>active</status>
• <username>someUser</username>
• <password>myPassword</password>
• </SomeProduct>
• </products>
• </mytag:update>
Both efforts are trying to make them belong to no namespace. However, if you have
default namespace defined on higher level, the second choice will make them belong
the higher-level default namespace. That is not the case for the source file you
provided. It is just a precautious warning.

How to pass value to XSLT stylesheet during run time?


I have declared xsl:varaible in one place,after that i want to assin a value
accoriding to some codtion,but i don't know how to assing value to variable.
one way is we can assing value like this.. <xsl:variable name="var"
select="root/sub"> somthing like this.. but i want to assi value for above
variable var in run time..
Location: http://www.jguru.com/faq/view.jsp?EID=1068527
Created: Mar 21, 2003
Author: Jan Matèrne (http://www.jguru.com/guru/viewbio.jsp?EID=567992)
Question originally posed by ethiraj krishnan
(http://www.jguru.com/guru/viewbio.jsp?EID=1041659

You have to use parameters.


XSL stylesheet

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="date">DEFAULT-DATE</xsl:param>

...

Calling via Ant

<xslt in="${xml}" out="${out}" style="${xsl}">


<param name="date" expression="${DATE}"/>
</xslt>

If you're using Java, then you can also use


javax.xml.transform.Transformer.setParameter(name, val);
to do the same.

DOCTYPE and "public identifier" URI I develop a web application and in the
web.xml file I must indicate a DTD. For the moment I have the following
declaration: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD
Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> I
would like to know how is it possible to point to a local copy of the resource
for the DTD, instead of the remote one at java.sun.com ? I use Tomcat 4.0.1
as web server.
Location: http://www.jguru.com/faq/view.jsp?EID=1069080
Created: Mar 24, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by HELIGON Sandra PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=823885

HELIGON Sandra:

I just tried all possible options I could think of. You will have more than one choices
to do what you want. Agreed, the worst choice would be the hard-coded system
dependent path.

Here we are (I assume you put web-app_2_3.dtd in webapps/yourapp/WEB-INF/


directory):

• <!DOCTYPE web-app SYSTEM "web-app_2_3.dtd">


• <!DOCTYPE web-app SYSTEM "/WEB-INF/web-app_2_3.dtd">
• <!DOCTYPE web-app SYSTEM
"file:/home/yourname/tomcat4.1.18/webapps/yourapp/WEB-INF/web-
app_2_3.dtd">
!!!Avoid this one, please!!!

I want to create an XML file from a comma delimited text file I have a .txt
file which contains some atomic values separated by commas. From this .txt
file I want to create an XML file of those values. How do I do this ? Can I get
the code also ?
Location: http://www.jguru.com/faq/view.jsp?EID=1070027
Created: Mar 26, 2003
Author: Jan Matèrne (http://www.jguru.com/guru/viewbio.jsp?EID=567992)
Question originally posed by Arun V
(http://www.jguru.com/guru/viewbio.jsp?EID=1069860

Three possibilities:

1. convert the file manually


2. write a generator which will do the convertion once
3. use an adapter

I did a quick hack in perl for the generator:


print "<values>\n";
foreach (<STDIN>) {
chomp;
s/,/<\/value>\n <value>/g;
print " <valuelist>\n <value>$_</value>\n </valuelist>\n";
}
print "</values>";
You can use that for starting. If you want write an adapter I recommend looking at
chaperon (i never used it, but sounds good).

Roseanne Zhang added:


You also can use Java, just read one line at a time. Then using a StringTokenizer to
get one token at a time. Do the same thing Jan does in perl. Add a root, put each
token into a child tag. It is simple and easy because your data is structured.

How do I use XLST to parse through a delimited string value? How do I


transform the following?

lt;root>
<a>aaa|bbb|ccc|vvv|abc|</a>
</root>
I want this result
<root>
<a1>aaa</a1>
<a2>bbb</a2>
<a3>ccc</a3>
<a4>vvv</a4>
<a5>abc</a5>
</root>

Location: http://www.jguru.com/faq/view.jsp?EID=1070072
Created: Mar 26, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Kenneth Chin
(http://www.jguru.com/guru/viewbio.jsp?EID=1060452
It is not easy, but can be done. It also requires recursion. You probably can do better
than I did, but here is a solution anyway. It might not be the greatest, but it works.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/*" >
<xsl:element name="{name()}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="a" >
<xsl:call-template name="proc" >
<xsl:with-param name="num" select="1" />
<xsl:with-param name="txt" select="text()" />
</xsl:call-template>
</xsl:template>
<xsl:template name="proc">
<xsl:param name="num" select="0"/>
<xsl:param name="txt" select="''" />
<xsl:element name="{concat('a', $num)}" >
<xsl:choose>
<xsl:when test="contains($txt, '|')" >
<xsl:value-of select="substring-before($txt, '|')"
/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$txt" />
</xsl:otherwise>
</xsl:choose>
</xsl:element>
<xsl:variable name="left" select="substring-after($txt, '|')" />
<xsl:if test="string-length($left)>1" >
<xsl:call-template name="proc" >
<xsl:with-param name="num" select="$num + 1" />
<xsl:with-param name="txt" select="$left" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Differences in XML files(Urgent) If i have two xml files with the same dtd
do i have any mechanism to compare those two files
and to know the changes like whether any tag is
deleted or updated or newly added.
How can i do this in common way.
Thanks in Advance
Rizwan Md.
Location: http://www.jguru.com/faq/view.jsp?EID=1070454
Created: Mar 27, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Mohammad Rizwan
(http://www.jguru.com/guru/viewbio.jsp?EID=989793

• IBM has an applet.


• MicroSoft has an exe.
• There are many open source ones on the web.
• Do a search, you will find them.
• Be prepared, they are all buggy.

In theory, this is the best article for the topic so far, but it is not easy to read or
implement: http://citeseer.nj.nec.com/449452.html

Enjoy!

Comments and alternative answers

Compare XML files


Author: Andrey Lapick (http://www.jguru.com/guru/viewbio.jsp?EID=1177039), Jun
9, 2004
Please try ExamXML at http://www.a7soft.com thanks.

Xerces to JAXP migration We are moving from JDK 1.2 to JDK 1.3. The XML
parser are also upgraded from Xerces to JAXP. Our application code uses
XML extensively and uses inhouse XMLParcers. I was wondering what code
chages do you think is involved in moving from Xerces to JAXP. Do anyone
have any pointers as to what changes would be. Any input is appreciated.
Thanks.
Location: http://www.jguru.com/faq/view.jsp?EID=1070755
Created: Mar 27, 2003 Modified: 2003-03-27 18:40:51.954
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by krishnan meenakshi
(http://www.jguru.com/guru/viewbio.jsp?EID=2858

1. You have concepts problems. JAXP is just standard interfaces for you to use
any JAXP compliant Java parser. And xerces is just one of them
2. When you move to JAXP, you're still using xerces as your parser unless you
want to switch vendors.
3. What you need to do? Move your code to most recent stable build xerces first.
Make as less changes as possible. Test your application during the move,
don't break your application. If something does not work, fix it.
4. Move to JAXP after that, actually not much left. Search the files for xerces.
Change them to its parent interfaces or superclass in JAXP. Change one at a
time. Don't break your application any time.
5. You need to set a system property, otherwise, your application will be
compilable, but not work.
6. -Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser
7. If some method does not exist in JAXP or work differently in JAXP, then you
need to do more your own work to make it work in JAXP. Be conservative and
creative.
8. Good luck!

Why I know it? Since I've done it in a slight different scenario, but pretty similar.

How to reorder xml elements in the invalid XML according to the definition
of DTD?
I have some xml documents where elements appear in different order than
specified by dtd/schema, thus are not valid xml documents. What is the
best way to reorder them so they are valid? Thanks.
Location: http://www.jguru.com/faq/view.jsp?EID=1071020
Created: Mar 28, 2003 Modified: 2003-03-28 10:47:24.345
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Cheng Fang
(http://www.jguru.com/guru/viewbio.jsp?EID=485380

XSLT will not work, since xsl does not read DTD. I search the web, and not found
anyway to enforce DTD on invalid XMLs. The reason probably very similar to this one

Q. Can I used a arbitrary defined DTD to generate all possible XML templates?

I'm not saying it is impossible, but it might be extremely difficult.

• By hand is one way that will always work, if only once.


• Use a validating editor will greatly speed up your hand process.
• If the amount is large, but somewhat repeatable, and also you have sufficient
knowledage about your XML and DTD, then
1. use a Java or Perl program read the know DTD, get the order
2. Read the xml, do text processing (recommended) or use DOM or JDOM
to process your XML document.

Good luck!

I have a Servlet and a Servlet Filter that is supposed to apply an xsl to it.
The transformation appears to work, I get back a transformed document,
but it appears (in IE) as if IE thinks its an HTML document (i.e., you see the
<html><body>, etc tags). Viewing source, and saving as an HTML file will
display correctly. How can I correctly set the Content Type of the response?
Location: http://www.jguru.com/faq/view.jsp?EID=1074630
Created: Apr 8, 2003
Author: Vincent Fischer (http://www.jguru.com/guru/viewbio.jsp?EID=1035285)
Question originally posed by Vincent Fischer
(http://www.jguru.com/guru/viewbio.jsp?EID=1035285

I figured this out. Many thanks go out to Matan Amir for pointing me in the right
direction.

I used Ethereal (I couldn't get tcptrace to work), to verify that in fact the content
type was "text/xml", instead of "text/html". So, I finally figured out that instead of:

response.setContentType("text/html");
I wrote:

wrapper.setContentType("text/html");

I'm guessing the reason is because the response and request objects in the Filter
cannot be modified... So instead, I made the changes to the wrapper. Of course, if
I'd set my servlet to send the response as "text/html" this would work too, but the
purpose of the assignment was to change the contentType...

[See http://www.jguru.com/forums/view.jsp?EID=1072839 for the source code and


more discussion. -Alex]

[Todo: add "Filters" category; add this to it]

How to write xslt extended method? Can you give an example? The
following of my code does not work.

java code:
import javax.xml.parsers.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.io.File;
import java.util.Date;
import org.w3c.dom.*;

public class XSLOBJ {


public static String getDate() {
Date d = new Date();
return d.toString();
}
public XSLOBJ(){}
public static void main(String[] args){
try{
DocumentBuilderFactory bdf = DocumentBuilderFactory.newInstance();
DocumentBuilder bd = bdf.newDocumentBuilder();
Document doc = bd.newDocument();
Element root = doc.createElement("Root");
Element date = doc.createElement("Date");

root.appendChild(date);
doc.appendChild(root);
DOMSource source = new DOMSource(doc);

StreamSource xslSource = new StreamSource("date.xsl");


TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer(xslSource);

StreamResult target = new StreamResult("result.xml");


t.transform(source,target);
}catch(Exception e){System.out.println(e);}
}
}
=================================================
====
xslt code:
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns:myObject="java:XSLOBJ" version ="1.0">
<xsl:variable name="contextObj" select="myObject:new()" />
<xsl:template match = "@*|*">
<xsl:copy>
<xsl:apply-templates select = "@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Root/Date">
<xsl:element name = "Date">
<xsl:value-of select="myObject:getDate($contextObj)" />

</xsl:element>
</xsl:template>
</xsl:stylesheet>
=======================================
runtime exception:
file:/E:/working/XML/classes/date.xsl; Line 3; Column -1;
javax.xml.transform.TransformerException: Instance
method call to method new requires an Object instance
as first argument
=================================================
===========
Please help me to resolve this problem.Thanks.
Location: http://www.jguru.com/faq/view.jsp?EID=1075928
Created: Apr 12, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by TPZ Stamp
(http://www.jguru.com/guru/viewbio.jsp?EID=465268
What you want to do does not even require a customized java extention. Simply
using java.util.Date is enough. However, if you want to format you date string
properly, then you do need to write your own class.

I found some time, and wrote 2 examples. One does what you wanted to do. The
other does more.

• A simple Java extension example


You can apply it to any well-formed XML file. It is not a very good practice,
since you must use transformer knowing Java. The better practice would be
using Microsoft msxml. However, I need to learn it before giving an example.
The next FAQ allows you to change the date string format in your XSLT.
<h5>XSLT sourece </h5>
• <xsl:stylesheet version="1.0"
• xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
• xmlns:java="java">
• <!-- add a today-date to your XML under the root -->
• <xsl:template match="/*">
• <xsl:element name="{name(.)}">
• <today-date>
• <xsl:value-of select="java:util.Date.new()" />
• </today-date>
• <xsl:apply-templates />
• </xsl:element>
• </xsl:template>

• <!-- identity template -->
• <xsl:template match="@*|node()">
• <xsl:copy>
• <xsl:apply-templates select="@*|node()"/>
• </xsl:copy>
• </xsl:template>
</xsl:stylesheet>

• A customized Java extension example


You can apply it to any well-formed XML file. The date string format can be
provided by <xsl:param> if desired. <h5>XSLT sourece </h5>
• <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
• xmlns:xobj="com.example.XObj"
• version ="1.0">
• <!--<xsl:variable name="contextObj" select="xobj:new(.)" />-->
• <xsl:template match = "@*|node()">
• <xsl:copy>
• <xsl:apply-templates select = "@*|node()"/>
• </xsl:copy>
• </xsl:template>
• <xsl:template match="/*">
• <xsl:element name="{name(.)}" >
• <xsl:element name = "today-date">
• <xsl:value-of select="xobj:getDateString('EEE, MMM d,
yyyy')" />
• </xsl:element>
• <xsl:apply-templates />
• </xsl:element>
• </xsl:template>
</xsl:stylesheet>

<h5>Java Code</h5>

package com.example;
import java.util.Date;
import java.text.SimpleDateFormat;
public class XObj {
public static String getDateString(String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(new Date());
}
}
The code can also be found at XSLT Extension, advanced topics

Enjoy!!!

Comments and alternative answers

Java or JavaScript in a XSL file


Author: Rui Fernandes (http://www.jguru.com/guru/viewbio.jsp?EID=1081859), May
5, 2003
Hi. I am building a xsl file to transform files in GML to X3D. The problem is that i
have to include a script in the xsl file. What i want to do in the script is, build a matrix
in memory and put there some values. These values and the size of the matrix are in a
file ".txt", so i have to go there to read them. The values in the matrix are the
coordinate Z of a 'xyz graphic'. When i have a element X i have to go call a function
that returns the Z value. I am using XMLSpy version 5 release 3. Can you help me in
this problem? Thanks.

Xpath & namespaces, How to use namespace in xpath?

I have a problem with the XML Xerces parser when namespaces are used.

If I have just one defined namespace in the root of the document, and I use
a Xpath sentence to find one of the elements in this namespace, the parser
works.
But if there are more than one defined namespaces en the root, the same
Xpath sentence doesn´t find any element!

Here is the xpath and xml: When I search for:


//item/adlcp:datafromlms
or
//item/datafromlms
it doesn't work.

My xml is:

<?xml version="1.0" encoding="UTF-8"?>


<manifest identifier="1043" version="1.0" xml:base="/paquete"
xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"
xmlns:adlcp="http://www.adlnet.org/xsd/adl_cp_rootv1p1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_rootv1p1p2
imscp_rootv1p1p2.xsd http://www.imsproject.org/xsd/imsmd_rootv1p2
ims_md_rootv1p1.xsd http://www.adlnet.org/xsd/adl_cp_rootv1p1
adl_cp_rootv1p1.xsd ">
<metadata adlcp:location="/metadata" schema="schema"
schemaversion="version"/>
<organizations default="TOC1">
<organization identifier="TOC1" structure="hierarchical">
<item identifier="ITEM1" isVisible="true">
<title>2</title>
<item identifier="ITEM2" identifierref="RESOURCE1"
isVisible="true" parameter="parametros a pasarle si es un sco q hay q
lanzar ¿??????">
<title>2.1</title>
<metadata adlcp:location="adlcp:location de los
datos de la seccion" schema="schema de los metadatos de la seccion"
schemaversion="schema version de los datos de la seccion"/>
<adlcp:prerequisites type="aicc_script">
prerrequisitos </adlcp:prerequisites>
<adlcp:maxtimeallowed> maximo tiempo permitido
</adlcp:maxtimeallowed>
<adlcp:timelimitation> limitacion de tiempo
</adlcp:timelimitation>
<adlcp:datafromlms> datos de lms
</adlcp:datafromlms>
<adlcp:masteryscore> puntuacion maxima
</adlcp:masteryscore>
</item>

</item>
</organization>
</organizations>
<resources xml:base="/resources">
<resource adlcp:scormtype="sco" href="/file1.html"
identifier="RESOURCE1" type="webcontent" xml:base="/files"/>
<resource adlcp:scormtype="asset" href="/myImage.jpg"
identifier="RESOURCE2" type="webcontent" xml:base="/images"/>
</resources>
</manifest>
Thank you very much.
Location: http://www.jguru.com/faq/view.jsp?EID=1076028
Created: Apr 13, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Guido García
(http://www.jguru.com/guru/viewbio.jsp?EID=1011381
You need to understand how namespace and its prefix works to understand your
problem.

• All tags w/o prefix in your XML belong to the default namespace
"http://www.imsproject.org/xsd/imscp_rootv1p1p2"
• Prefix, such as "adlcp" must be defined in your xpath context before using.
• If you're using XSLT, it must be defined there. Otherwise, you need to use
namespace-uri() and local-name() functions.
• A simple example can be found here Q. How to handle attributes with
namespace in xpath?. Give it a try!

How do I display today's date in my XSL document Hi


Please can you help me, provide me with little example how to display
today's date in my XSL document.
I am creating on line letter using XSL and need date on it.
Many Thank

Location: http://www.jguru.com/faq/view.jsp?EID=1076214
Created: Apr 14, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Ismail Fadal
(http://www.jguru.com/guru/viewbio.jsp?EID=494628

If you're using Java based Transformer such as xalan, then I have two complete
examples for you:
• A simple Java extension example to display today's date
• A customized Java extension example to display today's date with specified
format

They can be found in this section of FAQ


http://bobcat.webappcabaret.net/javachina/faq/xslt_01.htm#xml_xslt_ex
Comments and alternative answers

Languages:Markup:XML:XSLT
Author: Dilip Beepyata (http://www.jguru.com/guru/viewbio.jsp?EID=1188055), Jul
22, 2004
Add this function in your java class - com.test.xslUtil;

public static String getDateString(String format) {


SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(new Date());
}

In your xsl add xmlns:xObj="com.test.xslUtil"


<xsl:element name = "today-date">
<xsl:value-of select="xObj:getDateString('MM/dd/yyyy HH:mm:ss')" />
</xsl:element>

How to pass output from JDOM to the next transformation etc. without
writing to a file? How to get output in JDOMStrem?
Location: http://www.jguru.com/faq/view.jsp?EID=1076223
Created: Apr 14, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by neil thompson PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=1053274

There is no such thing called JDOMStream. However, JDOM just uses all existing Java
IO classes as output tools, and extreamely flexible.

Take a look at the class org.jdom.output.XMLOutputter. It is very well documented,


and explained in detail. Search stream if you like.

How to concatenate XML files There are several XML files which have same
DTD file. I need to concatenate them into 1 file. Can anyone give me a hint?
Thanks
Location: http://www.jguru.com/faq/view.jsp?EID=1077360
Created: Apr 17, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Peter Zhang
(http://www.jguru.com/guru/viewbio.jsp?EID=1077199

Here is a working example with XML, XSLT, HTML results


http://bobcat.webappcabaret.net/javachina/faq/xslt_01.htm#xslt_ex_Q01

Enjoy!

Comments and alternative answers

merging inside jar file


Author: gaurav sharma (http://www.jguru.com/guru/viewbio.jsp?EID=1192992), Aug
31, 2004
Is there some other way than using XSLT to merger 2 XMl docs with same dtd. xPath
docuement() function doesnt work when classes and resources (including XMl files to
be merged) are bundeled in a jar file!..or does it?

String to XMLDocument I have an xml document stored in a String variable.


How do I take the String and parse it into an XMLDocument?
Location: http://www.jguru.com/faq/view.jsp?EID=1081258
Created: May 2, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Kenneth Chin
(http://www.jguru.com/guru/viewbio.jsp?EID=1060452

Q. How to parse an XML String?


A: Easy! No matter you using DOM (javax.xml.parsers.DocumentBuilder) or SAX
(org.xml.sax.XMLReader), or vendor specific SAXParser/DOMParser, you all can use
the same signature method parse(InputSource input) to parse you XML. Now the
question becomes how to use String to make an InputSource.

• Contruct a StringReader by using your XML String.


• Contruct an InputSource by using your StringReader
• Parse the InputSource by using parse(InputSource input)

Read javadoc for jaxp


Comments and alternative answers

NullPointerException
Author: camille espanol (http://www.jguru.com/guru/viewbio.jsp?EID=1248614), Jun
14, 2005
I have NullPointerException and don't know why.

nesting xsl:for-each I need to nest xsl:for-each elements (or do something


similar) to compare values to see if the inner element should be printed or
not. It would look something like this:

<xsl:for-each select="/loan/person">
<xsl:element name="borrower">

<<xsl:for-each select="/loan/money">
< <xsl:if test="">
< <<xsl:element name="income">
< <</xsl:element>
< <</xsl:if>
<</xsl:for-each>

</xsl:element>
</xsl:for-each>

Now, in the <xsl:if test=""> I need to compare a value in the /loan/person


element that I'm currently in to a value in every iteration through the
/loan/money (for-each) so that an income element is generated where
there is a match. Is this possible??? thanks. sjj
Location: http://www.jguru.com/faq/view.jsp?EID=1081264
Created: May 2, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by S Justin
(http://www.jguru.com/guru/viewbio.jsp?EID=1053813

Here is an complete answer for you, include the XMLs XSLT and analysis.

Q. Should I use nested loop in this problem?


A: Interesting one, see the XML source and required result XML.
XML sourece

<people>
<person>
<name>John Doe</name>
<ssn>111111111</ssn>
</person>
<person>
<name>Mary May</name>
<ssn>222222222</ssn>
</person>
<income>
<ssn>111111111</ssn>
<amount>33400</amount>
</income>
<income>
<ssn>111111111</ssn>
<amount>1000</amount>
</income>
<income>
<ssn>222222222</ssn>
<amount>25000</amount>
</income>
<income>
<ssn>333333333</ssn>
<amount>40000</amount>
</income>
</people>
Required XML result
<people>
<person ssn="111111111" name="John Doe">
<income amount="33400"/>
<income amount="1000"/>
</person>
<person ssn="222222222" name="Mary May">
<income amount="25000"/>
</person>
</people>

XSLT soureces for comparison


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:key name="incomekey"
match="/people/income"
use="ssn" />
<xsl:template match="/people" >
<people>
<xsl:apply-templates select="person" />
</people>
</xsl:template>
<xsl:template match="person" >
<person name="{name}" ssn="{ssn}" >
<xsl:variable name="tmp" select="ssn" />
<xsl:for-each select="key('incomekey', $tmp)" >
<income amount="{amount}" />
</xsl:for-each>
</person>
</xsl:template>
</xsl:stylesheet>
Important points here:

• Loops here are nested. The outer loop is <xsl:apply-templates> on


<person>, the inner loop is on the key
• If inner loop is on <income>, then it would be very inefficient.
• The <xsl:key> and key() function are playing the same role as index in
database.

XSL:Counting for an Attribute value...its URGENT!!! here is my xml


document:

<results>
<testResult>
<logs>
<log message="theLogMessage"/>
</logs>
<result status="-1" />
</testResult>
<testResult>
<logs>
<log message="theLogMessage"/>
</logs>
<result status="-5" />
</testResult>
<testResult>
<logs>
<log message="theLogMessage"/>
</logs>
<result status="-1" />
</testResult>
<testResult>
<logs>
<log message="theLogMessage"/>
</logs>
<result status="-5" />
</testResult>
</results>
i need output like:
status count
-1 2
-5 2
I wanna know how to count the number of an attribute with particular
values?? An early response is required...as i m stuck wit this
Location: http://www.jguru.com/faq/view.jsp?EID=1081267
Created: May 2, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Ravi Anand
(http://www.jguru.com/guru/viewbio.jsp?EID=1079662
Here is a complete answer for you!

Q. Can we do statistics on a specific attribute value?


For examplw, how to get the statistics of status attribute of result element:

<results>
<result status="-1" >aaa</result>
<result status="-5" >bbb</result>
<result status="-1" >ccc</result>
<result status="-5" >ddd</result>
<result status="-5" >lll</result>
<result status="-6" >vvv</result>
<result status="-1" >vvv</result>
</results>

A: Using XSLT Grouping. Here is using muenchain method.


XSLT sourece
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:key name="stvalkey" match="//result" use="@status" />
<xsl:template match="/">
<xsl:for-each select="//result/" >
<xsl:variable name="tmp" >
<xsl:value-of select="./@status" />
</xsl:variable>
<xsl:if test="generate-id(.) = generate-
id(key('stvalkey',$tmp)[1])">
<xsl:value-of select="$tmp" />:<xsl:value-of
select="count(key('stvalkey',$tmp))" /><xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
Text result to show the statistics
-1:3
-5:3
-6:1

HTML Tag embedded in XML The client I'm working with has embedded a
<br> tag inside one of their elements in a document.

<subheadline>7:00 EST <br> Listen on frodo.com</subheadline>


The want this element to be processed by an XSL to create an XML feed that
preserves the <br> without resorting to the apersand replacements.

I realize this is not properly formed XML, but I can't get them to change
their minds about this.

Any possible idea's withouth having to resort to CDATA?

Thanks

Stephen McConnell
Location: http://www.jguru.com/faq/view.jsp?EID=1082623
Created: May 7, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Stephen McConnell PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=248618

• If you insert html, AFAIK, CDATA section is the only answer to keep your XML
file well-formed.
• If you change your html to xhtml, of course, the well-formness problems
would be solved. The next, it would be validation problems if you need to
validate your XML. :) You also can solve that problem by setting
processContents="lax" in your schema to get away from there. See Q. What
does the validator do when we set processContents="lax"?

I'm now generating a xml in to a string from database and but for many
data like 15000 data it took almost 83 minutes...

I'm now generating a xml in to a string from database and


using XSLT generating reports but for many data like
15000 data it took almost 83 minutes...is there any
alternative, I'm using SAX and JAPX.
Main problem is in the loop or string manupulation
into the loop.
suppose String xmlString= null;
while (rs.next()){
xmlString = xmlString + "<id>" + id + "</id>"

...
...
xmlString= xmlString + "<value>" + value + "</value>"
}

....
some process to transform this xmlString with xsl file and
generate report.

Location: http://www.jguru.com/faq/view.jsp?EID=1083627
Created: May 11, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by mahbub kabir
(http://www.jguru.com/guru/viewbio.jsp?EID=1083598
I guess you would never need 15000 records for XSL transform and display.
Therefore, you need only to load the data needed, and show the user. Load other
data on needed bases. This is the general concept of GoF proxy design pattern.
There are many ways to implement it. Try to find one fit your needs.
Comments and alternative answers

stringbuffer
Author: Rob Parker (http://www.jguru.com/guru/viewbio.jsp?EID=914522), May 14,
2003
One thing I noticed from your code is that you should be using a
StringBuffer instead of a string when creating your xml. Strings are
immutable, so every time you concatenate a string to itself,
you really create a new string object. This can really hurt
your performance, especially when dealing with large strings. You should change:
String myString = "";
myString = myString + "blah";
myString = myString + "blah2";
...

To:
StringBuffer sb = new StringBuffer();
sb.append("blah");
sb.append("blah2");
...
myString = sb.toString();

Hope that makes sense. It should at least help a little.

Rob

Re: stringbuffer
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432),
May 16, 2003
Doing this is fine
xmlString= xmlString + "<value>" + value + "</value>"
Since javac is actually using StringBuffer to do String concatendations. No extra
String object created. Check j2sdk src files for details.

Using multiple parsers in classpath Hi All


I have following parsers ( they are needed by dff applications, so cant get
rid of any of them ) in my classpath.
jdom.jar:- latest version
crimson.jar: - latest version
jaxp.jar: - latest version parser.jar:
xalan.jar: -1st version
xerces.jar: - version 1.3
dom.jar: - 1st version
jaxp-api.jar: - latest version
saaj.jar:- latest version
axis.jar:- latest version
jaxrpc.jar:- latest version
j2ee.jar:- latest version

Here is the xml file encoding-map.xml

<?xml version="1.0" standalone="yes"?>


<!DOCTYPE EncodingMap [
<!ELEMENT EncodingMap (XmlEncoding?, JavaEncoding*)>
<!-- The encoding for writing XML -->
<!ELEMENT XmlEncoding (#PCDATA) >
<!-- Defines a mapping between a Java encoding name and a
standard name -->
<!ELEMENT JavaEncoding (JavaName, MimeName)>
<!-- The name Java assigns to a specific encoding (Case
sensative) -->
<!ELEMENT JavaName (#PCDATA)>
<!-- The IANA equivalent name for the Java encoding name -->
<!ELEMENT MimeName (Name, Alias*)>
<!-- The name (Case insensative) -->
<!ELEMENT Name (#PCDATA)>
<!-- An IANA alias for a MIME name (Case insensative) -->
<!ELEMENT Alias (#PCDATA)>
]>

<EncodingMap>
<!-- Set to the desired MIME defined in this file -->
<XmlEncoding>UTF-8</XmlEncoding>
<!-- ASCII -->
<JavaEncoding>
<JavaName>ASCII</JavaName>
<MimeName>
<Name>US-ASCII</Name>
</MimeName>
</JavaEncoding>
<!-- Latin1 -->
<JavaEncoding>
<JavaName>ISO8859_1</JavaName>
<MimeName>
<Name>ISO-8859-1</Name>
<Alias>ISO_8859-1</Alias>
<Alias>LATIN1</Alias>
<Alias>L1</Alias>
<Alias>IBM819</Alias>
<Alias>CP819</Alias>
</MimeName>
</JavaEncoding>
<!-- Latin2 -->
<JavaEncoding>
<JavaName>ISO8859_2</JavaName>
<MimeName>
<Name>ISO-8859-2</Name>
<Alias>ISO_8859-2</Alias>
<Alias>Latin2</Alias>
<Alias>L2</Alias>
<Alias>ISO-IR-101</Alias>
</MimeName>
</JavaEncoding>

</EncodingMap>
I m getting this error
ERROR: Unable to load encoding-map.xml from the CLASSPATH:
org.jdom.JDOMException: Error on line 51: Document root element
"EncodingMap", must match DOCTYPE root "EncodingMap".
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:367)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:673)

Caused by: org.xml.sax.SAXParseException: Document root element


"EncodingMap", must match DOCTYPE root "EncodingMap".
Please help. I m confused as to how to resolve these multiple parsers
issues.
Thanks

Location: http://www.jguru.com/faq/view.jsp?EID=1084018
Created: May 12, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by tech tech
(http://www.jguru.com/guru/viewbio.jsp?EID=809635
What a mess? Many things are wrong here. Don't know what is for what.

• Most of the jars listed here are not parsers.


• "I have following parsers ( they are needed by dff applications, so cant get rid
of any of them ) in my classpath." Wrong! You should set different
classpaths for different applications. Not set all of them in the same
environment variable.
• Many class libraries you listed here are not binary compatible, put them all on
your classpath is definitely disaster prone.
• What you or your project team really need is a good architect.
• I think I can help to straight things out. However, that cannot be solved by a
post. That should be at least a paid online tutoring session, provided you
and/or your company are interested.
• Read here for some more information: Online tutoring, training by
Roseanne Zhang

Thanks!
Comments and alternative answers

Take a new manners tutorial


Author: tech tech (http://www.jguru.com/guru/viewbio.jsp?EID=809635), May 13,
2003
1. I know very well that I should set diff claspaths for diff applications. but i m new
here and there are a lot of applications already deployed in this environment, so
chanign anything currently in a day or two is not possible. There are a lot of big
companies which have a lot of legacy code , code written for last 5 yesrs , and
systems are so big that you cannot just re write or change architecture just like that.
So dont tell me that the 1st thing i should do is to get a tutorial from anyone of you. I
know the things are bad here, but working woith legacy systems and bringing them to
current trends is a skill too. work in a big fat organization with 20 million hits a day to
73 diff applications then you would know what a change in architecture means. 2. I
was just looking for an answer if anyone has come across a specific JDOM errror or
sax parser error. If you cant get me an answer its fine, dont sell me your tutorial. I
know i can teach better than you guys. I USED NEW XERCES parser and it worked
fine ...

How to send a value from a Html form to an xslt HI, I have an html form
with some options , an xml file that contains many records , and an xslt file.
Now when the user selects an option in the html file then i want to pass that
value to my xslt to select only those records that matches the pattern. Is
this possible ?
Location: http://www.jguru.com/faq/view.jsp?EID=1085989
Created: May 19, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Harini Srinivasan
(http://www.jguru.com/guru/viewbio.jsp?EID=1082961

Good question!

Use Transformer.setParameter(name, value) method to pass it to your XSLT, then


pick it up and handle it in your XSLT.

Can I get at the processing instruction via dom or sax

I need to get the href attribute from a XML document processing instruction
tag can I do this via dom/sax or should I just read in the file line by line ? I
need to get the href from

<?xml-stylesheet type="text/xsl" href="value" ?>

Cheers, Pete. </P


Location: http://www.jguru.com/faq/view.jsp?EID=1087154
Created: May 22, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Peter Craddock
(http://www.jguru.com/guru/viewbio.jsp?EID=1060265
In SAX, use org.xml.sax.helpers.DefaultHandler.processingInstruction
method.
public void processingInstruction(java.lang.String target,
java.lang.String data)
throws SAXException
Receive notification of a processing instruction.
By default, do nothing. Application writers may override this method in
a subclass to take specific actions for each processing instruction,
such as setting status variables or invoking other methods.
In DOM: org.w3c.dom.ProcessingInstruction is a Node on the DOM tree.

how to handle 2 xml files with one xsl file i have 2 xml files .i have to write
anxsl file,which should go through the first xml file and search for an
element and that element should be replaced with another element which is
in second xml file. pls help me.
Location: http://www.jguru.com/faq/view.jsp?EID=1087553
Created: May 23, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by srinivas rallapalli
(http://www.jguru.com/guru/viewbio.jsp?EID=1087438

You have to use some extention until next XSLT recommendation (1.1 ?). Here is an
example: http://bobcat.webappcabaret.net/javachina/faq/xslt_01.htm#xslt_ex_Q01

Having both DTD and Schema On an XML Hi,

I'm just wondering it is logical to have both DTD and Schema for an XML?

I did that and when I parse it using Xerces, it give me a warning, saying
something like "parsing might not work properly if both DTD and Schema is
present".

Any comments?

Thanks in advance.
Location: http://www.jguru.com/faq/view.jsp?EID=1087556
Created: May 23, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by James Gordon
(http://www.jguru.com/guru/viewbio.jsp?EID=816892

It is not recommended. I made it work once, the major reason was trying to take
advantage of DTD entities. It would be easily causing confusion. It would be hard to
maintain stuff in the feature.

My suggestion: don't do it! That was not the w3c's intention either.

Writing a SAX parser that validates external schemas. Hi. Could someone
tell me how to write a SAX parser that validates an external schema? I've
previously written a parser that validates inline DTDs, but now I need to
urgently improve my existing code. Any help would be much appreciated -
I'm using Xerces and would prefer to stick to non-proprietary solutions.
Location: http://www.jguru.com/faq/view.jsp?EID=1088114
Created: May 26, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Stephen Smith
(http://www.jguru.com/guru/viewbio.jsp?EID=1088030

That is a norm instead of exception. You only need set certain features. I've an
example here. It is using JDOM, but the process is exact the same.

Q. A simple XML validator using xml schema (JDOM).

If you put the second argument as *.xsd, it will use the external xsd instead of the
one specified by the XML file

how to generate xml file with only some elements of another xml file using
xsl file. I have an xml file.Now i have to write an xsl file so that,it should
copy only some elements of the input xml file. please help me.this is urgent
Location: http://www.jguru.com/faq/view.jsp?EID=1088121
Created: May 26, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by srinivas rallapalli
(http://www.jguru.com/guru/viewbio.jsp?EID=1087438

This is a summary. It just depends how many you want to copy over.

1. If you only want to copy a few, then disable all default templates and only
write the copy templates for those you want to copy.
2. If you want to copy all but exclude a few, then use identity template, and only
disable a few templates for those you want not to copy.
3. How to disable some templates? Simple:
4. <xsl:template match="tag1_to_disable|tag2_to_disable" />
5. How to write identity template?
6. <xsl:template match="*|@*|comment()|processing-instruction()|
text()">
7. <xsl:copy>
8. <xsl:apply-templates select="*|@*|comment()|processing-
instruction()|text()"/>
9. </xsl:copy>
10. </xsl:template>
11. What is copy templates?
12.<xsl:template match="tag1_to_copy|tag2_to_copy" >
13. <xsl:copy-of select="." />
14.</xsl:template>

Attention, this will copy it and its decendents.

Jan Matèrne also gave a good example here.

XSDs, Attributes, and Enumeration

I've built XML schemas before, and created elements that had enumerations
or attributes, but never both. What I want is something like:
<state code="1234">GA</state>

Where the "GA" value comes from an enumerated list of possible state
values. No matter what I do, I can't come up with a solution that allows me
to give the element both enumeration and an attribute. How can I make it
happen?

Location: http://www.jguru.com/faq/view.jsp?EID=1091206
Created: Jun 4, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Jack Lamphier
(http://www.jguru.com/guru/viewbio.jsp?EID=1090448
This is a good question! When you try to enumerate base type string, you need
restriction. However, you want to add attributes to a simpleContent, you need
extension. This took me sometime to figure the answer out. It worthed the effort
anyway, since I learned a lot from the process. Here is the full xml schema solution
for the given XML, validated. The key is to separate restriction and extension.

XML Source

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="n1.xsd">
<state code="12345">TX</state>
<state code="34651">CO</state>
<state code="41562">FL</state>
<state code="28309">NJ</state>
</root>
XML Schema Source
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="state" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="state" >


<xs:complexType>
<xs:simpleContent>
<xs:extension base="enum" >
<xs:attribute name="code" type="xs:integer" />
</xs:extension>
</xs:simpleContent>

</xs:complexType>
</xs:element>

<xs:simpleType name="enum">
<xs:restriction base="xs:string">
<xs:enumeration value="NJ" />
<xs:enumeration value="TX" />
<xs:enumeration value="FL" />
<xs:enumeration value="CO" />
</xs:restriction>
</xs:simpleType>
</xs:schema>

How to enforce Elements has value by XSD definition Hi, I have a


requirement that the Element <startdate> must have value.How to enforce
that during XSD Design
Location: http://www.jguru.com/faq/view.jsp?EID=1091866
Created: Jun 6, 2003 Modified: 2003-06-08 15:28:26.213
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by subbuu girish
(http://www.jguru.com/guru/viewbio.jsp?EID=1091536

If it is xsd:date type, empty string will not be valid. If it is xsd:string type, you can
use length or pattern facet to control it...

However, if you want to do the opposite, allowing nil value for the elements as above
defined, you need to do something special, I've an FAQ on the web, take a look:

Q. What does nillable mean in schema? How to specify it in instance


document?

Comments and alternative answers

Enforce Elements have value by XSD definition


Author: subbuu girish (http://www.jguru.com/guru/viewbio.jsp?EID=1091536), Jun
6, 2003
Thanks Roseanne, Pl elaborate the same with an example.

Re: Enforce Elements have value by XSD definition


Author: Christopher Koenigsberg
(http://www.jguru.com/guru/viewbio.jsp?EID=722897), Jun 9, 2003

My example somehow got cut out when the original thread was edited into the
FAQ entry:

"xs:pattern" takes regular expressions.


Account number string type requiring numeric digits, with length
between 1 and 10:

<xs:simpleType name="acctnbrtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{1,10}"/>
</xs:restriction>
</xs:simpleType>
Merging two XML documents This question is very similar to the one on
adding existing JDOM/DOM elements to a document object. I just want to
know if it is possible to combine two existing XML documents together into
a new JDOM object (i.e. a new XML file).

ex1.xml
<fruit>
<apple></apple>
</fruit>

ex2.xml
<record>
<orange></orange>
</record>

result.xml
<fruit>
<apple></apple>
<orange></orange>
</fruit>

Let me know. Thanks.


Location: http://www.jguru.com/faq/view.jsp?EID=1092656
Created: Jun 10, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Vantrang Luong
(http://www.jguru.com/guru/viewbio.jsp?EID=1088431
If you are using JDOM, then use the org.jdom.Element.clone() method. It is a deep
clone, and the clone will forget where it from.

However, if you're using DOM, clone will not work, even you set it deep. You need to
use org.w3.dom.Document.importNode() to do the same.

That was why I used full qualified class names.

How to dynamically generate HTML table by using XSLT? Hi everyone.


I have this xml

<items>
<item desc="BA"/>
<item desc="BE"/>
<item desc="BI"/>
<item desc="BO"/>
<item desc="BU"/>
<item desc="CA"/>
<item desc="CE"/>
<item desc="CI"/>
</items>
I have to place all items into a 5 columns and n rows table.
So the final result should be like the following:

BA BE BI BO BU
CA CE CI

I can't understand how to insert <tr> in the right place to let the table
inserts a new row when necessary.
Can somebody give me a clue ?
Thanks.
Ivan.
Location: http://www.jguru.com/faq/view.jsp?EID=1094766
Created: Jun 17, 2003
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by ivan siviero
(http://www.jguru.com/guru/viewbio.jsp?EID=781131

Here is your simple and complete answer. There is no need to do more than one
steps. You just need a little xpath trick. The output is valid xhtml as well as html.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />
<xsl:template match="/items" >
<html>
<body >
<table border="1">
<xsl:apply-templates />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="item[(position()-1) mod 5 = 0]" >
<tr>
<td><xsl:value-of select="@desc" /></td>
<xsl:for-each select="following-sibling::node()[position() &lt;
5]">
<td><xsl:value-of select="@desc" /></td>
</xsl:for-each>
</tr>
</xsl:template>
</xsl:stylesheet>
Visit JavaChina Online
Comments and alternative answers

Problems with NETSCAPE


Author: Andrea ? (http://www.jguru.com/guru/viewbio.jsp?EID=1146275), Feb 14,
2004
Hi, I've tried the code using NETSCAPE 7.1 and it doesn't work. What's wrong with
this?
Re: Problems with NETSCAPE
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432),
Feb 29, 2004
I don't use netscape. However, you can use any transformer to transform yourself.
How about try to use ant xslt task?

Dynamic table with XSL


Author: Maurizio Sala (http://www.jguru.com/guru/viewbio.jsp?EID=1171194), May
17, 2004
I have the same problem of show a xml file in n cells of a table.
I find the solution in this answer but my xml is a little more complex with other
element child of "item".

<?xml version="1.0" encoding="ISO-8859-1" ?>


<?xml:stylesheet type="text/xsl" href="prova.xsl" ?>
<items>
<item desc="BA">
<text>aaa</text>
</item>
<item desc="BE">
<text>bbb</text>
</item>
<item desc="BI">
<text>ccc</text>
</item>
<item desc="BO">
<text>ddd</text>
</item> <item desc="BU">
<text>eee</text>
</item>
<item desc="CA">
<text>fff</text>
</item>
<item desc="CE">
<text>ggg</text>
</item>
<item desc="CI">
<text>hhh</text>
</item>
</items>

I want the same result of the question but in this case i have this response:

bbbcccdddeee
ggghhh
BA BE BI BO BU
CA CE CI

What is the problem of unwanted text?

Re: Dynamic table with XSL


Author: krishna koppisetty
(http://www.jguru.com/guru/viewbio.jsp?EID=1171438),
May 26, 2004
Hi, Just add the this line of code to your xsl file,
prova.xsl. xsl:template match="text"/>. This overrides
the default template for the text element. Now, your new
xsl file must look like this, <?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />
<xsl:template match="/items" > <html> <body >
<xsl:apply-templates /> </body> </html>
</xsl:template> <xsl:template match="item[(position() -
1) mod 5 = 0]">

<xsl:value-of
<xsl:value-of select="@desc"
select="@desc"/> /> </xsl:for-
<xsl:for-each each>
select="@desc"/> </xsl:template>
<xsl:for-each <xsl:template
select="following- match="text"/>
sibling::node()[position() </xsl:stylesheet>
< 5]">

Re[2]: Dynamic table with XSL


Author: Maurizio Sala
(http://www.jguru.com/guru/viewbio.jsp?EID=1171194), Jun 16, 2004
OK, Krishna, it works!
Thanks a lot.
Maurizio.

Re[3]: Dynamic table with XSL


Author: Maurizio Sala
(http://www.jguru.com/guru/viewbio.jsp?EID=1171194), Sep 13, 2004
Another question.
Can I sort and select element before create the table?
When I try to select the element after the "following-
sibling::node()[position()" statement I obtain a table with one row with 2
elements, one ow with 4 elements, ...
Maurizio

XML problem with '&' I am having a problem with XML. Whenever one of my
xml files had some data in it that contains an '&', for example <tag>
keyboard & mouse </tag> the xgen code throws an exceptions stating that
the reference cannot be found. When it hits the '&' it thinks that the value
'mouse' is a reference. Has anyone else had this problem, and does anyone
have a solution.
Location: http://www.jguru.com/faq/view.jsp?EID=1174122
Created: May 27, 2004
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by logicaCMG LogicaCMG
(http://www.jguru.com/guru/viewbio.jsp?EID=1174007

use &amp; instead. This should be used everywhere except you really have an entity
reference. It applies to html, xhtml, xml, etc.
Comments and alternative answers

Or CDATA
Author: Malachi de AElfweald
(http://www.jguru.com/guru/viewbio.jsp?EID=126518), Jul 22, 2004
I agree with using the &amp;

however, if you are unable to for whatever reason (for example, when multiple levels
might be encoding or decoding it), you can also use a CDATA....

example:

<tag><![CDATA[ Me & You ]]></tag>

anything in the CDATA is uninterpreted.

How can I display tree on the screen?

How can I display an XML file into tree structure format using jsp on the
screen?

Location: http://www.jguru.com/faq/view.jsp?EID=1241204
Created: Apr 26, 2005 Modified: 2005-04-26 15:35:59.827
Author: Ravi Kumar Srirangam
(http://www.jguru.com/guru/viewbio.jsp?EID=1080005) Question originally posed
by rajesh k (http://www.jguru.com/guru/viewbio.jsp?EID=1240895
If you are using XSL to render the HTML, then the following link will be of some help:
http://www.15seconds.com/issue/010921.htm
converting <abc/> to <abc></abc>

I have a xml which has nodes like <abc/>. My scanner fails to recognise
them. So I need to transform the nodes <abc/> to <abc></abc> before
calling the scanner. Please help me with a regular expression. Thanks!
Location: http://www.jguru.com/faq/view.jsp?EID=1254977
Created: Jul 25, 2005
Author: Almagest FUTT (http://www.jguru.com/guru/viewbio.jsp?EID=1244568)
Question originally posed by prabha p
(http://www.jguru.com/guru/viewbio.jsp?EID=1254389

You do actually mean <abc/> when you say "singleton nodes", don't you ? For
otherwise, it wouldn't be surprising they made a fuss.

A regex would be:


perl: s|\<(\w+)\s*/\>|<$1></$1>|g;
java: string.replaceAll("<(\\w+)\\s*/>", "<$1></$1>");

Cheers,
FUTT.

How to ask an XSLT question?


Location: http://www.jguru.com/faq/view.jsp?EID=1257356
Created: Aug 9, 2005
Author: Roseanne Zhang (http://www.jguru.com/guru/viewbio.jsp?EID=240432)
Question originally posed by Steven C Hughes
(http://www.jguru.com/guru/viewbio.jsp?EID=1256674

Put your input XML


Put your desired html output
Put your XSLT which does not work as desired

In this way, others might come out a suggestion or even a solution.

Comments and alternative answers

Your code
Author: Jan Matèrne (http://www.jguru.com/guru/viewbio.jsp?EID=567992), Aug 10,
2005

And don´t forget to format your code for (human) readability. Use <pre> tags for
keeping the indents and line breaks. Maybe you want to use <font color=?> for
showing difficult things.

Yep, and another tip regarding HTML here: because html tags are allowed while
posting you have to mask the html code in postings - simply replace all < by &lt; and
it should work. Use the preview ("verify") of jGuru for checking.
Thanks Roseanne for this FAQ :-)

Potrebbero piacerti anche