Top > OOoBasic > Generic > sax

sax Edit

Parser Edit

com.sun.star.xml.sax.Parser サービスは xml ファイルをパースします。BookmarksMenu などで使用しています。

点線のスタイルを保存している styles_ja.sod ファイルを読み込んでみます。

  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 Type aDashStroke
   LineName As String
   LineStyle As String
   LineDots1 As Integer
   LineDots2 As Integer
   LineDots1Length As String
   LineDots2Length As String
   LineDistance As String
 End Type
 
 Dim aDashStrokes() As aDashStroke
 
 
 Sub Main
   oPathSubst = CreateUnoService("com.sun.star.util.PathSubstitution")
   sxmlURL = oPathSubst.substituteVariables( _
     "$(user)/config/styles_ja.sod",True)
   
   oSFA = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
   If NOT oSFA.exists(sxmlURL) Then Exit Sub
   oInput = oSFA.openFileRead(sxmlURL)
   
   oParser = CreateUnoService("com.sun.star.xml.sax.Parser")
   oDocHandler = CreateUnoListener("xmlDocHandler_",_
     "com.sun.star.xml.sax.XDocumentHandler")
   oParser.setDocumentHandler(oDocHandler)
   aLocale = CreateUnoStruct("com.sun.star.lang.Locale")
   With aLocale
     .Language = "ja"
     .Country = "JP"
   End With
   oParser.setLocale(aLocale)
   
   oInputSource = CreateUnoStruct("com.sun.star.xml.sax.InputSource")
   oInputSource.aInputStream = oInput
   oParser.parseStream(oInputSource)
   oInput.closeInput()
   
   sTxt = ""
   For i = 0 To UBound(aDashStrokes)
     If aDashStrokes(i).LineName <> "" Then 
       sTxt = sTxt & aDashStrokes(i).LineName & ": " & _
         aDashStrokes(i).LineStyle & ", "'& chr(10)
     End If
   Next i
   msgbox sTxt
 End Sub
 
 
 Sub xmlDocHandler_startDocument()
 End Sub
 Sub xmlDocHandler_endDocument()
 End Sub
 Sub xmlDocHandler_startElement( aName As String, xAttribs As Object )
   If NOT sName = "draw:stroke-dash" Then Exit Sub
   nCount = UBound(aDashStrokes) +1
   ReDim Preserve aDashStrokes(nCount)
   With aDashStrokes(nCount)
     .LineName = xAttribs.getValueByName("draw:name")
     .LineStyle = xAttribs.getValueByName ("draw:style")
     .LineDots1 = xAttribs.getValueByName ("draw:dots1")
     .LineDots2 = xAttribs.getValueByName ("draw:dots2")
     .LineDots1Length = xAttribs.getValueByName ("draw:dots1-length")
     .LineDots1Length = xAttribs.getValueByName ("draw:dots1-length")
     .LineDistance = xAttribs.getValueByName ("draw:distance")
   End With
 End Sub
 Sub xmlDocHandler_endElement( aName As String )
 End Sub
 Sub xmlDocHandler_characters( aChars As String )
 End Sub
 Sub xmlDocHandler_ignorableWhitespace( aWhitespaces As String )
 End Sub
 Sub xmlDocHandler_processingInstruction( aTarget As String, aData As String )
 End Sub
 Sub xmlDocHandler_setDocumentLocator( xLocator As Object )
 End Sub

Writer Edit

css.sax.Writer では xml ファイルとして書き込みます。

  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
Type ElementEntry
  sName As String
  sValue As String
End Type
 
Dim oCurrent() As Object
 
 
Sub sax_Writer_1()
  sfa = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
  oOut = sfa.openFileWrite("file:///home/asuka/Desktop/manifest.xml")
  
  oWriter = CreateUnoService("com.sun.star.xml.sax.Writer")
  oWriter.setOutputStream(oOut)
  
  oAttrList = CreateUnoListener("XAttrList_", "com.sun.star.xml.sax.XAttributeList")
  
  With oWriter
    .startDocument()
    .startElement("manifest:manifest", oAttrList)
    .characters(chr(10))
    ReDim oCurrent(1)
    oCurrent(0) = CreateObject("ElementEntry")
    oCurrent(1) = CreateObject("ElementEntry")
    oCurrent(0).sName = "manifest:full-path"
    oCurrent(0).sValue = "config.xcs"
    oCurrent(1).sName = "manifest:media-type"
    oCurrent(1).sValue = "application/vnd.sun.star.configuration-schema"
    
    .startElement("manifest:file-entry", oAttrList)
    .endElement("manifest:file-entry")
    
    .characters(chr(10))
    
    ReDim oCurrent(1)
    oCurrent(0) = CreateObject("ElementEntry")
    oCurrent(1) = CreateObject("ElementEntry")
    oCurrent(0).sName = "manifest:full-path"
    oCurrent(0).sValue = "config.xcu"
    oCurrent(1).sName = "manifest:media-type"
    oCurrent(1).sValue = "application/vnd.sun.star.configuration-data"
    
    .startElement("manifest:file-entry", oAttrList)
    .endElement("manifest:file-entry")
    
    .characters(chr(10))
    .endElement("manifest:manifest")
    .endDocument()
  End With
  'mri oWriter
  
End Sub
 
 
Function XAttrList_getLength() As Integer
  XAttrList_getLength = UBound(oCurrent) + 1
End Function
Function XAttrList_getNameByIndex(index As Integer) As String
  XAttrList_getNameByIndex = oCurrent(index).sName
End Function
Function XAttrList_getTypeByIndex(index As Integer) As String
  XAttrList_getTypeByIndex = ""
End Function
Function XAttrList_getTypeByName(sName As String) As String
  XAttrList_getTypeByName = ""
End Function
Function XAttrList_getValueByIndex(index As Integer) As String
  XAttrList_getValueByIndex = oCurrent(index).sValue
End Function
Function XAttrList_getValueByName(sName As String) As String
  XAttrList_getValueByName = ""
End Function

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup   Help   RSS of recent changes