List Info

Thread: Understanding SAX to parse XML and avoiding empty tags




Understanding SAX to parse XML and avoiding empty tags
user name
2007-04-05 15:22:26
So I got a snippet of code to parse an XML file using SAX.
This script  
pops a windows witht he legend "Some character"
and the value of the  
key tags in the XML.

However the script print all the tags content which having
no value  
just give out the blank space.

The script is the following: Code:
Sub Main
    cXmlFile = "/home/jza/tmp/test.xml"

    cXmlUrl = ConvertToURL( cXmlFile )

    ReadXmlFromUrl( cXmlUrl )
End Sub

Sub ReadXmlFromUrl( cUrl )
    oSFA = createUnoService(
"com.sun.star.ucb.SimpleFileAccess" )

    oInputStream = oSFA.openFileRead( cUrl )

    ReadXmlFromInputStream( oInputStream )

    oInputStream.closeInput()
End Sub

Sub ReadXmlFromInputStream( oInputStream )
    oSaxParser = createUnoService(
"com.sun.star.xml.sax.Parser" )
    oDocEventsHandler = CreateDocumentHandler()
    oSaxParser.setDocumentHandler( oDocEventsHandler )
    oInputSource = createUnoStruct(
"com.sun.star.xml.sax.InputSource" )
    With oInputSource
       .aInputStream = oInputStream   ' plug in the input
stream
    End With

    oSaxParser.parseStream( oInputSource )
End Sub

Private goLocator As Object
Private glLocatorSet As Boolean

Function CreateDocumentHandler()
    oDocHandler = CreateUnoListener(
"DocHandler_",  
"com.sun.star.xml.sax.XDocumentHandler" )

    glLocatorSet = False

    CreateDocumentHandler() = oDocHandler
End Function

Sub DocHandler_startDocument()
End Sub

Sub DocHandler_endDocument()
End Sub

Sub DocHandler_startElement( cName As String, oAttributes As
 
com.sun.star.xml.sax.XAttributeList )
End Sub

Sub DocHandler_endElement( cName As String )
End Sub

Sub DocHandler_characters( cChars As String )
    Print "Some character", cChars
End Sub

Sub DocHandler_setDocumentLocator( oLocator As
com.sun.star.xml.sax.XLocator )
    goLocator = oLocator

    glLocatorSet = True
End Sub



The issue is basically this part where the Print functions
just print  
all the characters which becomes every single cell of the
XML: Code:
Sub DocHandler_characters( cChars As String )
    Print "Some character", cChars
End Sub



The XML is simple: Code:
<Employees>
    <Employee id="101">
       <Name>
          <First>John</First>
          <Last>Smith</Last>
       </Name>
       <Address>
          <Street>123 Main</Street>
          <City>Lawrence</City>
          <State>KS</State>
          <Zip>66049</Zip>
       </Address>
       <Phone
type="Home">785-555-1234</Phone>
    </Employee>
    <Employee id="102">
       <Name>
          <First>Bob</First>
          <Last>Jones</Last>
       </Name>
       <Address>
          <Street>456 Puke Drive</Street>
          <City>Lawrence</City>
          <State>KS</State>
          <Zip>66049</Zip>
       </Address>
       <Phone
type="Home">785-555-1235</Phone>
    </Employee>
</Employees>

------------------------------------------------------------
---------
To unsubscribe, e-mail: dev-unsubscribexml.openoffice.org
For additional commands, e-mail: dev-helpxml.openoffice.org


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )