You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by Paul Lalonde <pl...@neoptica.com> on 2006/09/12 20:18:47 UTC

Getting at the value in an XalanNode?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I have a chunk of code that returns a NodeRefList from a  
XPathEvaluator, and I'm stepping over the list getting an XalanNode *  
for each element.
I can print the node name, I can get its attributes and print their  
values,  but XalanNode::getNodeValue() just seems to return empty  
strings.
The code below consistently generates the output:
Node 1 float_array =
    Attrib id = box-Pos-array
    Attrib count = 24
Node 1 float_array =
    Attrib id = box-0-Normal-array
    Attrib count = 18
when run on this fragment of XML:

	<float_array id="box-Pos-array" count="24">
           -0.5  0.5  0.5
            0.5  0.5  0.5
           -0.5 -0.5  0.5
            0.5 -0.5  0.5
           -0.5  0.5 -0.5
            0.5  0.5 -0.5
           -0.5 -0.5 -0.5
            0.5 -0.5 -0.5
         </float_array>
	<float_array id="box-0-Normal-array" count="18">
            1.0  0.0  0.0
           -1.0  0.0  0.0
            0.0  1.0  0.0
            0.0 -1.0  0.0
            0.0  0.0  1.0
            0.0  0.0 -1.0
         </float_array>


            tsAssert(theResult.getLength() > 0);
             for(size_t i=0;i<theResult.getLength();++i) {
                     XalanNode *nd = theResult.item(i);
                     cout << "Node " << nd->getNodeType() << " "
                          << TranscodeToLocalCodePage(nd->getNodeName())
                          << " = "
                          << TranscodeToLocalCodePage(nd->getNodeValue 
())
                          << endl;

                     const XalanNamedNodeMap * attribs = nd- 
 >getAttributes();
                     for(size_t j=0;j<attribs->getLength();++j) {
                         cout << "   Attrib " << attribs->item(j)- 
 >getNodeName()
                                              << " = "
                                              << attribs->item(j)- 
 >getNodeValue()
                                              << endl;
                     }
             }

Is there any obvious reason why nd->getNodeValue() isn't returning me  
my data?

Thanks,
     Paul

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (Darwin)

iD8DBQFFBvoHr7+oA6AsvAkRAnojAKDlbf4e9V1/zn3UF3QWgsTIadmd6QCbBf9Y
BRR1Sxz0YBEq4h8xxjNsWGY=
=3CZD
-----END PGP SIGNATURE-----

Re: Getting at the value in an XalanNode?

Posted by Jayant Das <ja...@bankofamerica.com>.
It has been too long. Hopefully you have a working solution by now. But here
is the solution any way -
As David said, you gotta get to the text node of the element to get a value

		XalanNode *node = theEvaluator1->selectSingleNode(
					theDOMSupport,
					theContextNode,
					XalanDOMString(xpath).c_str(),
					(*thePrefixResolver1));
		cout<<"outputA is "<<node->getNodeValue()<<endl;  //No value
		cout<<"outputB is "<<node->getNodeName()<<endl;
		XalanNode *child = node->getFirstChild();
		// For a non-leaf node, this gives the child node or whitespace(if any)
		// For a leaf node, this gives the text node
		if (child!=NULL) {
			result = 	string(convertXalanDOMString(child->getNodeValue()));
			if (trim(result).length()>0) {
				
				cout<<"got it "<<trim(result)<<endl;
			} // No need to go further. You had a leaf node
			else {
                  
				  while (child!=0) {
					  if (child->getFirstChild()!=NULL) { //This is NULL for whitespace as
the current node
					      cout<<"Value is "<<child->getFirstChild()->getNodeValue()<<endl;
					      cout<<"output2 is "<<child->getNodeName()<<endl;

					  }
					  child = child->getNextSibling();

				  }
			}
		}
Note: convertXalanDOMString is my custom function - you may ignore it.
This code snippet would give you values for "/bookstore/book[1]"  and also
for "/bookstore/book[1]/author"

For your second question about year, you have to get a nodelist and then
iterate through it to get individual years. I do not know of any way for
xapth to give it directly.
Try the theEvaluator1->selectNodeList(....)    Hopefully this helps. 


