473,394 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 1788
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 :...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.