I'm trying to parse a XML document using Xerces-C++. I just would like to be able to search for a element by its id. I have written the following code but it doesn't work. ...

try {
        XMLPlatformUtils::Initialize();
    }
    catch(XMLException& e) {
        char* message = XMLString::transcode( e.getMessage() );
        cout << "XML toolkit initialization error: " << message << endl;
        XMLString::release( &message );
    }

    XMLCh tempStr[100];
    XMLString::transcode("LS", tempStr, 99);
    DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
    DOMLSParser* parser = ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);

    char *filename = "C:\\odx1.xml";


    xercesc::DOMDocument *doc = 0;

    try {
        doc = parser->parseURI(filename);
        DOMElement *element = doc->getElementById(XMLString::transcode("test"));
        if(element != NULL) cout << "element found";
        cout << "DONE";
    }
    catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Exception message is: \n"
                << message << "\n";
        XMLString::release(&message);
        return;
    }
    catch (const DOMException& toCatch) {
        char* message = XMLString::transcode(toCatch.msg);
        cout << "Exception message is: \n"
                << message << "\n";
        XMLString::release(&message);
        return;
    }
    catch (...) {
        cout << "Unexpected Exception \n" ;
        return ;
    }

    parser->release();
    XMLPlatformUtils::Terminate();
}
...

The XML is:

<ODX MODEL-VERSION="2.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="odx.xsd">
  <DIAG-LAYER-CONTAINER ID="test">
    test done
  </DIAG-LAYER-CONTAINER>
</ODX>

I expect it to print "element found" but the program terminates correctly without printing "element found".

Anyway...in the XSD file associated to the XML document the element I'm searching for has <xsd:attribute name="ID" type="xsd:ID" use="required"/> So I expect the element to be returned by getElementById.

有帮助吗?

解决方案 2

The solution is:

XMLCh tempStr[100];
    XMLString::transcode("LS", tempStr, 99);
    DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
    DOMLSParser* parser = ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
    DOMConfiguration* conf = parser->getDomConfig ();
    conf->setParameter(XMLUni::fgXercesSchema, true);
    char *filename = "C:\\odx1.xml";


    xercesc::DOMDocument *doc = 0;

    try {
        doc = parser->parseURI(filename);
        DOMElement *element = doc->getElementById(XMLString::transcode("test"));
        if(element != NULL) cout << "element found";
        cout << "DONE";
    }

其他提示

Please take a look at this

Returns the DOMElement whose ID is given by elementId. If no such element exists, returns null. Behavior is not defined if more than one element has this ID. The DOM implementation must have information that says which attributes are of type ID. Attributes with the name "ID" are not of type ID unless so defined. Implementations that do not know whether attributes are of type ID or not are expected to return null.

Maybe you can get the element by other means ? as tag name ?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top