Shalmi wrote:
> 
> 
> David Bertoni wrote:
>> 
>> Paul Lalonde wrote:
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>> 
>>> So now I'm further confused - I tried calling XalanNode::getChildren(), 
>>> but recieved an XalanDOMException with value 9 = NOT_SUPPORTED_ERR.
>>> Clearly I'm asking for the wrong thing.  What's the right thing?
>>> 
>> 
>> This is a deliberate limitation in Xalan-C's implementation, to save 
>> memory.  Because the processor never needs to see a list of the children, 
>> only the iterative member functions are implemented.  Try the following
>> code:
>> 
>> XalanNode* child = node->getFirstChild();
>> 
>> while(child != 0)
>> {
>>      ...
>> 
>>      child = child->getNextSibling();
>> }
>> 
>> 
>> Dave
>> 
>> 
> 
> Hi,
> Consider following XML
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <bookstore>
> <book category="CHILDREN">
>   <title lang="en">Harry Potter</title>
>   <author>J K. Rowling</author>
>   <year>2005</year>
>   <price>29.99</price>
> </book>
> <book category="WEB">
>   <title lang="en">Learning XML</title>
>   <author>Erik T. Ray</author>
>   <year>2003</year>
>   <price>39.95</price>
> </book>
> </bookstore>
> 
> It has four text nodes/elment(?). So if i run "/bookstore/book[1]"
> getNodeValue() on this,what should i get?
> I did traversed through the node and print it,but in this case I get null
> string. The same piece of code works for "/bookstore/book/year".
> I mean if i have to print following output what would be the generic
> approach?
> 1. For "/bookstore/book[1]"  i should get <book category="CHILDREN">
>   <title lang="en">Harry Potter</title>
>   <author>J K. Rowling</author>
>   <year>2005</year>
>   <price>29.99</price>
> </book>
> 
> 2. For "/bookstore/book/year" i should get
> 2005
> 2003
> 
> Thanks
> Shalmi
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Getting-at-the-value-in-an-XalanNode--tp6271680p15138243.html
Sent from the Xalan - C - Users mailing list archive at Nabble.com.


Re: Getting at the value in an XalanNode?

Posted by David Bertoni <db...@apache.org>.
Shalmi wrote:
> 
> David Bertoni wrote:
>> Paul Lalonde wrote:
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>>
>>> So now I'm further confused - I tried calling XalanNode::getChildren(), 
>>> but recieved an XalanDOMException with value 9 = NOT_SUPPORTED_ERR.
>>> Clearly I'm asking for the wrong thing.  What's the right thing?
>>>
>> This is a deliberate limitation in Xalan-C's implementation, to save 
>> memory.  Because the processor never needs to see a list of the children, 
>> only the iterative member functions are implemented.  Try the following
>> code:
>>
>> XalanNode* child = node->getFirstChild();
>>
>> while(child != 0)
>> {
>>      ...
>>
>>      child = child->getNextSibling();
>> }
>>
>>
>> Dave
>>
>>
> 
> Hi,
> Consider following XML
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <bookstore>
> <book category="CHILDREN">
>   <title lang="en">Harry Potter</title>
>   <author>J K. Rowling</author>
>   <year>2005</year>
>   <price>29.99</price>
> </book>
> <book category="WEB">
>   <title lang="en">Learning XML</title>
>   <author>Erik T. Ray</author>
>   <year>2003</year>
>   <price>39.95</price>
> </book>
> </bookstore>
> 
> It has four text nodes/elment(?). So if i run "/bookstore/book[1]"
> getNodeValue() on this,what should i get?
Element nodes do not have values in the "DOM" representation of the data 
model.  The values you are looking for are their text node children. And 
don't forget that the whitespace you're using for indentation is also 
considered part of the document's content, and will be represented by text 
nodes.

There are some helper functions in DOMSupport/DOMServices.hpp that will 
retrieve what the XPath recommendation defines as the data in a particular 
node.  You could use those, although I'm not sure they will return what 
you're looking for, since I'm not really sure what you're looking for.

> I did traversed through the node and print it,but in this case I get null
> string. The same piece of code works for "/bookstore/book/year".
> I mean if i have to print following output what would be the generic
> approach?
> 1. For "/bookstore/book[1]"  i should get <book category="CHILDREN">
>   <title lang="en">Harry Potter</title>
>   <author>J K. Rowling</author>
>   <year>2005</year>
>   <price>29.99</price>
> </book>
XalanNode::getNodeValue() returns _values_ of nodes, not their markup.  If 
you want the generate the markup for a node, take a look at the 
SerializeNodeSet sample application.

> 
> 2. For "/bookstore/book/year" i should get
> 2005
> 2003
Those are the values of the text node children of the year element nodes.

Dave

Re: Getting at the value in an XalanNode?

Posted by Shalmi <sh...@gmail.com>.

David Bertoni wrote:
> 
> Paul Lalonde wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>> 
>> So now I'm further confused - I tried calling XalanNode::getChildren(), 
>> but recieved an XalanDOMException with value 9 = NOT_SUPPORTED_ERR.
>> Clearly I'm asking for the wrong thing.  What's the right thing?
>> 
> 
> This is a deliberate limitation in Xalan-C's implementation, to save 
> memory.  Because the processor never needs to see a list of the children, 
> only the iterative member functions are implemented.  Try the following
> code:
> 
> XalanNode* child = node->getFirstChild();
> 
> while(child != 0)
> {
>      ...
> 
>      child = child->getNextSibling();
> }
> 
> 
> Dave
> 
> 

