By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,851 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,851 IT Pros & Developers. It's quick & easy.

problem when parsing the xml file

P: 1
Dear All,

I wanted to parse an XML file and print the element's content.
I have the following code for the same. I have printed the ourput too.
The problem is it is printing unwanted spaces and new lines in the output.
Help me avoid this problem.
Expand|Select|Wrap|Line Numbers
  1. use XML::Parser;
  2. my $parser = XML::Parser->new( Handlers => {
  3. Init => \&handle_doc_start,
  4. Final => \&handle_doc_end,
  5. Start => \&handle_elem_start,
  6. End => \&handle_elem_end,
  7. Char => \&handle_char_data,
  8. });
  9. #
  10. # globals
  11. #
  12. my $record; # points to a hash of element contents
  13. my $context; # name of current element
  14. my %records; # set of address entries
  15. #
  16. # read in the data and run the parser on it
  17. #
  18. my $file = "/home/reddyth/HPPGlobalAuditTool/client/config/test.xml";
  19.  
  20. if( $file ) {
  21.     #print "going to parse the file";
  22. $parser->parsefile( $file );
  23. ###
  24. ### Handlers
  25. ###
  26.  
  27. sub handle_doc_start {}
  28. #
  29. # save element name and attributes
  30. #
  31. sub handle_elem_start {
  32. my( $expat, $name, %atts ) = @_;
  33. $context = $name;
  34. $record = {} if( $name eq 'jdbc-data-source' );
  35. }
  36. # collect character data into the recent element's buffer
  37. #
  38. sub handle_char_data {
  39. my( $expat, $text ) = @_;
  40. # Perform some minimal entitizing of naughty characters
  41. $text =~ s/&/&/g;
  42. $text =~ s/</&lt;/g;
  43. $record->{ $context } .= $text;
  44. }
  45. #
  46. # if this is an <entry>, collect all the data into a record
  47. #
  48. sub handle_elem_end {
  49. my( $expat, $name ) = @_;
  50. return unless( $name eq 'jdbc-data-source' );
  51. my $fullname = $record->{'name'} . $record->{'value'};
  52. print $fullname;
  53. $records{ $fullname } = $record;
  54. }
  55. #
  56. # Output the close of the file at the end of processing.
  57. #
  58. sub handle_doc_end {
  59.  
  60. foreach my $key ( sort( keys( %records ))) {
  61.     %chardata = "";
  62. print $records{$key}->{'name'} ;
  63. print $records{$key}->{'algorithm-type'} ;
  64. %chardata = ($records{$key}=>'url');
  65. print $chardata{'url'};
  66. #print $records{$key}->{'url'};
  67. print $records{ $key }->{'initial-capacity'};
  68. }
  69.  
  70. }
  71.  
Output:

JDBC Data Source-0
user
hpp_wls9


JDBC Data Source-0
user
30

XML File:
Expand|Select|Wrap|Line Numbers
  1. <jdbc-data-source xmlns="http://www.bea.com/ns/weblogic/90" xmlns:sec="http://www.bea.com/ns/weblogic/90/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wls="http://www.bea.com/ns/weblogic/90/security/wls" xsi:schemaLocation="http://www.bea.com/ns/weblogic/910/domain.xsd">
  2.   <name>JDBC Data Source-0</name>
  3.   <jdbc-driver-params>
  4.     <url>jdbc:oracle:thin:@gvu0183.houston.hp.com:1525:hppsys2</url>
  5.     <driver-name>oracle.jdbc.xa.client.OracleXADataSource</driver-name>
  6.     <properties>
  7.       <property>
  8.         <name>user</name>
  9.         <value>hpp_wls9</value>
  10.       </property>
  11.     </properties>
  12. </jdbc-data-source>
  13.  
Feb 5 '08 #1
Share this Question
Share on Google+
1 Reply


eWish
Expert 100+
P: 971
I don't see anything in your code to cause any line breaks or extra spaces. Are you unhappy that the following data is not on one line?

Expand|Select|Wrap|Line Numbers
  1. JDBC Data Source-0
  2. user
  3. 30
--Kevin
Feb 5 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.