472,971 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,971 software developers and data experts.

xml parser newbie

i need to make parser to go trough node tree

i have this structure
<root>
<bookstore pass="1">
<book>
<name id="1">programming xml</name>
</book>
<book>
<name id="2">programming C#</name>
</book>
</bookstore>
</root>
which is not same all the time. sometimes it have more element in book and
sometimes is deeper like this.
this is just example xml
<root>
<bookstore pass="1">
<book>
<katalog num="1">
<name id="1">programming xml</name>
</katalog>
</book>
<book>
<name id="2">programming C#</name>
</book>
</bookstore>
</root>

i have 3 xml files
first xml where i read data and if i have attribute in bookstore i go and check
for name attribute. i took it and than i go to bookstore tag with same
attribute in second xml. and i need to write all elements in bookstore to third
xml (same structure), but i need to change name tag innertext with text from
first xml.

i was thinking

public void ParseNodeTree(string id, string path, XPathNavigator nav,
XmlTextWriter writer)
{
XPathNodeIterator iter = nav.Select(path);

while (iter.MoveNext())
{
if (iter.Current.GetAttribute("id", "") == id)
{
//change this node text with text from first file
}

writer.WriteStartElement(iter.Current.Name);

if (iter.Current.HasChildren)
{
//dont know how to get children path to put it in recursion
ParseNodeTree(id, path, nav, writer);
}
else
{
writer.WriteFullEndElement();
}
}
}
can someone please help me. i lost 2 days for this
i cant do it anymore
thanks!
Jun 27 '08 #1
3 1777
Maybe this would help you at least to move to the right direction:

if (iter.Current.HasChildren)
{
//dont know how to get children path to put it in recursion
XPathNavigator nodesNavigator = nodes.Current;
XPathNodeIterator nodes =
nodesNavigator.SelectChildren("katalog", "");
//or check other SelectChildren overloaded method

while (nodes.MoveNext())
// extract infos that you need from nodes.Current
ParseNodeTree(id, path, nav, writer);
}


"gigs" <gi**@hi.t-com.hrwrote in message
news:g1**********@ss408.t-com.hr...
i need to make parser to go trough node tree

i have this structure
<root>
<bookstore pass="1">
<book>
<name id="1">programming xml</name>
</book>
<book>
<name id="2">programming C#</name>
</book>
</bookstore>
</root>
which is not same all the time. sometimes it have more element in book and
sometimes is deeper like this.
this is just example xml
<root>
<bookstore pass="1">
<book>
<katalog num="1">
<name id="1">programming xml</name>
</katalog>
</book>
<book>
<name id="2">programming C#</name>
</book>
</bookstore>
</root>

i have 3 xml files
first xml where i read data and if i have attribute in bookstore i go and
check for name attribute. i took it and than i go to bookstore tag with
same attribute in second xml. and i need to write all elements in
bookstore to third xml (same structure), but i need to change name tag
innertext with text from first xml.

i was thinking

public void ParseNodeTree(string id, string path, XPathNavigator nav,
XmlTextWriter writer)
{
XPathNodeIterator iter = nav.Select(path);

while (iter.MoveNext())
{
if (iter.Current.GetAttribute("id", "") == id)
{
//change this node text with text from first file
}

writer.WriteStartElement(iter.Current.Name);

if (iter.Current.HasChildren)
{
//dont know how to get children path to put it in recursion
ParseNodeTree(id, path, nav, writer);
}
else
{
writer.WriteFullEndElement();
}
}
}
can someone please help me. i lost 2 days for this
i cant do it anymore
thanks!
Jun 27 '08 #2
thanks,

i need to check if current node have children, and than get names of that
children because they are different. can i somehow get node child names to put
in nodesNavigator.SelectChildren.

Daniel Cigic wrote:
Maybe this would help you at least to move to the right direction:

if (iter.Current.HasChildren)
{
//dont know how to get children path to put it in recursion
XPathNavigator nodesNavigator = nodes.Current;
XPathNodeIterator nodes =
nodesNavigator.SelectChildren("katalog", "");
//or check other SelectChildren overloaded method

while (nodes.MoveNext())
// extract infos that you need from nodes.Current
ParseNodeTree(id, path, nav, writer);
}


