XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 25th, 2004, 11:29 PM
fip0203 fip0203 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 50 fip0203 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 56 sec
Reputation Power: 5
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.

Reply With Quote
  #2  
Old April 28th, 2004, 03:58 AM
peteh peteh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 42 peteh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 22 m 20 sec
Reputation Power: 5
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > DOM parsing


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT