XercesDomParser
-
Hi i had been using XercersDomParser for parsing the XML file in C++ I am not familiar with XML. In the follwing XML file <?xml version="1.0" encoding="UTF-8"?> <Locomotives> <Locomotive xmlns="http://localhost:8080/source/schemas/locomotive.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost:8080/source/schemas/locomotive.xsd http://localhost:8080/source/schemas/locomotive.xsd"> <LocoId>9001</LocoId> <LocoRunNumber>UnReg</LocoRunNumber> <LocomotiveGPSData> <SampleTime>2008-07-13T13:37:33</SampleTime> <Latitude>-32.86693333</Latitude> <Longitude>151.75333333</Longitude> <Speed>-1</Speed> <Direction>-1</Direction> <KmPostPosition>178500</KmPostPosition> <TrackNumber>85</TrackNumber> </LocomotiveGPSData> <Events> <Alarms>0</Alarms> <Faults>0</Faults> </Events> </Locomotive> <Locomotive xmlns="http://localhost:8080/source/schemas/locomotive.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost:8080/source/schemas/locomotive.xsd http://localhost:8080/source/schemas/locomotive.xsd"> <LocoId>9002</LocoId> <LocoRunNumber>UnReg</LocoRunNumber> <LocomotiveGPSData> <SampleTime>2008-07-13T23:59:56</SampleTime> <Latitude>-32.61888333</Latitude> <Longitude>151.14988333</Longitude> <Speed>-1</Speed> <Direction>-1</Direction> <KmPostPosition>239000</KmPostPosition> <TrackNumber>87</TrackNumber> </LocomotiveGPSData> <Events> <Alarms>0</Alarms> <Faults>0</Faults> </Events> </Locomotives> Eventhough i can traverse through XML file through DOMTreeWalker but can't get idea to store Values in a strucured <list> the structure look like this typedef struct { Loco_Id LocoId ; string /*DateTime*/ SampleTime ; float Lattitude; float Longitude; float Speed ; float Direction; int KmPostPosition; int TrackNumber; int Alarms; int Failures ; }LocoDetails Can any one tell how to access the value from the XML file and store to a list
-
Hi i had been using XercersDomParser for parsing the XML file in C++ I am not familiar with XML. In the follwing XML file <?xml version="1.0" encoding="UTF-8"?> <Locomotives> <Locomotive xmlns="http://localhost:8080/source/schemas/locomotive.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost:8080/source/schemas/locomotive.xsd http://localhost:8080/source/schemas/locomotive.xsd"> <LocoId>9001</LocoId> <LocoRunNumber>UnReg</LocoRunNumber> <LocomotiveGPSData> <SampleTime>2008-07-13T13:37:33</SampleTime> <Latitude>-32.86693333</Latitude> <Longitude>151.75333333</Longitude> <Speed>-1</Speed> <Direction>-1</Direction> <KmPostPosition>178500</KmPostPosition> <TrackNumber>85</TrackNumber> </LocomotiveGPSData> <Events> <Alarms>0</Alarms> <Faults>0</Faults> </Events> </Locomotive> <Locomotive xmlns="http://localhost:8080/source/schemas/locomotive.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost:8080/source/schemas/locomotive.xsd http://localhost:8080/source/schemas/locomotive.xsd"> <LocoId>9002</LocoId> <LocoRunNumber>UnReg</LocoRunNumber> <LocomotiveGPSData> <SampleTime>2008-07-13T23:59:56</SampleTime> <Latitude>-32.61888333</Latitude> <Longitude>151.14988333</Longitude> <Speed>-1</Speed> <Direction>-1</Direction> <KmPostPosition>239000</KmPostPosition> <TrackNumber>87</TrackNumber> </LocomotiveGPSData> <Events> <Alarms>0</Alarms> <Faults>0</Faults> </Events> </Locomotives> Eventhough i can traverse through XML file through DOMTreeWalker but can't get idea to store Values in a strucured <list> the structure look like this typedef struct { Loco_Id LocoId ; string /*DateTime*/ SampleTime ; float Lattitude; float Longitude; float Speed ; float Direction; int KmPostPosition; int TrackNumber; int Alarms; int Failures ; }LocoDetails Can any one tell how to access the value from the XML file and store to a list
There are a lot of ways to do it - here's some suggestions:
- The DOMTreeWalker lets you set up a node filter. You can set up a filter to accept all element nodes whose tag is Locomotive. That means that each call to the DOMTreeWalker's nextNode method will step to the next Locomotive element. For each of these, you iterate through the found element's child nodes. Each child's tag name indicates which struct memeber they relate to, while the child's text node is the value that you'll convert to the correct type using string conversion functions like strtol.
- From the root node, apply an XPath of "//Locomotive". That'll get you all the Locomotive elements, which you can process as above.
- Get a DOMElement for the root element of the XML document. Use the getElementsByTagName method to retrieve all descendent nodes with the tag naem Locomotive. Process each node as indicated above.
Hope that helps you!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
There are a lot of ways to do it - here's some suggestions:
- The DOMTreeWalker lets you set up a node filter. You can set up a filter to accept all element nodes whose tag is Locomotive. That means that each call to the DOMTreeWalker's nextNode method will step to the next Locomotive element. For each of these, you iterate through the found element's child nodes. Each child's tag name indicates which struct memeber they relate to, while the child's text node is the value that you'll convert to the correct type using string conversion functions like strtol.
- From the root node, apply an XPath of "//Locomotive". That'll get you all the Locomotive elements, which you can process as above.
- Get a DOMElement for the root element of the XML document. Use the getElementsByTagName method to retrieve all descendent nodes with the tag naem Locomotive. Process each node as indicated above.
Hope that helps you!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
VVVimal wrote:
could you please give me a example
This[^] indicates this sample[^] which may be helpful, as well as many other pages.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
There are a lot of ways to do it - here's some suggestions:
- The DOMTreeWalker lets you set up a node filter. You can set up a filter to accept all element nodes whose tag is Locomotive. That means that each call to the DOMTreeWalker's nextNode method will step to the next Locomotive element. For each of these, you iterate through the found element's child nodes. Each child's tag name indicates which struct memeber they relate to, while the child's text node is the value that you'll convert to the correct type using string conversion functions like strtol.
- From the root node, apply an XPath of "//Locomotive". That'll get you all the Locomotive elements, which you can process as above.
- Get a DOMElement for the root element of the XML document. Use the getElementsByTagName method to retrieve all descendent nodes with the tag naem Locomotive. Process each node as indicated above.
Hope that helps you!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
DOMElement* elementRoot = domDocumet->getDocumentElement(); DOMTreeWalker *walker =domDocumet->createTreeWalker(elementRoot,DOMNodeFilter::SHOW_ELEMENT,NULL, true); DOMNode *pCurrent = NULL; for ( pCurrent = walker->getCurrentNode();pCurrent != 0; pCurrent = walker->nextNode() ) { if(pCurrent->getNodeType() == DOMNode::ELEMENT_NODE) { char *str = XMLString::transcode(pCurrent->getNodeName()); if(!strcmp(str,"LocoId")) int i=0; //myDetails->LocoId = XMLString::transcode(text->getNodeValue()); if(!strcmp(str,"Latitude")) //myDetails->Lattitude = XMLString::transcode(text->getNodeValue()); XMLString::release(&str); } } Could you please tell me where to set the filter as Locomotive you also mentioned "From the root node, apply an XPath of "//Locomotive " what does it mean Thanks you also mentioned as
-
DOMElement* elementRoot = domDocumet->getDocumentElement(); DOMTreeWalker *walker =domDocumet->createTreeWalker(elementRoot,DOMNodeFilter::SHOW_ELEMENT,NULL, true); DOMNode *pCurrent = NULL; for ( pCurrent = walker->getCurrentNode();pCurrent != 0; pCurrent = walker->nextNode() ) { if(pCurrent->getNodeType() == DOMNode::ELEMENT_NODE) { char *str = XMLString::transcode(pCurrent->getNodeName()); if(!strcmp(str,"LocoId")) int i=0; //myDetails->LocoId = XMLString::transcode(text->getNodeValue()); if(!strcmp(str,"Latitude")) //myDetails->Lattitude = XMLString::transcode(text->getNodeValue()); XMLString::release(&str); } } Could you please tell me where to set the filter as Locomotive you also mentioned "From the root node, apply an XPath of "//Locomotive " what does it mean Thanks you also mentioned as
Set the filter in the createTreeWalker method call.
VVVimal wrote:
you also mentioned "From the root node, apply an XPath of "//Locomotive " what does it mean
Using an XPath[^] library such as Xalan[^] or XQilla[^], you apply an XPath evaluator class to the DOM. That will give you an iterator that'll go through the nodes that match the XPath expression you gave to the XPath evaluator. Read up on XPath - it's a very useful tool to know how to use.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p