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 "Purdy, Edgar M" <ed...@lmco.com> on 2003/12/24 19:04:19 UTC

Parser data extraction code snippet - Help Please





Here is the code that I implemented.  At the bottom of the file is the complete XML file that I am parsing.
I capture all six person id's and their values.   The i loop captures all the id's. The k loop is where I try to capture the tags and values within each person id block.   My approach is not correct. But I think I am close. Is there a suggestion to capture the rest of these tags?

	DOMNodeList* personList= 0;
	DOMNodeList* childList =0;
	DOMDocument* myDocument = 0;
	static XMLCh *pOutputEncoding[5];
    try
    {
	
	parser->parse(gXmlFile);

  	myDocument = parser->getDocument();
	pOutputEncoding[0] = XMLString::transcode( "person" );
	pOutputEncoding[1] = XMLString::transcode( "id" );
	pOutputEncoding[2] = XMLString::transcode( "email");
	pOutputEncoding[3] = XMLString::transcode( "link");

 	personList = myDocument->getElementsByTagName(pOutputEncoding[0]);
	
	DOMNode* currentNode;
	DOMNode* currTagName;
	char* Node_Name;
	
	DOMNamedNodeMap* nextNode;

	for (int i=0; i<personList->getLength(); i++) 		
		{
		currentNode = personList->item(i);
		nextNode = currentNode->getAttributes();


		currTagName = nextNode->getNamedItem(pOutputEncoding[1]);
		Node_Name = XMLString::transcode(currTagName->getNodeValue());
		printf("person id = %s\n", Node_Name);
		
		childList = currentNode->getChildNodes();
		for(int k = 0; k<childList->getLength(); k++)
		{
			currentNode = childList->item(k);

			if(nextNode = currentNode->getAttributes())
			{
				currTagName = nextNode->getNamedItem(currentNode->getNodeName());
				Node_Name = XMLString::transcode(currTagName->getNodeValue());
				printf("email = %s\n", Node_Name);
			}
		}

*********************************************************************************************************
XMLFILE:

  <personnel>
  	<person id="Big.Boss">
  		<name>
  			<family>Boss</family> 
  			<given>Big</given> 
  		</name>
  		<email>chief@foo.com</email> 
  		<link subordinates="one.worker two.worker three.worker four.worker five.worker" /> 
  	</person>
  	<person id="one.worker">
  		<name>
  			<family>Worker</family> 
  			<given>One</given> 
  		</name>
  		<email>one@foo.com</email> 
  		<link manager="Big.Boss" /> 
  	</person>
  	<person id="two.worker">
  		<name>
  			<family>Worker</family> 
  			<given>Two</given> 
  		</name>
  		<email>two@foo.com</email> 
  		<link manager="Big.Boss" /> 
  	</person>
  	<person id="three.worker">
  		<name>
  			<family>Worker</family> 
  			<given>Three</given> 
  		</name>
  		<email>three@foo.com</email> 
  		<link manager="Big.Boss" /> 
  	</person>
  	<person id="four.worker">
  		<name>
  			<family>Worker</family> 
  			<given>Four</given> 
  		</name>
  		<email>four@foo.com</email> 
  		<link manager="Big.Boss" /> 
  	</person>
  	<person id="five.worker">
  		<name>
  			<family>Worker</family> 
  			<given>Five</given> 
  		</name>
  		<email>five@foo.com</email> 
  		<link manager="Big.Boss" /> 
  	</person>
  </personnel>

---------------------------------------------------------------------
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