Problemet XML. XML Exempel. Principer. Gammalt problem: arkivera/byta data. Binärt Text Struktur - PDF

Description
Problemet Gammalt problem: arkivera/byta data XML Introduktion Binärt Text Struktur Principer XML Exempel En strukturerad textfil Använder taggar för att märka ut informationen Informationen kan lätt flyttas

Please download to get full document.

View again

of 6
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information
Category:

Science & Technology

Publish on:

Views: 14 | Pages: 6

Extension: PDF | Download: 0

Share
Transcript
Problemet Gammalt problem: arkivera/byta data XML Introduktion Binärt Text Struktur Principer XML Exempel En strukturerad textfil Använder taggar för att märka ut informationen Informationen kan lätt flyttas mellan olka system Informationen kan lätt läsas i olika programspråk ?xml version= 1.0 encoding= utf-8 ? annikastuff values l1 d0= 1 d1= 2 d2= 3 / l2 d0= 4 d1= 5 / l3 d0= 6 d1= 7 d2= 8 / px d0= 11 d1= 12 / py d0= 13 d1= 14 / /values students student id= 1 name= kalle Anka l1= 0 l2= 0 l3= 0 px= 0 py= 0 / student id= 2 name= kajsa Anka l1= 0 l2= 0 l3= 0 px= 0 py= 0 / student id= 3 name= knatte Anka l1= 0 l2= 0 l3= 0 px= 0 py= 0 / student id= 4 name= fnatte Anka l1= 0 l2= 0 l3= 0 px= 0 py= 0 / student id= 5 name= tjatte Anka l1= 0 l2= 0 l3= 0 px= 0 py= 0 / /students /annikastuff ?xml version= 1.0 encoding= utf-8 ? annikastuff values variable name= l1 value value= 1 / value value= 2 / value value= 3 / variable name= l2 value value= 4 / value value= 5 / variable name= l3 value value= 6 / value value= 7 / value value= 8 / variable name= px value value= 11 / value value= 12 / variable name= py value value= 13 / value value= 14 / /values students student id= 1 l1= 0 l2= 0 l3= 0 name= kalle Anka px= 0 py= 0 / student id= 2 l1= 0 l2= 0 l3= 0 name= kajsa Anka px= 0 py= 0 / student id= 3 l1= 0 l2= 0 l3= 0 name= knatte Anka px= 0 py= 0 / student id= 4 l1= 0 l2= 0 l3= 0 name= fnatte Anka px= 0 py= 0 / student id= 5 l1= 0 l2= 0 l3= 0 name= tjatte Anka px= 0 py= 0 / /students /annikastuff XML och Java Java har naturligtvis stöd SAX och DOM XSLT import java.io.fileoutputstream; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.domsource; import javax.xml.transform.stream.streamresult; import org.w3c.dom.*; public class GenerateData... public static void main( String[] argv ) throws Exception DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newdocumentbuilder(); DOMImplementation domimpl = builder.getdomimplementation(); Document doc = domimpl.createdocument( null, annikastuff , null ); Element root = doc.getdocumentelement(); root.appendchild( buildxmlvalues( doc ) ); root.appendchild( buildxmlstudents( doc ) ); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newtransformer(); Source src = new DOMSource( doc ); FileOutputStream fos = new FileOutputStream( argv[0] ); Result output = new StreamResult( fos ); trans.transform( src, output ); private static Element buildvalues( Document doc, String n, Integer... vals) Element val = doc.createelement( variable ); Element tmp; private static Element buildstudent( Document doc, String s, Integer id, Integer l1, Integer l2, Integer l3, Integer px, Integer py ) Element val = doc.createelement( student ); val.setattribute( name , n ); for( int counter = 0; counter vals.length; counter++ ) tmp = doc.createelement( value ); tmp.setattribute( value , vals[counter].tostring() ); val.appendchild( tmp ); val.setattribute( name , s ); val.setattribute( id , id.tostring() ); val.setattribute( l1 , l1.tostring() ); val.setattribute( l2 , l2.tostring() ); val.setattribute( l3 , l3.tostring() ); val.setattribute( px , px.tostring() ); val.setattribute( py , py.tostring() ); private static Element buildxmlvalues( Document doc ) Element val = doc.createelement( values ); private static Element buildxmlstudents( Document doc ) Element val = doc.createelement( students ); val.appendchild( buildvalues( doc, l1 , 1, 2, 3 ) ); val.appendchild( buildvalues( doc, l2 , 4, 5 ) ); val.appendchild( buildvalues( doc, l3 , 6, 7, 8 ) ); val.appendchild( buildvalues( doc, px , 11, 12 ) ); val.appendchild( buildvalues( doc, py , 13, 14 ) ); val.appendchild( buildstudent( doc, Kalle Anka , 1, 0, 0, 0, 0, 0 ) ); val.appendchild( buildstudent( doc, Kajsa Anka , 2, 0, 0, 0, 0, 0 ) ); val.appendchild( buildstudent( doc, Knatte Anka , 3, 0, 0, 0, 0, 0 ) ); val.appendchild( buildstudent( doc, Fnatte Anka , 4, 0, 0, 0, 0, 0 ) ); val.appendchild( buildstudent( doc, Tjatte Anka , 5, 0, 0, 0, 0, 0 ) ); ?xml version= 1.0 encoding= utf-8 ? annikastuff values variable name= l1 value value= 1 / value value= 2 / value value= 3 / variable name= l2 value value= 4 / value value= 5 / variable name= l3 value value= 6 / value value= 7 / value value= 8 / variable name= px value value= 11 / value value= 12 / variable name= py value value= 13 / value value= 14 / /values students student id= 1 l1= 0 l2= 0 l3= 0 name= kalle Anka px= 0 py= 0 / student id= 2 l1= 0 l2= 0 l3= 0 name= kajsa Anka px= 0 py= 0 / student id= 3 l1= 0 l2= 0 l3= 0 name= knatte Anka px= 0 py= 0 / student id= 4 l1= 0 l2= 0 l3= 0 name= fnatte Anka px= 0 py= 0 / student id= 5 l1= 0 l2= 0 l3= 0 name= tjatte Anka px= 0 py= 0 / /students /annikastuff import javax.xml.parsers.*; import org.xml.sax.inputsource; import org.w3c.dom.*; public class ConsumeData... public static void main( String[] args ) throws Exception DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = factory.newdocumentbuilder(); Document document = parser.parse( new InputSource( args[0] ) ); Element data = document.getdocumentelement(); // Det ska bara vara en (struntar i felkoll) NodeList el = data.getelementsbytagname( values ); displayvariables( (Element) el.item(0) ); // Det ska bara vara en (struntar i felkoll) el = data.getelementsbytagname( students ); displaystudents( (Element) el.item(0) ); private static void displayonevariable( Element v ) int nrofvalues; System.out.print( v.getattribute( name ) + = [ ); NodeList vals = v.getelementsbytagname( value ); nrofvalues = vals.getlength(); for( int counter = 0; counter nrofvalues; counter++ ) if( counter nrofvalues-1 ) System.out.print( ( (Element) vals.item(counter) ).getattribute( value ) + , ); else System.out.print( ( (Element) vals.item(counter) ).getattribute( value ) ); System.out.println( ] ); private static void displayvariables( Element v ) NodeList vars = v.getelementsbytagname( variable ); for( int counter = 0; counter vars.getlength(); counter++ ) displayonevariable( (Element) vars.item( counter ) ); private static void displayonestudent( Element s ) System.out.println( Namn ; + s.getattribute( name ) ); System.out.println( ID ; + s.getattribute( id ) ); System.out.println( l1 ; + s.getattribute( l1 ) ); System.out.println( l2 ; + s.getattribute( l2 ) ); System.out.println( l3 ; + s.getattribute( l3 ) ); System.out.println( px ; + s.getattribute( px ) ); System.out.println( py ; + s.getattribute( py ) ); private static void displaystudents( Element v ) NodeList students = v.getelementsbytagname( student ); for( int counter = 0; counter students.getlength(); counter++ ) displayonestudent( (Element) students.item( counter ) ); java ConsumeData hejsan l1 = [1, 2, 3] l2 = [4, 5] l3 = [6, 7, 8] px = [11, 12] py = [13, 14] Namn ; Kalle Anka ID ; 1 Namn ; Kajsa Anka ID ; 2 Namn ; Knatte Anka ID ; 3 Namn ; Fnatte Anka ID ; 4 Namn ; Tjatte Anka ID ; 5 Rickards exempel ?xml version= 1.0 encoding= iso ? ?xml-stylesheet type= text/xsl href= drycker.xsl ? dryckesback dryck namn= kocacola kolsyra= ja sockergrad= 9 alkoholprocent= 0 Kola dryck namn= loka kolsyra= ja sockergrad= 1 alkoholprocent= 0 Loka dryck namn= guinness kolsyra= ja sockergrad= 2 alkoholprocent= 4,5 Guinness dryck namn= single malt Laphroig 10 yrs kolsyra= nej sockergrad= 2 alkoholprocent= 40 Laphroig dryck namn= oj kolsyra= nej sockergrad= 8 alkoholprocent= 1 jos /dryckesback Rickards exempel Rickards exempel ?xml version= 1.0 encoding= iso ? xsl:stylesheet version= 1.0 xmlns:xsl= http://www.w3.org/1999/xsl/transform xsl:template match= / html table style= font-family:tahoma; tr td width= 200 style= font-weight:bold;background-color:blue;color:white colspan= 3 Drycker /td tr style= font-weight:bold;background-color:#333333;color:white td namn /td td sockerhalt /td td alkoholpossent /td xsl:apply-templates select= dryckesback/dryck xsl:sort order= descending / /xsl:apply-templates /table /html xsl:template match= dryckesback/dryck tr style= background-color:#999999;color:#000000 td xsl:value-of select= . / /td td xsl:value-of td xsl:value-of /xsl:stylesheet import java.io.fileoutputstream; import javax.xml.transform.*; import javax.xml.transform.stream.*; public class FormatStudentData public static void main( String[] args ) throws Exception TransformerFactory factory = TransformerFactory.newInstance(); Transformer xlst = factory.newtransformer( new StreamSource( args[0] ) ); StreamSource xmlsrc = new StreamSource( args[1] ); StreamResult output = new StreamResult( new FileOutputStream( args[2] ) ); xlst.transform( xmlsrc, output ); java FormatStudentData studenttext.xlst demo.xml out.txt java FormatStudentData studenthtml.xlst demo.xml out.html ?xml version= 1.0 encoding= utf-8 ? xsl:stylesheet version= 1.0 xmlns:xsl= http://www.w3.org/1999/xsl/transform xsl:output method= html / xsl:template match= / html head title student lista /title /head body h1 studenter /h1 xsl:apply-templates select= annikastuff/students / /body /html xsl:template match= students table align= center border= 1 tr th namn /th th id /th th l1 /th th l2 /th th l3 /th th px /th th py /th xsl:apply-templates select= student / /table ?xml version= 1.0 encoding= utf-8 ? xsl:stylesheet version= 1.0 xmlns:xsl= http://www.w3.org/1999/xsl/transform xsl:output method= text / xsl:template match= / Student lista xsl:apply-templates select= annikastuff/students / xsl:template match= students xsl:preserve-space elements= student / xsl:for-each select= student xsl:value-of / , xsl:value-of / , xsl:value-of / , xsl:value-of / , xsl:value-of / , xsl:value-of / , xsl:value-of / xsl:template match= student tr th xsl:value-of / /th td xsl:value-of / /td td xsl:value-of / /td td xsl:value-of / /td td xsl:value-of / /td td xsl:value-of / /td td xsl:value-of / /td /xsl:stylesheet /xsl:for-each /xsl:stylesheet
Related Search
Similar documents
View more...
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks