You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Jason Jesso <jj...@global-matrix.com> on 2002/02/18 15:58:20 UTC
parsing an xml document
I think I am confused in the way parsing an xml file works.
Let me give my example and the output, then I will tell you what I would
expect as output.
My function:
void
build( DOM_Document doc )
{
// get the list of actions
DOM_NodeList nl = doc.getElementsByTagName("ACTION");
cout << "Number of actions = " << nl.getLength() << endl;
// process each action
for ( int actionNum = 0; actionNum < nl.getLength(); actionNum++ ){
// the current action node
DOM_Node actionNode = nl.item( actionNum );
DOM_Node objectNode = actionNode.getFirstChild();
if ( objectNode != NULL ){
cout << "object " << objectNode.getNodeName() << endl;
}
DOM_Node sendXmlNode = actionNode.getNextSibling();
if ( sendXmlNode != NULL ){
cout << "sendxml " << sendXmlNode.getNodeName() << endl;
}
DOM_Node sendHtmlNode = actionNode.getNextSibling();
if ( sendHtmlNode != NULL ){
cout << "sendhtml " << sendHtmlNode.getNodeName() <<
endl;
}
}
}
My xml input file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE SCRIPT SYSTEM "mtxXml.dtd" >
<SCRIPT>
<ACTION>
<OBJECT>
<FREE_FORM>
<LITERAL>EAS</LITERAL><VARIABLE>username</VARIABLE>
<LITERAL>GBL</LITERAL><VARIABLE>password</VARIABLE>
</FREE_FORM>
</OBJECT>
<SENDXML/>
</ACTION>
<ACTION>
<OBJECT>
<FREE_FORM>
<LITERAL>QBN</LITERAL>
</FREE_FORM>
</OBJECT>
<SENDXML/>
</ACTION>
<ACTION>
<OBJECT>
<FREE_FORM>
<LITERAL>XAS</LITERAL>
</FREE_FORM>
</OBJECT>
<SENDXML/>
<SENDHTML/>
</ACTION>
</SCRIPT>
My output:
Number of actions = 3
object #text
sendxml #text
sendhtml #text
object #text
sendxml #text
sendhtml #text
object #text
sendxml #text
sendhtml #text
Parse successful!
I would expect that a call to getFirstChild on the ACTION element would
give me the OBJECT element. A call to getNextSibling on the ACTION
element would give me the SENDXML element. This does not seem to be the
case. It looks like I get a text element.
Can somebody please clarify why I am getting what I do not expect?
Thanks
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: parsing an xml document
Posted by Tinny Ng <tn...@ca.ibm.com>.
> // process each action
> for ( int actionNum = 0; actionNum < nl.getLength(); actionNum++ ){
BTW please avoid calling getLength in a for loop like this. The length is
dynamic, and is re-calculated everytime getLength() is called. So calling
getLength for each iteration is very expensive.
We should do something like this:
int length = n1.getLength();
for ( int actionNum = 0; actionNum < length; actionNum++ ){
:
}
Tinny
----- Original Message -----
From: "Jason Jesso" <jj...@global-matrix.com>
To: <xe...@xml.apache.org>
Sent: Monday, February 18, 2002 9:58 AM
Subject: parsing an xml document
> I think I am confused in the way parsing an xml file works.
>
> Let me give my example and the output, then I will tell you what I would
> expect as output.
>
> My function:
>
> void
> build( DOM_Document doc )
> {
> // get the list of actions
> DOM_NodeList nl = doc.getElementsByTagName("ACTION");
>
> cout << "Number of actions = " << nl.getLength() << endl;
>
> // process each action
> for ( int actionNum = 0; actionNum < nl.getLength(); actionNum++ ){
>
> // the current action node
> DOM_Node actionNode = nl.item( actionNum );
>
> DOM_Node objectNode = actionNode.getFirstChild();
> if ( objectNode != NULL ){
> cout << "object " << objectNode.getNodeName() << endl;
> }
>
> DOM_Node sendXmlNode = actionNode.getNextSibling();
> if ( sendXmlNode != NULL ){
> cout << "sendxml " << sendXmlNode.getNodeName() << endl;
> }
>
> DOM_Node sendHtmlNode = actionNode.getNextSibling();
> if ( sendHtmlNode != NULL ){
> cout << "sendhtml " << sendHtmlNode.getNodeName() <<
> endl;
> }
>
> }
> }
>
> My xml input file:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE SCRIPT SYSTEM "mtxXml.dtd" >
> <SCRIPT>
>
> <ACTION>
> <OBJECT>
> <FREE_FORM>
> <LITERAL>EAS</LITERAL><VARIABLE>username</VARIABLE>
> <LITERAL>GBL</LITERAL><VARIABLE>password</VARIABLE>
> </FREE_FORM>
> </OBJECT>
> <SENDXML/>
> </ACTION>
>
> <ACTION>
> <OBJECT>
> <FREE_FORM>
> <LITERAL>QBN</LITERAL>
> </FREE_FORM>
> </OBJECT>
> <SENDXML/>
> </ACTION>
>
> <ACTION>
> <OBJECT>
> <FREE_FORM>
> <LITERAL>XAS</LITERAL>
> </FREE_FORM>
> </OBJECT>
> <SENDXML/>
> <SENDHTML/>
> </ACTION>
>
> </SCRIPT>
>
> My output:
>
> Number of actions = 3
> object #text
> sendxml #text
> sendhtml #text
> object #text
> sendxml #text
> sendhtml #text
> object #text
> sendxml #text
> sendhtml #text
> Parse successful!
>
> I would expect that a call to getFirstChild on the ACTION element would
> give me the OBJECT element. A call to getNextSibling on the ACTION
> element would give me the SENDXML element. This does not seem to be the
> case. It looks like I get a text element.
>
> Can somebody please clarify why I am getting what I do not expect?
>
> Thanks
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: parsing an xml document
Posted by Jason Jesso <jj...@global-matrix.com>.
Thanks. I missed that.
Jason
Lenny Hoffman wrote:
> Hi Jason,
>
> > DOM_Node sendXml = actionNode.getNextSibling(); to give me the SENDXML
> > node ( instead I get a ACTION node )
>
> The sibling of an ACTION node is an ACTION node, SENDXML is a sibling of
> OBJECT node. You probably want to do this:
>
> DOM_Node sendXml = objectNode.getNextSibling();
>
> Regards,
>
> Lenny
>
> -----Original Message-----
> From: Jason Jesso [mailto:jjesso@global-matrix.com]
> Sent: Monday, February 18, 2002 10:43 AM
> To: Juergen Hermann
> Cc: xerces-c-dev@xml.apache.org
> Subject: Re: parsing an xml document
>
> Ok. That helps. I still don't understand why a call to getNextSibling
> gives me an ACTION element.
>
> I still would expect
>
> DOM_Node objectNode = actionNode.getFirstChild(); to give me the OBJECT
> node ( and it does )
>
> DOM_Node sendXml = actionNode.getNextSibling(); to give me the SENDXML
> node ( instead I get a ACTION node )
>
> AND
>
> DOM_Node sendHtml = actionNode.getNextSibling(); to give me the SENDHTML
> nodw ( instead I get a ACTION node )
>
> static void
> build( DOM_Document doc )
> {
> // get the list of actions
> DOM_NodeList nl = doc.getElementsByTagName("ACTION");
>
> cout << "Number of actions = " << nl.getLength() << endl << endl;
>
> // process each action
> for ( int actionNum = 0; actionNum < nl.getLength(); actionNum++ ){
>
> // the current action node
> DOM_Node actionNode = nl.item( actionNum );
>
> // OBJECT node
> DOM_Node objectNode = actionNode.getFirstChild();
> if ( objectNode != NULL )
> cout << objectNode.getNodeName() <<
> objectNode.getNodeValue() << endl;
>
> // SENDXML node
> DOM_Node sendXml = actionNode.getNextSibling();
> if ( sendXml != NULL )
> cout << sendXml.getNodeName() << sendXml.getNodeValue()
> << endl;
>
> // SENDHTML node
> DOM_Node sendHtml = actionNode.getNextSibling();
> if ( sendHtml != NULL )
> cout << sendHtml.getNodeName() << sendHtml.getNodeValue()
> << endl;
>
> cout << endl;
>
> } /* for */
> }
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE SCRIPT SYSTEM "mtxXml.dtd" >
> <SCRIPT>
>
> <ACTION>
> <OBJECT>
> <FREE_FORM>
> <LITERAL>EAS</LITERAL><VARIABLE>username</VARIABLE>
> <LITERAL>GBL</LITERAL><VARIABLE>password</VARIABLE>
> </FREE_FORM>
> </OBJECT>
> <SENDXML/>
> </ACTION>
>
> <ACTION>
> <OBJECT>
> <FREE_FORM>
> <LITERAL>QBN</LITERAL>
> </FREE_FORM>
> </OBJECT>
> <SENDXML/>
> </ACTION>
>
> <ACTION>
> <OBJECT>
> <FREE_FORM>
> <LITERAL>XAS</LITERAL>
> </FREE_FORM>
> </OBJECT>
> <SENDXML/>
> <SENDHTML/>
> </ACTION>
>
> </SCRIPT>
>
> Juergen Hermann wrote:
>
> > On Mon, 18 Feb 2002 09:58:20 -0500, Jason Jesso wrote:
> >
> > > <ACTION>
> > > <OBJECT>
> >
> > Between action and object, there is _whitespace_, which results in a
> > text node if you do not parse with a DTD that enables the parser to
> > recognize this as _ignorable_ whitespace.
> >
> > Ciao, Jürgen
> >
> > --
> > Jürgen Hermann, Developer (jhe@webde-ag.de)
> > WEB.DE AG, http://webde-ag.de/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
RE: parsing an xml document
Posted by Lenny Hoffman <le...@earthlink.net>.
Hi Jason,
> DOM_Node sendXml = actionNode.getNextSibling(); to give me the SENDXML
> node ( instead I get a ACTION node )
The sibling of an ACTION node is an ACTION node, SENDXML is a sibling of
OBJECT node. You probably want to do this:
DOM_Node sendXml = objectNode.getNextSibling();
Regards,
Lenny
-----Original Message-----
From: Jason Jesso [mailto:jjesso@global-matrix.com]
Sent: Monday, February 18, 2002 10:43 AM
To: Juergen Hermann
Cc: xerces-c-dev@xml.apache.org
Subject: Re: parsing an xml document
Ok. That helps. I still don't understand why a call to getNextSibling
gives me an ACTION element.
I still would expect
DOM_Node objectNode = actionNode.getFirstChild(); to give me the OBJECT
node ( and it does )
DOM_Node sendXml = actionNode.getNextSibling(); to give me the SENDXML
node ( instead I get a ACTION node )
AND
DOM_Node sendHtml = actionNode.getNextSibling(); to give me the SENDHTML
nodw ( instead I get a ACTION node )
static void
build( DOM_Document doc )
{
// get the list of actions
DOM_NodeList nl = doc.getElementsByTagName("ACTION");
cout << "Number of actions = " << nl.getLength() << endl << endl;
// process each action
for ( int actionNum = 0; actionNum < nl.getLength(); actionNum++ ){
// the current action node
DOM_Node actionNode = nl.item( actionNum );
// OBJECT node
DOM_Node objectNode = actionNode.getFirstChild();
if ( objectNode != NULL )
cout << objectNode.getNodeName() <<
objectNode.getNodeValue() << endl;
// SENDXML node
DOM_Node sendXml = actionNode.getNextSibling();
if ( sendXml != NULL )
cout << sendXml.getNodeName() << sendXml.getNodeValue()
<< endl;
// SENDHTML node
DOM_Node sendHtml = actionNode.getNextSibling();
if ( sendHtml != NULL )
cout << sendHtml.getNodeName() << sendHtml.getNodeValue()
<< endl;
cout << endl;
} /* for */
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE SCRIPT SYSTEM "mtxXml.dtd" >
<SCRIPT>
<ACTION>
<OBJECT>
<FREE_FORM>
<LITERAL>EAS</LITERAL><VARIABLE>username</VARIABLE>
<LITERAL>GBL</LITERAL><VARIABLE>password</VARIABLE>
</FREE_FORM>
</OBJECT>
<SENDXML/>
</ACTION>
<ACTION>
<OBJECT>
<FREE_FORM>
<LITERAL>QBN</LITERAL>
</FREE_FORM>
</OBJECT>
<SENDXML/>
</ACTION>
<ACTION>
<OBJECT>
<FREE_FORM>
<LITERAL>XAS</LITERAL>
</FREE_FORM>
</OBJECT>
<SENDXML/>
<SENDHTML/>
</ACTION>
</SCRIPT>
Juergen Hermann wrote:
> On Mon, 18 Feb 2002 09:58:20 -0500, Jason Jesso wrote:
>
> > <ACTION>
> > <OBJECT>
>
> Between action and object, there is _whitespace_, which results in a
> text node if you do not parse with a DTD that enables the parser to
> recognize this as _ignorable_ whitespace.
>
> Ciao, Jürgen
>
> --
> Jürgen Hermann, Developer (jhe@webde-ag.de)
> WEB.DE AG, http://webde-ag.de/
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: parsing an xml document
Posted by Jason Jesso <jj...@global-matrix.com>.
Ok. That helps. I still don't understand why a call to getNextSibling
gives me an ACTION element.
I still would expect
DOM_Node objectNode = actionNode.getFirstChild(); to give me the OBJECT
node ( and it does )
DOM_Node sendXml = actionNode.getNextSibling(); to give me the SENDXML
node ( instead I get a ACTION node )
AND
DOM_Node sendHtml = actionNode.getNextSibling(); to give me the SENDHTML
nodw ( instead I get a ACTION node )
static void
build( DOM_Document doc )
{
// get the list of actions
DOM_NodeList nl = doc.getElementsByTagName("ACTION");
cout << "Number of actions = " << nl.getLength() << endl << endl;
// process each action
for ( int actionNum = 0; actionNum < nl.getLength(); actionNum++ ){
// the current action node
DOM_Node actionNode = nl.item( actionNum );
// OBJECT node
DOM_Node objectNode = actionNode.getFirstChild();
if ( objectNode != NULL )
cout << objectNode.getNodeName() <<
objectNode.getNodeValue() << endl;
// SENDXML node
DOM_Node sendXml = actionNode.getNextSibling();
if ( sendXml != NULL )
cout << sendXml.getNodeName() << sendXml.getNodeValue()
<< endl;
// SENDHTML node
DOM_Node sendHtml = actionNode.getNextSibling();
if ( sendHtml != NULL )
cout << sendHtml.getNodeName() << sendHtml.getNodeValue()
<< endl;
cout << endl;
} /* for */
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE SCRIPT SYSTEM "mtxXml.dtd" >
<SCRIPT>
<ACTION>
<OBJECT>
<FREE_FORM>
<LITERAL>EAS</LITERAL><VARIABLE>username</VARIABLE>
<LITERAL>GBL</LITERAL><VARIABLE>password</VARIABLE>
</FREE_FORM>
</OBJECT>
<SENDXML/>
</ACTION>
<ACTION>
<OBJECT>
<FREE_FORM>
<LITERAL>QBN</LITERAL>
</FREE_FORM>
</OBJECT>
<SENDXML/>
</ACTION>
<ACTION>
<OBJECT>
<FREE_FORM>
<LITERAL>XAS</LITERAL>
</FREE_FORM>
</OBJECT>
<SENDXML/>
<SENDHTML/>
</ACTION>
</SCRIPT>
Juergen Hermann wrote:
> On Mon, 18 Feb 2002 09:58:20 -0500, Jason Jesso wrote:
>
> > <ACTION>
> > <OBJECT>
>
> Between action and object, there is _whitespace_, which results in a
> text node if you do not parse with a DTD that enables the parser to
> recognize this as _ignorable_ whitespace.
>
> Ciao, Jürgen
>
> --
> Jürgen Hermann, Developer (jhe@webde-ag.de)
> WEB.DE AG, http://webde-ag.de/
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: parsing an xml document
Posted by Juergen Hermann <jh...@web.de>.
On Mon, 18 Feb 2002 09:58:20 -0500, Jason Jesso wrote:
> <ACTION>
> <OBJECT>
Between action and object, there is _whitespace_, which results in a
text node if you do not parse with a DTD that enables the parser to
recognize this as _ignorable_ whitespace.
Ciao, Jürgen
--
Jürgen Hermann, Developer (jhe@webde-ag.de)
WEB.DE AG, http://webde-ag.de/
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org