We work hard to add more examples which illustrate various aspects of PlainXML library usage, however most of them are not ready yet. Please visit this page later (we plan that they will be available soon) or please feel free to contact us if you have some specific questions.

Also, please refer to FAQ section where typical questions are collected.

Example illustrates reading XML tree and search for particular XML element

This example illustrates how to read XML from file to tree of XML POJO (XMLElements) and how to locate particular element.

    // first, we create instance of XMLData object
    // which is used to wrap functionality related for
    // reading/writing XMLElements

    XMLData xmlData = new XMLData();

    if (! xmlData.read(aSrcFile))
      System.out.println("Can't read xml data from "
                            + aSrcFile);
      // the root of XML tree we read before is
      // retrieved from appropriate property of XML
      // data

      XMLElement root = xmlData.getRoot();

      // Now we try to find last element <binary ..>
      // under the root

      int index = root.findLast("binary");
      if (index == -1)
        System.out.println("element not found");
        // and here we can obtain XML element
        // for index found

        XMLElement binary = root.Items().get(index);

        // Here we could show found element.  
        // Note that for convenience, the XMLElement
        // class provides toText() method which allows
        // to view short data stored in element in
        // short form


Example of storing XML tree to output stream

This examples illustrates how to save the tree of XMLElement's to output stream (as plain text XML) with pretty print formatting.

      // first we need to create writer which will
      // be used for storing XML tree to xml
      OutputStreamWriter osw =
         new OutputStreamWriter(System.out);
      Writer wout = new BufferedWriter(osw);

      // And here we create instance of XMLWriter
      // class (this is alternative approach to
      // using XMLData)
      XMLWriter xml = new XMLWriter();

      // Here we specify that XML should be
      // written with formatting

      // Simply write passed root element (aElm)
      // into stream
      xml.write(wout, aElm);

      // closing all IO related stuff
    catch (IOException e)
      // process exception somehow 

UML for most important classes in PlainXML

Please click on the image to enlarge.

UML for PlainXML

  SourceForge.net Logo   Support This Project