|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
DOM parsing
Hi friends,
consider the XML below: <?xml version="1.0" encoding="ISO-8859-1"?> <selected> <fi id="1">sam</fi> <fi id="2">john</fi> <fi id="3">tom</fi> <fi id="4">paul</fi> </selected> using DOM parsing, we can extract individual elements using methods like getElementbyId(). but if i have a XML of the form: <fi id="1">C:\Program Files\aa.txt</fi> <fi id="2">C:\Program Files\ab.txt</fi> where i need to extract values from the text files, how can i use DOM in this case. thanks in advance. |
|
#2
|
|||
|
|||
|
I don't get your problem :S
You can only use Document::getElementbyId() reliably if you do not have multiple elements with the same ID. The following example would violate this rule: <?xml version="1.0" encoding="ISO-8859-1"?> <selected> <fi id="1">sam</fi> <fi id="2">john</fi> </selected> <unselected> <fi id="1">tom</fi> <fi id="2">paul</fi> </unselected> Assuming that the element ID is unique, you could do: Code:
Element* pElement;
Node* pChild = NULL;
std::string strFileName;
pElement = pDocument->getElementbyId("1");
if (pElement)
pChild = pElement->getFirstChild();
while (pChild)
{
if (pChild->getNodeType() == Node::TEXT_NODE)
{
strFileName = pChild->getNodeValue();
break;
}
pChild = pChild->getNextSibling();
}
if (!strFileName.empty())
{
FILE* f = fopen(strFileName.c_str(), "r");
// now read from the file
}
Of course, you will want to include error handling ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > DOM parsing |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|