473,378 Members | 1,375 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,378 software developers and data experts.

problem when parsing the xml file

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
1 1359
eWish
971 Expert 512MB
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

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

Similar topics

0
by: burn_hall | last post by:
Hi, I have a problem and can't figure it out and need your help, please look at the following code and the output also a xml file snippet is down there too. Looking at the output I don't know why...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
7
by: Merlin | last post by:
Hi there, I have a problem with opening new windows with JS. If the webpage did not complete loading due to any reason a click on a link which is supposed to open a small new other window loads...
14
by: m | last post by:
all, i am trying to use the function round() which I found through google to be declared in math.h ( http://www.gnu.org/software/libc/manual/html_node/Rounding-Functions.html). this function does...
10
by: Bryce Calhoun | last post by:
Hello, First of all, this is a .NET 1.1 component I'm creating. SUMMARY ----------------------- This component that I'm creating is, for all intents and purposes, a document parser (I'm...
4
by: Richard | last post by:
Hi, I like a demo on layers posted at http://www.echoecho.com/csslayers.htm. It displays two text phrases in separate layers visually overlapped, first with one on top and the other beneath,...
1
by: Martin Pöpping | last post by:
Hello, I´ve a problem with parsing a double value from an xml file. My code looks like this: int concept_id; double rank; XmlElement root = documentXMLString.DocumentElement; XmlNodeList...
27
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
12
by: Julian | last post by:
Hi, I am having problems with a function that I have been using in my program to read sentences from a 'command file' and parse them into commands. the surprising thing is that the program works...
7
by: souravmallik | last post by:
Hello, I'm facing a big logical problem while writing a parser in VC++ using C. I have to parse a file in a chunk of bytes in a round robin fashion. Means, when I select a file, the parser...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...

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.