473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem with Xerces-J

The solution to this problem is probably very simple but I can't seem to
find any information about it. I'm trying to parse an XML document with
the Xerces-J (xml.apache.org) DOM implentation. I don't know how common
is the usage of that library but it seemed to be the most documented one
I could find.

The problem is that many functions return child elements as NodeList.
NodeList being a list of Node, I tryed to cast them as Element, to be
able to use getAttribute() on them. When I do so, I get a
ClassCastException. Is there a way to to convert a Node to an Element?

I also have a theorical question, which will probably solve the problem
above... What is the difference between a Node and an Element in XML? In
what cases is a Node not an Element? I'm just trying to figure out why
they seperated both. I'm sure there is a good reason, but I couldn't
find it explicitly and as a student, I don't have enough time to read
the entire specification to find this single information.

--
Louis-Philippe Huberdeau

Jul 20 '05 #1
4 1654
In article <fJ*******************@news20.bellglobal.com>, Louis-Philippe Huberdeau wrote:
The solution to this problem is probably very simple but I can't seem to
find any information about it. I'm trying to parse an XML document with
the Xerces-J (xml.apache.org) DOM implentation. I don't know how common
is the usage of that library but it seemed to be the most documented one
I could find.

The problem is that many functions return child elements as NodeList.
NodeList being a list of Node, I tryed to cast them as Element, to be
able to use getAttribute() on them. When I do so, I get a
ClassCastException. Is there a way to to convert a Node to an Element?
A quick look at

http://xml.apache.org/xerces2-j/java.../NodeList.html

shows that NodeList has a Node item(int) method. Invoking item(0) on a
NodeList should return the first Node of the NodeList.
I also have a theorical question, which will probably solve the problem
above... What is the difference between a Node and an Element in XML? In
what cases is a Node not an Element? I'm just trying to figure out why
they seperated both. I'm sure there is a good reason, but I couldn't
find it explicitly and as a student, I don't have enough time to read
the entire specification to find this single information.


A quick look at

http://xml.apache.org/xerces2-j/java.../dom/Node.html

shows that the list of subinterfaces of Node Element and other
non-element types types, such as Attr and CharacterData.
Jul 20 '05 #2
A. Bolmarcich wrote:
In article <fJ*******************@news20.bellglobal.com>, Louis-Philippe Huberdeau wrote:
The solution to this problem is probably very simple but I can't seem to
find any information about it. I'm trying to parse an XML document with
the Xerces-J (xml.apache.org) DOM implentation. I don't know how common
is the usage of that library but it seemed to be the most documented one
I could find.

The problem is that many functions return child elements as NodeList.
NodeList being a list of Node, I tryed to cast them as Element, to be
able to use getAttribute() on them. When I do so, I get a
ClassCastException. Is there a way to to convert a Node to an Element?

A quick look at

http://xml.apache.org/xerces2-j/java.../NodeList.html

shows that NodeList has a Node item(int) method. Invoking item(0) on a
NodeList should return the first Node of the NodeList.


That's not the problem, I can get the Node, I just can't use it as an
Element to use the methods I really want to use.

I also have a theorical question, which will probably solve the problem
above... What is the difference between a Node and an Element in XML? In
what cases is a Node not an Element? I'm just trying to figure out why
they seperated both. I'm sure there is a good reason, but I couldn't
find it explicitly and as a student, I don't have enough time to read
the entire specification to find this single information.

A quick look at

http://xml.apache.org/xerces2-j/java.../dom/Node.html

shows that the list of subinterfaces of Node Element and other
non-element types types, such as Attr and CharacterData.


I just tought about something related to that. I will test it out
tomorrow and give you feedback about it.

Thank you for your time.

--
Louis-Philippe Huberdeau

Jul 20 '05 #3
Louis-Philippe Huberdeau wrote:
The solution to this problem is probably very simple but I can't seem to
find any information about it. I'm trying to parse an XML document with
the Xerces-J (xml.apache.org) DOM implentation. I don't know how common
is the usage of that library but it seemed to be the most documented one
I could find.

The problem is that many functions return child elements as NodeList.
NodeList being a list of Node, I tryed to cast them as Element, to be
able to use getAttribute() on them. When I do so, I get a
ClassCastException. Is there a way to to convert a Node to an Element?

I also have a theorical question, which will probably solve the problem
above... What is the difference between a Node and an Element in XML? In
what cases is a Node not an Element? I'm just trying to figure out why
they seperated both. I'm sure there is a good reason, but I couldn't
find it explicitly and as a student, I don't have enough time to read
the entire specification to find this single information.

--
Louis-Philippe Huberdeau


Hi,

In the data model, all object of the document (and it is true for the
Document itself) is a Node.
So, an Element is also a Node.
But a Node may be something else than an Element, for example, a Text
(there is a dozen of types).
So, when you've a NodeList, you should test wich kind of node you got
before casting it :

Node node = list.item(i);
if (node.getNodeType()==Node.ELEMENT_NODE) {
Element el = (Element) node;
// your stuff here
}

Notice that in the following document :
<foo>
<bar>
</foo>
the NodeList obtained from the foo Element will return 3 nodes :
-a text node with &#xA;
-an element node named bar
-a text node with &#xA;
--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------

Jul 20 '05 #4
Thanks for the information, that did work. I just never tought a blank
space could be a node.

Philippe Poulard wrote:
Louis-Philippe Huberdeau wrote:
The solution to this problem is probably very simple but I can't seem
to find any information about it. I'm trying to parse an XML document
with the Xerces-J (xml.apache.org) DOM implentation. I don't know how
common is the usage of that library but it seemed to be the most
documented one I could find.

The problem is that many functions return child elements as NodeList.
NodeList being a list of Node, I tryed to cast them as Element, to be
able to use getAttribute() on them. When I do so, I get a
ClassCastException. Is there a way to to convert a Node to an Element?

I also have a theorical question, which will probably solve the
problem above... What is the difference between a Node and an Element
in XML? In what cases is a Node not an Element? I'm just trying to
figure out why they seperated both. I'm sure there is a good reason,
but I couldn't find it explicitly and as a student, I don't have
enough time to read the entire specification to find this single
information.

--
Louis-Philippe Huberdeau


Hi,

In the data model, all object of the document (and it is true for the
Document itself) is a Node.
So, an Element is also a Node.
But a Node may be something else than an Element, for example, a Text
(there is a dozen of types).
So, when you've a NodeList, you should test wich kind of node you got
before casting it :

Node node = list.item(i);
if (node.getNodeType()==Node.ELEMENT_NODE) {
Element el = (Element) node;
// your stuff here
}

Notice that in the following document :
<foo>
<bar>
</foo>
the NodeList obtained from the foo Element will return 3 nodes :
-a text node with &#xA;
-an element node named bar
-a text node with &#xA;


Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Roy Benjamin | last post by:
I'm developing a WEB service for Sun ONE deployment (AppServer7). I'm developing on Windows XP Pro though will deploy on Solaris, Sun AppServer7 includes a XercesImpl.jar in share/lib. 2 kb...
2
by: Bekkali Hicham | last post by:
hi, i have downloaded the latest version 2.4 of Xerces, and unziped it, i end up with a diectory hierarchy like this c:\xerces-2_4_0\XercesImpl.jar c:\xerces-2_4_0\XercesSamples.jar...
1
by: Stu | last post by:
I am trying to build the xerces shared library with 2.3.0 version of their source code on an AIX 5.1 32 bit machine with the following version of the g++ compiler /usr/local/bin/g++ -v Reading...
0
by: Waseem | last post by:
Hi I have looked and tried everything and i still cant sort this out i have no idea why this wont work I am using Xerces Perl on Windows and Debian to try this and it wont work on both of...
0
by: Brian | last post by:
I am having alot of trouble getting a XML document validated with a schema. I got a sample document and schema off of w3schools.com, which passed an online xml validator:...
4
by: joes | last post by:
Hello there I tried for several days to get a simple validation with xml schema & xerces working. Goal for me is tuse JAXP and not specific Xerces classes. I don't get the point what I am doing...
0
by: Jim Phelps | last post by:
After having memory leak issues with Xerces-c 2.3.0 for Solaris 2.7 for CC 6.2 I have decided to update to at least 2.4. I have downloaded the binary tarball and have installed it on my...
2
by: MBR | last post by:
Problem with xerces and C++ Hi, I try the sample from the xerces homepage http://xml.apache.org/xerces-c/program-dom.html, take the iterator and step through the tree: for (DOMNode*...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
9
by: mstilli | last post by:
Hi, I am trying to use schema for server side validation using xerces to catch the validation errors. validating this XML: <Content4> <textarea13></textarea13>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.