

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
1 message in org.openoffice.fr.prog[prog] Lire un xml externe| From | Sent On | Attachments |
|---|---|---|
| vinc...@regis-dgac.net | May 26, 2004 1:18 am |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | [prog] Lire un xml externe | Actions... |
|---|---|---|
| From: | vinc...@regis-dgac.net (vinc...@regis-dgac.net) | |
| Date: | May 26, 2004 1:18:25 am | |
| List: | org.openoffice.fr.prog | |
Bonjour,
J'ai trouvé cela sur OOoForum. Ca peut servir à d'autre. C'est un sujet de DannyB, http://www.oooforum.org/forum/viewtopic.php?t=4907, le voici:
Sub Main cXmlFile = "C:\TestData.xml"
cXmlUrl = ConvertToURL( cXmlFile )
ReadXmlFromUrl( cXmlUrl ) End Sub
' This routine demonstrates how to use the Universal Content Broker's ' SimpleFileAccess to read from a local file. Sub ReadXmlFromUrl( cUrl ) ' The SimpleFileAccess service provides mechanisms to open, read, write files, ' as well as scan the directories of folders to see what they contain.
' The advantage of this over Basic's ugly file manipulation is that this ' technique works the same way in any programming language. ' Furthermore, the program could be running on one machine, while the SimpleFileAccess ' accesses files from the point of view of the machine running OOo, not the machine ' where, say a remote Java or Python program is running. oSimpleFileAccess = createUnoService( "com.sun.star.ucb.SimpleFileAccess" )
' Open input file. oInputStream = oSimpleFileAccess.openFileRead( cUrl )
ReadXmlFromInputStream( oInputStream )
oInputStream.closeInput() End Sub
Sub ReadXmlFromInputStream( oInputStream ) ' Create a Sax Xml parser. oSaxParser = createUnoService( "com.sun.star.xml.sax.Parser" )
' Create a document event handler object. ' As methods of this object are called, Basic arranges ' for global routines (see below) to be called. oDocEventsHandler = CreateDocumentHandler()
' Plug our event handler into the parser. ' As the parser reads an Xml document, it calls methods ' of the object, and hence global subroutines below ' to notify them of what it is seeing within the Xml document. oSaxParser.setDocumentHandler( oDocEventsHandler )
' Create an InputSource structure. oInputSource = createUnoStruct( "com.sun.star.xml.sax.InputSource" ) With oInputSource .aInputStream = oInputStream ' plug in the input stream End With
' Now parse the document. ' This reads in the entire document. ' Methods of the oDocEventsHandler object are called as ' the document is scanned. oSaxParser.parseStream( oInputSource ) End Sub
'================================================== ' Xml Sax document handler. '==================================================
' Global variables used by our document handler. ' ' Once the Sax parser has given us a document locator, ' the glLocatorSet variable is set to True, ' and the goLocator contains the locator object. ' ' The methods of the locator object has cool methods ' which can tell you where within the current Xml document ' being parsed that the current Sax event occured. ' The locator object implements com.sun.star.xml.sax.XLocator. ' Private goLocator As Object Private glLocatorSet As Boolean
' This creates an object which implements the interface ' com.sun.star.xml.sax.XDocumentHandler. ' The doucment handler is returned as the function result. Function CreateDocumentHandler() ' Use the CreateUnoListener function of Basic. ' Basic creates and returns an object that implements a particular interface. ' When methods of that object are called, ' Basic will call global Basic functions whose names are the same ' as the methods, but prefixed with a certian prefix. oDocHandler = CreateUnoListener( "DocHandler_", "com.sun.star.xml.sax.XDocumentHandler" )
glLocatorSet = False
CreateDocumentHandler() = oDocHandler End Function
'================================================== ' Methods of our document handler call these ' global functions. ' These methods look strangely similar to ' a SAX event handler. ;-) ' These global routines are called by the Sax parser ' as it reads in an XML document. ' These subroutines must be named with a prefix that is ' followed by the event name of the com.sun.star.xml.sax.XDocumentHandler interface. '==================================================
Sub DocHandler_startDocument() Print "Start document" End Sub
Sub DocHandler_endDocument() ' Print "End document" End Sub
Sub DocHandler_startElement( cName As String, oAttributes As com.sun.star.xml.sax.XAttributeList ) Print "Start element", cName End Sub
Sub DocHandler_endElement( cName As String ) ' Print "End element", cName End Sub
Sub DocHandler_characters( cChars As String ) End Sub
Sub DocHandler_ignorableWhitespace( cWhitespace As String ) End Sub
Sub DocHandler_processingInstruction( cTarget As String, cData As String )
End Sub
Sub DocHandler_setDocumentLocator( oLocator As com.sun.star.xml.sax.XLocator ) ' Save the locator object in a global variable. ' The locator object has valuable methods that we can ' call to determine goLocator = oLocator
glLocatorSet = True End Sub
You need some XML to parse, so create a document named C:\TestData.xml, and put the following text into it.
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>
a+ Vincent







