Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: parsing xml

  1. #1
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    parsing xml

    I want to create xml definitions that I can use to define 2D graphics with my iterators for recursive data structures.

    e.g. attached is a picture of a recursive triangle, but instead of writing code to create the structure I want to read it from an XML file and have some fun with "fractal" images.

    I know XML can handle hierarchical data no problem but how would I handle the cyclic (recursive) links placing the images themselves within themselves?

    I was hoping someone has experience with XML who knows how to do that perhaps ?
    Attached Images Attached Images

  2. #2
    Join Date
    Jul 2008
    Beans
    1,491

    Re: parsing xml

    You would create provisions for elements to link to themselves. It's as simple or as difficult as you want it to be because you create the XML format.

    A simple enough way would be to define an ID type attribute/element and define a continue/recursion REFID type attribute/element.

    So a fractal could be defined to look like this:
    Code:
    <fractal limit="300"> <!-- limit to recursion depth of 300 -->
    <!-- create a triangle with equal sides -->
    <triangle id="base">
      <param name="x"/> <!-- x coord of top of triangle -->
      <param name="y"/> <!-- y coord of top of triangle -->
      <param name="u"/> <!-- vector from top to base of triangle (x) -->
      <param name="v"/> <!-- vector from top to base of triangle (y) -->
      <!-- call base again with new x, y values -->
      <apply refid="base">
        <with-param name="x" value="..."/>
        <with-param name="y" value="..."/>
        <with-param name="u" value="..."/>
        <with-param name="v" value="..."/>
      </apply>
    </triangle>
    </fractal>

  3. #3
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: parsing xml

    Quote Originally Posted by Reiger View Post
    You would create provisions for elements to link to themselves. It's as simple or as difficult as you want it to be because you create the XML format.

    A simple enough way would be to define an ID type attribute/element and define a continue/recursion REFID type attribute/element...
    Thanks for that idea Reiger
    I see XML really is very flexible and it's hard to decide when to make something an attribute of a tag, or to make it into a property tag within the body of something. Having an "id" attribute is a good way to make references and I see they do that in XHTML.

    I think XHTML is probably quite a well thought out pattern to emulate so I'll even borrow the idea of having a "class" attribute to determine the appearance of things (I was going to call it layers, but aiming for consistency with existing structures can't be a bad thing. OTOH I don't know why they use css files that are not themselves an XML syntax

    Right from the start I could define coordinates (points) in many different ways:
    Code:
    <coord id="top1"><x>10</x><y>20</y></coord>
    <!-- or -->
    <coord id="top2" x="10" y="20">but then what to put in here?</coord>
    <!-- and transformed coordinates -->
    <coord><rotate angle="30">
          <value name="top1" />
    </rotate></coord>
    There is a big danger that it becomes hideously verbose and an inefficient way to store and process even trivial diagrams. So now I suppose I need to find some guidelines on systematic and consistent way to convert a data structure into XML

  4. #4
    Join Date
    May 2006
    Beans
    1,787

    Re: parsing xml

    Quote Originally Posted by worksofcraft View Post
    Thanks for that idea Reiger
    I see XML really is very flexible and it's hard to decide when to make something an attribute of a tag, or to make it into a property tag within the body of something. Having an "id" attribute is a good way to make references and I see they do that in XHTML.

    I think XHTML is probably quite a well thought out pattern to emulate so I'll even borrow the idea of having a "class" attribute to determine the appearance of things (I was going to call it layers, but aiming for consistency with existing structures can't be a bad thing. OTOH I don't know why they use css files that are not themselves an XML syntax

    Right from the start I could define coordinates (points) in many different ways:
    Code:
    <coord id="top1"><x>10</x><y>20</y></coord>
    <!-- or -->
    <coord id="top2" x="10" y="20">but then what to put in here?</coord>
    <!-- and transformed coordinates -->
    <coord><rotate angle="30">
          <value name="top1" />
    </rotate></coord>
    There is a big danger that it becomes hideously verbose and an inefficient way to store and process even trivial diagrams. So now I suppose I need to find some guidelines on systematic and consistent way to convert a data structure into XML
    For representing any particular structure, XML doesn't give you anything that you couldn't have got with a much simpler format, so think about why you need XML. There may be many good reasons, or none.

    Inefficient it may be, but I wouldn't worry about the efficiency for trivial diagrams.

  5. #5
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: parsing xml

    Quote Originally Posted by Arndt View Post
    For representing any particular structure, XML doesn't give you anything that you couldn't have got with a much simpler format, so think about why you need XML. There may be many good reasons, or none.

    Inefficient it may be, but I wouldn't worry about the efficiency for trivial diagrams.
    Indeed... I did ask myself that question. I mean I could just dump the data out in binary records, but then I would get problems with byte ordering and how to represent pointers and machines with different word sizes etcetera

    Thus my first reason for choosing XML is because it is a portable text based format and if need be can be edited with a text editor.

    My second reason is that I don't want to reinvent things that have already been thought out, tried tested and refined based on real experience unless I have a good reason to avoid it. My biggest concern with XML is exactly how verbose it is.

    My third reason is that XML has become industry standard and so there software libraries and tools available for processing XML. e.g. you can open one with Firefox and even if it doesn't know what application it is for it displays a nicely colored nested tree structure with expand/contract tabs.

    I suppose I won't really get a feel for it until I start using it so I better go find a tutorial on libexpat or whatever that XML parser library is called.

  6. #6
    Join Date
    Apr 2008
    Beans
    507

    Re: parsing xml

    A bit left-field but have you considered SVG? Perhaps coupled with Raphael? This way you could easily use the more lightweight JSON data format. SVG provides for all the rotation etc and drawing triangles and the like is a cakewalk with Raphael.

    Anyhoo, just a thought if you don't mind javascript.

    http://raphaeljs.com/
    Go you good thing!

  7. #7
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: parsing xml

    Quote Originally Posted by myrtle1908 View Post
    A bit left-field but have you considered SVG? Perhaps coupled with Raphael? This way you could easily use the more lightweight JSON data format. SVG provides for all the rotation etc and drawing triangles and the like is a cakewalk with Raphael.

    Anyhoo, just a thought if you don't mind javascript.

    http://raphaeljs.com/
    Yes, awesome! SVG is exactly the kind of standard I wanted to start with

    I do have some interesting ideas for this that are probably not in the standard... yet
    oh and I've already spotted some interesting ideas in the standard that I haven't quite fathomed why do it that way... like here is one - why use a 3D vector for 2D graphics
    Code:
    current transformation matrix (CTM)
        Transformation matrices define the mathematical mapping from one coordinate
        system into another using a 3x3 matrix using the equation:
        [x' y' 1] = [x y 1] * matrix.
        The current transformation matrix (CTM) defines the mapping from the user coordinate
        system into the viewport coordinate system...
    Admittedly I'll not be using javascript because one of my objectives is to explore developing proper GUI apps in C++ and also find out a bit more about the lower levels of graphics rendering on X windows based systems and I don't want to tie into any specific SVG processing libraries because I want to be able to explore possibilities of extending and adapting SVG capabilities

    p.s. my immediate concern is atm to use Gtk builder and Glade to manage the user interface and make double buffering work with Cairo graphics in the application window. Proof of concept code is to get all those recursive triangles rotating smoothly, and then get on to SVG based storage
    Last edited by worksofcraft; November 6th, 2010 at 12:52 AM.

  8. #8
    Join Date
    Apr 2008
    Beans
    507

    Re: parsing xml

    Might be something useful here http://antigrain.com/about/index.html
    Go you good thing!

  9. #9
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: parsing xml

    Quote Originally Posted by myrtle1908 View Post
    Might be something useful here http://antigrain.com/about/index.html
    Yes, yet another awesome discovery
    I'm not sure yet how much it overlaps with Cairo graphics functionality but it says it works with X11. Gdk does give me direct access to them.

  10. #10
    Join Date
    Jul 2008
    Beans
    1,491

    Re: parsing xml

    Quote Originally Posted by worksofcraft View Post
    why use a 3D vector for 2D graphics
    Not that I'm particularly knowledge-able about these kinds of things, but 3x3 matrices are used in affine transforms. Then it makes sense to use a 3d vector because relations (concatenated transforms) can be expressed in terms of matrix multiplication.

    The Java SE API features a convenience class for implementing affine transforms which has some hints on how it's done: http://download.oracle.com/javase/1....Transform.html

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •