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

Trying to get the XML content without spaces with SAX2

4
Hi there,

I'm trying for a while to obtain the XML content of a file without spaces.

I have a XML file with the following content:

<script>
<command>
<createDocument>
<name>MyDocument</name>
<type>Text</type>
</createDocument>
</command>
<command>
<createFile>
<name>MyFile</name>
<type>File</type>
</createFile>
</command>
</script>

It happens that I need to obtain the name and type values from the XML but I'm getting the content with white spaces. Here is the code I'm trying to use:

Expand|Select|Wrap|Line Numbers
  1. void MySAX2Handler::startElement(const   XMLCh* const    uri,
  2.                             const   XMLCh* const    localname,
  3.                             const   XMLCh* const    qname,
  4.                             const   Attributes&     attrs)
  5. {
  6.     outStream.clear();
  7.     char* message = XMLString::transcode(localname);
  8.     std::cout << "I saw element: "<< message <<  std::endl;
  9.     XMLString::release(&message);
  10.  
  11. }
  12.  
  13. void MySAX2Handler::fatalError(const SAXParseException& exception)
  14. {
  15.     char* message = XMLString::transcode(exception.getMessage());
  16.     std::cout << "Fatal Error: " << message
  17.          << " at line: " << exception.getLineNumber()
  18.          << std::endl;
  19. }
  20.  
  21. void MySAX2Handler::characters(const XMLCh* const chars, const unsigned int length)
  22. {
  23.     char* message = XMLString::transcode(chars);
  24.     std::cout << "Characters: " << message << " Lenght: " << length << std::endl;
  25.     if (XMLString::compareIString(chars,XMLUni::fgZeroLenString) != 0) {
  26.       outStream << message;
  27.     }
  28.     XMLString::release(&message);
  29. }
  30.  
  31. void MySAX2Handler::endElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname)
  32. {
  33.       std::cout << "EndElement - OutStream: " << outStream.str() << std::endl;
  34. }
But I'm still getting the outStream value from the endElement with white spaces.
I would really appreciate if you can help me to solve this problem.

Thank you.

Nei
Feb 8 '08 #1
1 1377
jkmyoung
2,057 Expert 2GB
Not really familiar with SAX2.

You should see the whitespace when you output the message here correct?

Expand|Select|Wrap|Line Numbers
  1. std::cout << "Characters: " << message << " Lenght: " << length << std::endl;
  2.  
You should be able to replace the whitespace like so:
Expand|Select|Wrap|Line Numbers
  1. message = message.replaceAll("\s+", " ");
  2.  
Note, this does not get rid of all whitespace, but adds in a single space where there used to be multiple spaces, newlines, tabs, etc..
Feb 8 '08 #2

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

Similar topics

1
by: Christian Roth | last post by:
Hi, as far as I can see there is no callback method defined for comments in SAX2's ContentHandler interface. 1) Why is this omission? 2) How can I get also the contents of XML comments from...
13
by: Jesse Thompson | last post by:
Greetings fell XML folk. I've just gotten started making SAX filters in Perl. I was hoping to build an XML templating engine this way, but the performance of XML::SAX::Expat and XML::SAX::Writer...
16
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will...
2
by: Mike P | last post by:
Does anyone know where i can download acopy of the SAX2 module? Cheers Mike
1
by: aphillips | last post by:
I have an application that uses the Xerces SAX2 parser. My problem is that if I encounter a particular error in one of my callback methods I would like to terminate the parsing. Of course, the...
2
by: muralibala68 | last post by:
Hi, How do I parse an xml message that is in std::string using sax2 parse method that does not seem to take a string and expects a filename? Thanks.
1
by: gvk123 | last post by:
Hi All, I am new to C++ Programming. Our Requirement is to parse the XML Responses generated from the Java Web Services. We are using the Xerces-Sax2 parser. Please any one suggest me the best...
3
by: Jonathan Wood | last post by:
I noticed that some sites will display content and have a Print link that will display the same content but without all the unneeded graphics and colors. Anyone here ever implemented this? I...
1
by: Michael Nemtsev [MVP] | last post by:
Hello gerry, Everything ok with your code - it works fine and I see controls in designer - i just rebuld project and everything okey-dokey As I understand u are using VS 2008. Try to install the...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.