"gigs" <gi**@hi.t-com.hrwrote in message
news:g1**********@ss408.t-com.hr...
Jun 27 '08 #3
If this exercise is purely academic please ignore this response.

There is a much easier way to deal with XML in .Net. Just make sure you
have a schema (.xsd) file that defines your formating. This should
include all the optional elements you talked about. Then use the .Net
xsd.exe tool to convert the schema into a source file for a strongly
typed dataset. You can then use that file in you project to handle the
xml data. This greatly simplifies the data handling and also give you
some release immunity. For example a bunch of XML methods were
deprecated between .net 1.1 and 2.0 which gave off a lot of warnings. I
had no issues like this when using xsd.exe tool. Of course if this is an
academic problem for learning purposes you can just ignore this advice.

Hope this helps.
Leon Lambert

gigs wrote:
i need to make parser to go trough node tree

i have this structure
<root>
<bookstore pass="1">
<book>
<name id="1">programming xml</name>
</book>
<book>
<name id="2">programming C#</name>
</book>
</bookstore>
</root>
which is not same all the time. sometimes it have more element in book
and sometimes is deeper like this.
this is just example xml
<root>
<bookstore pass="1">
<book>
<katalog num="1">
<name id="1">programming xml</name>
</katalog>
</book>
<book>
<name id="2">programming C#</name>
</book>
</bookstore>
</root>

i have 3 xml files
first xml where i read data and if i have attribute in bookstore i go
and check for name attribute. i took it and than i go to bookstore tag
with same attribute in second xml. and i need to write all elements in
bookstore to third xml (same structure), but i need to change name tag
innertext with text from first xml.

i was thinking

public void ParseNodeTree(string id, string path, XPathNavigator nav,
XmlTextWriter writer)
{
XPathNodeIterator iter = nav.Select(path);

while (iter.MoveNext())
{
if (iter.Current.GetAttribute("id", "") == id)
{
//change this node text with text from first file
}

writer.WriteStartElement(iter.Current.Name);

if (iter.Current.HasChildren)
{
//dont know how to get children path to put it in recursion
ParseNodeTree(id, path, nav, writer);
}
else
{
writer.WriteFullEndElement();
}
}
}
can someone please help me. i lost 2 days for this
i cant do it anymore
thanks!
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rene | last post by:
Hi, i'm a newbie at java and have a question for beginners. So i'm try to create a type of parser for a input-string. i take a StringTokenizer to cut the Sting into token. But i want, that the...
4
by: tuxlover | last post by:
Hello everyone I have to write a verilog parser in python for a class project. I was wondering if all you folks could advise me on choosing the right python parser module. I am not comfortable...
5
by: qqcq6s59 | last post by:
Hi all I am a newbie and I just saw a ongoing thread on Fileprocessing which talks abt config parser. I have writen many pyhton program to parse many kind of text files by using string module and...
2
by: Jim Duffie | last post by:
Hi, can anyone give me a clue why I am getting the following error, Thanks, Jim Parser Error Message: Could not load type System.Web.UI.WebControls.Menu from assembly System.Web,...
6
by: jmike | last post by:
Hi everyone, I am a total newbie to XML parsing. I've written a couple of toy examples under the instruction of tutorials available on the web. The problem I want to solve is this. I have an...
4
by: seberino | last post by:
I'm a compiler newbie and was curious if Python's language/grammar can be handled by a recursive descent parser. Well? Chris
121
by: jacob navia | last post by:
Hi guys I have written this small parser to print out the functions defined in a C file. This is an example of parsing in C, that I want to add to my tutorial. Comments (and bug reports) are...
15
by: an0047 | last post by:
Hello Would like to develop a simple XML parser with own commands The aproach is first to develop a state machine to later implement it in C. I had a look to some posts relating lexical...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
1
by: GoodMan | last post by:
So I was looking for a way to be able to parse images (<img>) from a given url. It so happened that I stumbled upon a nice little piece of code called "PHP Simple HTML DOM Parser" found here :...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.