I have the following type of XML:

 

<ns1:IPEModelInput xmlns:ns1="http://creare/ipe";>

    <ns1:system>

        <ns1:pinchpoint>

            <ns1:boundingCylinder1>1</ns1:boundingCylinder1>

            <ns1:boundingCylinder2>2</ns1:boundingCylinder2>

            <ns1:location>NECK</ns1:location>

            <ns1:flow>

                <ns1:closure>Open</ns1:closure>

            </ns1:flow>

        </ns1:pinchpoint>

        <ns1:pinchpoint>

            <ns1:boundingCylinder1>2</ns1:boundingCylinder1>

            <ns1:boundingCylinder2>3</ns1:boundingCylinder2>

            <ns1:location>R_SHOULDER</ns1:location>

            <ns1:flow>

                <ns1:resistance>Medium</ns1:resistance>

            </ns1:flow>

        </ns1:pinchpoint>

        <ns1:pinchpoint>

            <ns1:boundingCylinder1>3</ns1:boundingCylinder1>

            <ns1:boundingCylinder2>4</ns1:boundingCylinder2>

            <ns1:location>R_ELBOW</ns1:location>

            <ns1:flow>

                <ns1:resistance>Medium</ns1:resistance>

            </ns1:flow>

        </ns1:pinchpoint>

    </ns1:system>

</ns1:IPEModelInput>

 

 

I want to be able to get a list of the "ns1:pinchpoint" nodes, and then
step through each node in the list and extract the boundingCylinder1,
boundingCylinder2, location, and flow/resistance or flow/closure data.
I have been able to get the list of pinchpoint nodes, and then I can
extract *one* of the desired sub-nodes (such as boundingCylinder1), but
I can not then extract the other child nodes.  Here is my code:

 

 

XMLPlatformUtils::Initialize();

      XPathEvaluator::initialize();

      

      XercesDOMParser *parser = new XercesDOMParser;

      

      parser->setDoNamespaces(true);

      parser->setDoSchema(true);

      parser->setValidationSchemaFullChecking(true);

      parser->setCreateEntityReferenceNodes(true);

      

      DOMDocument *doc = 0;

      

      parser->parse(argv[1]);

      doc = parser->getDocument();

      // The XPath expression to evaluate

      std::string xpath =
"/ns1:IPEModelInput/ns1:system/ns1:pinchpoint";

      

      XercesDOMSupport dom_support;

      XercesParserLiaison parser_liaison;

      XalanDocument *xalan_document = parser_liaison.createDocument(doc,
false, true);

      XalanNode * root_context_node = xalan_document;

      XalanElement *namespace_node =
xalan_document->getDocumentElement();

      

      const XalanDOMString expression(xpath.c_str());

      XPathEvaluator evaluator;

      NodeRefList nodelist;

      

      evaluator.selectNodeList(

            nodelist,

            dom_support,

            root_context_node,

            expression.c_str(),

            namespace_node);

      

      NodeRefList::size_type length = nodelist.getLength();

      cout << "number of nodes = " << length << endl;

      for (unsigned int i = 0; i < nodelist.getLength(); ++i) {

            // Get this pinchpoint node

            char newxpath[1024];

            sprintf(newxpath,"%s[%d]",xpath.c_str(),i+1);

            XalanDOMString newexpression(newxpath);

            cout << "pinchpoint path = " << newxpath << endl;

            XalanNode* node = nodelist.item(i);

            XalanNode* const theContextNode =

                  evaluator.selectSingleNode(

                        dom_support,

                        node,

                        newexpression.c_str(),

                        namespace_node);

 

            // Get the location - THIS WORKS

XalanDOMString locationDOMStr("ns1:location");

            const XObjectPtr locationResult(

                  evaluator.evaluate(

                        dom_support,

                        theContextNode,

                        locationDOMStr.c_str(),

                        namespace_node));

            assert(locationResult.null() == false);

            cout << "The location = "

                   << locationResult->str()

                   << endl;

            

            // Get the first bounding cylinder - THIS DOESN'T WORK

            XalanDOMString boundaryDOMStr("ns1:boundingCylinder1");

            const XObjectPtr boundaryResult(

                  evaluator.evaluate(

                        dom_support,

                        theContextNode,

                        boundaryDOMStr.c_str(),

                        namespace_node));

            assert(boundaryResult.null() == false);

            cout << "Boundary 1 = "

                   << boundaryResult->str()

                   << endl;

      }

 

The "assert" before I print boundaryResult is always thrown.

 

How do I step through each "pinchpoint" object and extract the data?

 

Many, many thanks for your help.

 

John P. Wilson

Creare Inc.

P.O. Box 71

Hanover, NH 03755

603-640-2474

Fax 603-643-4657

 

Reply via email to