xml parsing

L

lnatz

I need to parse a configuration file using xml. How do I retrieve the
data between the elements? For example, if my configuration file was
organized in the following manner:

<configFile>
<server_name> NY_ONLINE </server_name>
<database> Schedulers </database>
<table> Sched_Info </table>
<user_column> userID </user_column>
<delete_column> Deleted </delete_column>
<delete_value> 2 </delete_value>
</configFile>

And would the textContent() function be needed? I considered doing the
following:

$server = "server_name";
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($configFile);
my $docRoot = $doc->getDocumentElement();
my @configs = $docRoot->getChildrenByTagName($server);
$dbserver= @configs[0] ->textContent();

I don't think it is properly written though and I don't think it would
work, but would that be something close to the proper way of parsing
the configuration file?

Thanks in advance,
Natalie
 
J

Joseph Kesselman

lnatz said:
I need to parse a configuration file using xml. How do I retrieve the
data between the elements?

If you're parsing using SAX, that's returned as characters() events
(possibly multiple events per contiguous chunk of text; forgetting to
allow for that is the single most common SAX error).

If you're parsing into a DOM, that's returned as Text node children of
the Element node that encloses them.
And would the textContent() function be needed?

That funtion isn't a part of either of those two standard APIs, so I
can't advise.
 
?

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

lnatz said:
I need to parse a configuration file using xml. How do I retrieve the
data between the elements? For example, if my configuration file was
organized in the following manner:

<configFile>
<server_name> NY_ONLINE </server_name>
<database> Schedulers </database>
<table> Sched_Info </table>
<user_column> userID </user_column>
<delete_column> Deleted </delete_column>
<delete_value> 2 </delete_value>
</configFile>

Why dont you put the data into attributes ?
Something like

<configFile servername="NY ONLINE" database="Schedulers">
...
</configFile>
 
L

lnatz

I did not want to put the data into attribute because I read that an
attribute is meant to describe the data in the elements. It is not
proper coding to make the whole configuration file an attribute like :
<configFile servername="NY ONLINE" database="Schedulers">
...
</configFile>

But to respond to Joseph Kesselman..I'm using SAX but I just don't
understand which functions to use to extract the data. For example:
<server_name> NY_ONLINE </server_name>
How can I get the "NY_ONLINE" ? Can I use character(), is that what you
mean? And what do you mean by :
 
L

lnatz

I did not want to put the data into attribute because I read that an
attribute is meant to describe the data in the elements. It is not
proper coding to make the whole configuration file an attribute like :
<configFile servername="NY ONLINE" database="Schedulers">
...
</configFile>

But to respond to Joseph Kesselman..I'm using SAX but I just don't
understand which functions to use to extract the data. For example:
<server_name> NY_ONLINE </server_name>
How can I get the "NY_ONLINE" ? Can I use character(), is that what you
mean? And what do you mean by :
 
?

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

lnatz said:
I did not want to put the data into attribute because I read that an
attribute is meant to describe the data in the elements. It is not
proper coding to make the whole configuration file an attribute like :

Who said that ? I have seen _many_ XML files
where all information is in the attributes.
But to respond to Joseph Kesselman..I'm using SAX but I just don't
understand which functions to use to extract the data. For example:
<server_name> NY_ONLINE </server_name>

Maybe you give an example telling us what you
want to do with the data. Dont get stuck when
you run into details that arent essential.
 
W

William Park

lnatz said:
I need to parse a configuration file using xml. How do I retrieve the
data between the elements? For example, if my configuration file was
organized in the following manner:

<configFile>
<server_name> NY_ONLINE </server_name>
<database> Schedulers </database>
<table> Sched_Info </table>
<user_column> userID </user_column>
<delete_column> Deleted </delete_column>
<delete_value> 2 </delete_value>
</configFile>

And would the textContent() function be needed? I considered doing the
following:

$server = "server_name";
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($configFile);
my $docRoot = $doc->getDocumentElement();
my @configs = $docRoot->getChildrenByTagName($server);
$dbserver= @configs[0] ->textContent();

I don't think it is properly written though and I don't think it would
work, but would that be something close to the proper way of parsing
the configuration file?

Not sure what you want to do after extracting the "data", but simple
printing would go something like

func () # Usage: func text
{
case ${XML_TAG_STACK[1]} in
configFile) echo "${XML_TAG_STACK[0]}={$1}" ;;
esac
}

expat -d func < file.xml

which produces

server_name={ NY_ONLINE }
database={ Schedulers }
table={ Sched_Info }
user_column={ userID }
delete_column={ Deleted }
delete_value={ 2 }

--
William Park <[email protected]>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top