2013-03-15 81 views
1

我想使用Xerces-C++解析XML文檔。我只是希望能夠通過它的id來搜索元素。我寫了下面的代碼,但它不起作用。 ...無法使用Xerces-C++解析XML

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(); 
} 
... 

的XML是:

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

我希望它打印 「元素中找到」,但程序正確終止,並且不打印 「的元素中找到」。

無論如何...在與XML文檔關聯的XSD文件中,我正在搜索的元素有<xsd:attribute name="ID" type="xsd:ID" use="required"/> 所以我期望元素由getElementById返回。

+0

你能還添加XML? – 2013-03-15 14:34:19

+0

如果你想得到一些幫助,「它不起作用」是不夠的信息。你的代碼產生了什麼,你期望它產生了什麼? – undu 2013-03-15 14:34:24

+0

XSD文件如何與XML文檔關聯?也許你在XML文件中使用'schemaLocation'?或者,您也可以在軟件中指定模式(請參閱http://codesynthesis.com/~boris/blog/2010/03/15/validating-external-schemas-xerces-cxx/) – 2013-03-19 15:10:06

回答

2

解決的辦法是:

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"; 
    } 
3

請看看this

返回其ID由elementId給出了一個DOMElement。如果不存在這樣的 元素,則返回null。如果多個 元素具有此ID,則行爲未定義。 DOM實現必須具有信息 ,該信息說明哪些屬性是ID類型的。除非如此定義,名稱爲 「ID」的屬性不是類型ID。沒有 的實現知道屬性是否爲ID類型,預計返回 null。

也許你可以通過其他方式獲取元素?作爲標籤名稱?

+0

你知道如何將屬性定義爲id嗎? – salvo 2013-03-15 15:11:56

+0

@salvo你可能想爲此使用DTD,請參閱http://www.quackit.com/xml/tutorial/dtd_attribute_types_id.cfm。 – 2013-03-15 15:16:43

+0

無論如何...在與XML文檔關聯的XSD文件中,我正在搜索的元素有 So我期望元素由getElementById返回 – salvo 2013-03-18 10:02:53