Hi,
Consider following XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>
</bookstore>

It has four text nodes/elment(?). So if i run "/bookstore/book[1]"
getNodeValue() on this,what should i get?
I did traversed through the node and print it,but in this case I get null
string. The same piece of code works for "/bookstore/book/year".
I mean if i have to print following output what would be the generic
approach?
1. For "/bookstore/book[1]"  i should get <book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

2. For "/bookstore/book/year" i should get
2005
2003

Thanks
Shalmi


-- 
View this message in context: http://www.nabble.com/Getting-at-the-value-in-an-XalanNode--tf2260533.html#a12628165
Sent from the Xalan - C - Users mailing list archive at Nabble.com.


Re: Getting at the value in an XalanNode?

Posted by Paul Lalonde <pl...@neoptica.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thank you David!  That works a charm!

Paul

On 12-Sep-06, at 12:51 PM, David Bertoni wrote:

> Paul Lalonde wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>> So now I'm further confused - I tried calling  
>> XalanNode::getChildren(), but recieved an XalanDOMException with  
>> value 9 = NOT_SUPPORTED_ERR.
>> Clearly I'm asking for the wrong thing.  What's the right thing?
>
> This is a deliberate limitation in Xalan-C's implementation, to  
> save memory.  Because the processor never needs to see a list of  
> the children, only the iterative member functions are implemented.   
> Try the following code:
>
> XalanNode* child = node->getFirstChild();
>
> while(child != 0)
> {
>     ...
>
>     child = child->getNextSibling();
> }
>
>
> Dave
>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (Darwin)

iD8DBQFFBxQOr7+oA6AsvAkRAhQSAKCLsqlcgeWVcIl5faqBfN8ms6zKVACgnchN
ML5Kv4VhX+2u8Z5KbkexTGc=
=LmWh
-----END PGP SIGNATURE-----

Re: Getting at the value in an XalanNode?

Posted by David Bertoni <db...@apache.org>.
Paul Lalonde wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> So now I'm further confused - I tried calling XalanNode::getChildren(), 
> but recieved an XalanDOMException with value 9 = NOT_SUPPORTED_ERR.
> Clearly I'm asking for the wrong thing.  What's the right thing?
> 

This is a deliberate limitation in Xalan-C's implementation, to save 
memory.  Because the processor never needs to see a list of the children, 
only the iterative member functions are implemented.  Try the following code:

XalanNode* child = node->getFirstChild();

while(child != 0)
{
     ...

     child = child->getNextSibling();
}


Dave

Re: Getting at the value in an XalanNode?

Posted by Paul Lalonde <pl...@neoptica.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

So now I'm further confused - I tried calling XalanNode::getChildren 
(), but recieved an XalanDOMException with value 9 = NOT_SUPPORTED_ERR.
Clearly I'm asking for the wrong thing.  What's the right thing?

Paul

On 12-Sep-06, at 12:28 PM, David Bertoni wrote:

> Paul Lalonde wrote:
>> I have a chunk of code that returns a NodeRefList from a  
>> XPathEvaluator, and I'm stepping over the list getting an  
>> XalanNode * for each element.
>> I can print the node name, I can get its attributes and print  
>> their values,  but XalanNode::getNodeValue() just seems to return  
>> empty strings.
>> The code below consistently generates the output:
>
> ...
>
>
>> Is there any obvious reason why nd->getNodeValue() isn't returning  
>> me my data?
>>
>
> An element has no "value" of its own.  In this case, the value is  
> in the text node children, so you would need to iterate through them.
>
> Dave
>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Darwin)

iD8DBQFFBwwzr7+oA6AsvAkRAp6pAKDsTRgy9s+hWl8PYHy4+9lmMk5YlgCgrE8s
dvsdnMvsMKYirjTKZBJckac=
=cWKn
-----END PGP SIGNATURE-----

Re: Getting at the value in an XalanNode?

Posted by David Bertoni <db...@apache.org>.
Paul Lalonde wrote:
> I have a chunk of code that returns a NodeRefList from a XPathEvaluator, 
> and I'm stepping over the list getting an XalanNode * for each element.
> I can print the node name, I can get its attributes and print their 
> values,  but XalanNode::getNodeValue() just seems to return empty strings.
> The code below consistently generates the output:

...


> 
> Is there any obvious reason why nd->getNodeValue() isn't returning me my 
> data?
>

An element has no "value" of its own.  In this case, the value is in the 
text node children, so you would need to iterate through them.

Dave