Re: Ausgewählte Felder kopieren und in xml File abspeichern
Verfasst: Mi, 05.08.2009 15:47
Hmm hab mir mal angeschaut wie man Java zusammen mit OpenOffice verwenden kann und das kommt mir schon sehr mühsam und kompliziert vor 

deutsches Forum rund um Apache OpenOffice und LibreOffice
http://www.oooforum.de/
jup, hoffentlich antwortet dort einer!eBayer hat geschrieben:Hallo, ich habe zwar keine Ahnung von XML..... aber schau doch mal hier herein:
http://www.oooforum.org/forum/viewtopic ... create+xml
Gruß eBayer
Code: Alles auswählen
oDomBuilder = _
createUnoService("com.sun.star.xml.dom.DocumentBuilder")
oDom = oDocBuilder.newDocument()
Code: Alles auswählen
oN3 = oDom.createElement("Anschrift")
Code: Alles auswählen
oDom.createTextNode(sCellContent)
Code: Alles auswählen
oN3.appendChild(oN4)
Code: Alles auswählen
oN3.setAttribute(Name_des_Attributs, Wert_des_Attributs)
Code: Alles auswählen
Sub WriteDomToFile(oTree as object, sFilePath as string)
REM Writes the structure of an actual DOM (oTree) line after line
REM into a simple text file.
REM Mark the line feed (LF). It has to be set explicitly.
REM Linux: chr(10), Windows: chr(13) + chr(10)
dim oSimpleFileAccess as object
dim oOutputStream as object
dim oTextOutput as object
dim oTreeNodes as object
dim i as integer
on error goto Catch
LF = chr(10)
REM First set the output stream
sFilePath = converttourl(sFilePath)
oSimpleFileAccess = createUnoService("com.sun.star.ucb.SimpleFileAccess")
with oSimpleFileAccess
if .exists(sFilePath) then .kill(sFilePath)
oOutputStream = .openFileWrite(sFilePath)
end with
oTextOutput = createUnoService("com.sun.star.io.TextOutputStream")
with oTextOutput
.OutputStream = oOutputStream
.setEncoding("UTF-8")
REM The first line is a processing instruction. It usually isn't part of
REM the DOM tree. So we write it separately.
.WriteString("<?xml version=" + chr(34) + "1.0" + chr(34) + "encoding=" _
+ chr(34) + "UTF-8" + chr(34) + "?>" + LF)
REM A DOM tree can consist of zero, one or more child nodes on the
REM root level. They are treated separately.
if oTree.hasChildNodes() then
oTreeNodes = oTree.getChildNodes()
for i = 0 to oTreeNodes.getLength - 1
PrintDom(oTreeNodes.item(i), oTextOutput, 0, 2)
next
end if
.closeOutput()
end with
oOutputStream.closeOutput()
exit sub
Catch:
print "Fehler " + err + " (" + error(err) + ")"
End Sub
Code: Alles auswählen
Sub PrintDom(oElement as object, oStream as object, _
iLevel as integer, iIndent as integer)
REM Writes the elements of a DOM tree recursively line after line into a
REM text file. Mark to start with iLevel 0.
REM Indents lower levels by iIndent characters, except for text nodes: they
REM are written directly after the element start tag followed by the
REM element end tag.
REM It is assumed that there are either one text node or one or more other
REM child nodes to an element, if any.
REM
REM This is a 0.1 version in Starbasic without any validating.
REM Missing: prevent to write parent element tag in case there are child nodes
REM without any content.
REM Please tell me if there already exists some other solution.
REM Please feel free to inform me about errors, improvements and amendments.
REM Author: Volker Lenhardt
REM Email: volker.lenhardt@uni.due.de
dim oElementChildren as object
dim oChild as object
dim sLine as string
dim i as integer
dim iLen as integer
dim sNodeName as string
sNodeName = oElement.getNodeName()
if oElement.getNodeType() = _
com.sun.star.xml.dom.NodeType.COMMENT_NODE then
sLine = string(iLevel * iIndent, " ") + "<!-- " _
+ oElement.getNodeValue() + " -->"
oStream.WriteString(sLine + LF)
elseif oElement.getNodeType() = _
com.sun.star.xml.dom.NodeType.PROCESSING_INSTRUCTION_NODE then
REM Is there a bug? If I create a processing instruction with both
REM the target "xml" and data "version..." arguments, the getTarget
REM yields "xml", but the getData yields "". The only way is to give
REM the whole string as target and "" as data, and to retrieve it as
REM nodename. Can someone reproduce this behaviour?
sLine = string(iLevel * iIndent, " ") + "<?" + sNodeName + "?>"
oStream.WriteString(sLine + LF)
else
sLine = string(iLevel * iIndent, " ") + "<" + sNodeName _
+ AttString(oElement.getAttributes()) + ">"
if oElement.hasChildNodes() then
oElementChildren = oElement.getChildNodes()
iLen = oElementChildren.getLength()
if iLen = 1 then 'Test for text node, assuming that there are no other
' sibling nodes besides.
oChild = oElementChildren.item(0)
if oChild.getNodeType() = com.sun.star.xml.dom.NodeType.TEXT_NODE then
sLine = sLine + oChild.getNodeValue() + _
"</" + sNodeName + ">"
oStream.WriteString(sLine + LF)
exit sub
end if
end if
oStream.WriteString(sLine + LF)
for i = 0 to iLen - 1
PrintDom(oElementChildren.item(i), oStream, iLevel + 1, iIndent)
next
sLine = string(iLevel * iIndent, " ") + "</" + sNodeName + ">"
oStream.WriteString(sLine + LF)
else
REM If there is no child node, but attributes, the short form is used.
REM If there are not even attributes, no element tag is written.
if oElement.hasAttributes() then
sLine = left(sLine, len(sLine) - 1) + " />"
oStream.WriteString(sLine + LF)
end if
end if
end if
End Sub
Aber hallo, Volker.preklov hat geschrieben:bin zwar beileibe kein Profi
Code: Alles auswählen
Function HasContent(oElementList as object) as boolean
REM Tests recursively, if there is some child node with content, be it text
REM or attribute value other than "".
REM It runs in PrintDom before the actual element start tag is written,
REM so PrintDom can abstain from printing an empty element.
dim oChildren as object
dim oChild as object
dim oAttributes as object
dim b as boolean
dim i as integer
dim j as integer
dim l as integer
l = oElementList.getLength()
for i = 0 to l - 1
oChild = oElementList.item(i)
if oChild.hasAttributes() then
oAttributes = oChild.getAttributes()
for j = 0 to oAttributes.getLength() - 1
if oAttributes.item(j).getNodeValue() <> "" then
b = true
exit for
end if
next
if b then exit for
end if
if oChild.getNodeType() = com.sun.star.xml.dom.NodeType.TEXT_NODE then
if oChild.getNodeValue() <> "" then
b = true
exit for
end if
end if
oChildren = oChild.getChildNodes()
if oChildren.getLength() = 0 then
exit for
else
b = HasContent(oChildren)
if b then exit for
end if
next
HasContent = b
End Function
Code: Alles auswählen
if oChildren.getLength() = 0 then
exit for
else
b = HasContent(oChildren)
if b then exit for
end if
Code: Alles auswählen
if oChildren.getLength() <> 0 then
b = HasContent(oChildren)
if b then exit for
end if
Code: Alles auswählen
iCount = Freefile
oDoc = ThisComponent
oSels = oDoc.getCurrentSelection() 'Aktuelle Selektion
oData() = oSels.getDataArray()
sR=1
sC=1
for i = lbound(oData) to ubound(oData) 'auslesen der selektierten Zeilen
oRow() = oData(i)
for j = lbound(oRow) to ubound(oRow)
oArray(sR,sC) = oRow(j) 'schreiben des Arrays
sC= sC + 1
next j
sR = sR + 1
sC = 1
next i
'Überprüfungen zur Selektion
if oArray(1,1) = Empty then
msgbox "Bitte keine leeren Zeilen selektieren!"
stop
elseif j > 13 then
msgbox "Bitte nur bis zur Spalte M selektieren!"
stop
else
outputR = 0
outputC = 0
'Schleife zum durchlaufen der Zeilen
while outputR < i + 1
if oArray(outputR,1) = "Label" then
msgbox "Bitte nicht die Überschriftszeile(n) selektieren!"
stop
end if
if oArray(outputR,4) = Empty then 'Überprüfung ob es ein Titel ist
sSQL = chr(13)+" <!--<field name="+chr(34)+oArray(outputR,1)+chr(34)+"/>-->"
goto mark1
end if
sSQL = chr(13)+" <field name="+chr(34)+oArray(outputR,1)+chr(34)
'Überprüfungen zum Inhalt der Spalten --> hinzufügen von Informationen
if oArray(outputR,2) <> Empty then
sSQL = sSQL+" modul="+chr(34)+oArray(outputR,2)+chr(34)
end if
if oArray(outputR,3) <> Empty then
sSQL = sSQL+" name="+chr(34)+oArray(outputR,3)+chr(34)
end if
'... Weitere Überprüfungen
sSQL = sSQL+"/>"
mark1:
sSQLComplett = sSQLComplett + sSQL 'Zusammenführen der jeweiligen Zeileninhalte
outputR = outputR + 1
wend
dim args1(0) as new com.sun.star.beans.PropertyValue
oDoc.storeasurl(path & filename ,args1()) 'erstellen der Datei
open path & filename for Output as iCount 'öffnen der Datei
'schreiben des Dateiinhaltes
print #iCount, sSQLComplett
close #iCount