Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1
    Member
    Join Date
    Apr 2010
    Location
    Everett
    Posts
    6

    Post DataAdapter with XML

    I am trying to use the HelloWorld example and use an external XML file to be read. I'm not sure what to put in public void run and what I would put in the getData part either. I looked at the other thread talking about DataAdapter using XML but when it wouldn't compile. So I just want to see if I'm on the right direction. Any help would be very much appreciated.
    class GreetingsThread extends Thread {

    private final Object itemHandle;
    public volatile boolean go = true;

    public GreetingsThread(Object itemHandle) {
    this.itemHandle = itemHandle;
    }

    public void main(String argv[]) {

    try {
    File fXmlFile = new File("scoreboard.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();

    System.out.println("Sport : " + doc.getDocumentElement().getNodeName());
    NodeList nList = doc.getElementsByTagName("matchup");
    System.out.println("-----------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {
    Node nNode = nList.item(temp);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
    Element eElement = (Element) nNode;
    System.out.println(getTagValue("away",eElement));
    System.out.println(getTagValue("home",eElement));
    System.out.println(getTagValue("score",eElement));
    System.out.println(getTagValue("inning",eElement)) ;
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    private String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChi ldNodes();
    Node nValue = (Node) nlList.item(0);

    return nValue.getNodeValue();
    }



    public void run() {
    int c = 0;
    Random rand = new Random();


    while(go) {
    Map<String, String> data = new HashMap<String, String>();
    data.put("xmldata", "");
    data.put("timestamp", new Date().toString());

    listener.smartUpdate(itemHandle, data, false);
    c++;
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    }
    }
    }
    }

  2. #2
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    973
    You should be managing data that change with time.
    What does your scoreboard.xml file contain?
    A list of current match results for different matches (hence some external process may be updating the file)?
    Or a list of updates for one or multiple matches that happened at different times (and that you put in a single document only for simulation purpose)?

    As a general rule about managing XML files with LS, note that Lightstreamer is best suited for transmitting tabular data.
    So, you could take a fixed set of fields from an XML document and put them into your Map as different key-value pairs; the names used as the keys have to be specified also as field names in the client page.
    If your XML document is very structured and you cannot have it fit into a set of field-value pairs, then you have to send the document as a whole in a single field and read it by custom code on the client side; this won't take advantage of some of the optimization features of LS.

  3. #3
    Member
    Join Date
    Apr 2010
    Location
    Everett
    Posts
    6
    Sorry for the long delay in my response. My scoreboard.xml file contains four fields. They are home, away, inning, and score. I just want to create a simple scoreboard simulation. I looked at the example from amarin about his using a data adapter with xml. I just am not quite sure on how to code the data adapter to read data from my xml file.

  4. #4
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    973
    As your XML file has a simple tabular structure, you can easily fit your data into the items/fields concepts used in Lightstreamer.
    What you can associate to the concept of "item" depends on what your front-end page is aimed at representing.
    Does your page explicitly mention each single match, so that you only need to decorate the match data with the current result?
    Or does the page contain a table which just lists the current matches, so that new matches could even enter the table dynamically as they start?
    The behavior of the page and the identification of what is a data "item" is a precondition for designing the Data Adapter.

  5. #5
    Member
    Join Date
    Apr 2010
    Location
    Everett
    Posts
    6
    Here is what I have for my front-end.

    <html>
    <head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <script language="JavaScript" src="LS/lscommons.js"></script>
    <script language="JavaScript" src="LS/lspushpage.js"></script>

    </head>

    <body>
    <div source="lightstreamer" table="hellotable" item="greetings" field="away">loading...</div>
    <div source="lightstreamer" table="hellotable" item="greetings" field="home">loading...</div>
    <div source="lightstreamer" table="hellotable" item="greetings" field="score">loading...</div>
    <div source="lightstreamer" table="hellotable" item="greetings" field="inning">loading...</div>
    <div source="lightstreamer" table="hellotable" item="greetings" field="timestamp">loading...</div>

    <script type="text/javascript">
    var page = new PushPage();
    page.onEngineCreation = function(engine) {
    engine.connection.setAdapterName("HELLOWORLD");
    engine.changeStatus("STREAMING");
    }
    page.bind();
    page.createEngine("HelloWorldApp", "LS", "SHARE_SESSION");

    var pushtable = new OverwriteTable(null, null, "MERGE");
    page.addTable(pushtable, "hellotable");
    </script>
    </body>
    </html>
    I'm just learning Java and Lightstreamer and that is why I'm having this basic problem.

  6. #6
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    973
    You have a page that only has the place for displaying one match result.
    However, your source XML file is supposed to contain a list of match results.

    What, in your intentions, should the page show?
    One of the results included in the XML file?
    All the results included in the XML file in round, so that each result is displayed for a few seconds?

    Or is your real intention that of showing a table which reports all the results (so that you would need to extend your page)?

  7. #7
    Member
    Join Date
    Apr 2010
    Location
    Everett
    Posts
    6
    I want my page to show all of my matches. I was looking at another example and they used an array. I figure that is what I should use. I'm just not sure on how to code it?

  8. #8
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    973
    You have usually two options for displaying a set of matches
    and again it all depends on your data.
    1) you are managing a fixed set of matches, say 10 matches, and your page already includes static contents regarding those 10 matches.
    2) you want to list whatever matches are currently available and your page, in its static part, has no knowledge of the available matches.

  9. #9
    Member
    Join Date
    Apr 2010
    Location
    Everett
    Posts
    6
    Yes I want to list whatever matches are currently available, in its static part, has no knowledge of the available matches.

  10. #10
    Power Member
    Join Date
    Jul 2006
    Location
    Cesano Maderno, Italy
    Posts
    784
    hi,

    so what you need is to implement something similar to our PortfolioDemo.

    Such demo is based on the MultiMetapush logic. There is a main item that in our case gives a list of stocks but in yours will give a list of matches and then each match will be related to its own item that supplies the updates related to such match.

    We used two different data adapters to differentiate the kind of data sent. The main item is supplied by the PortfolioAdapter while the sub-items are supplied by the StockListDemo adapter.
    Note that our main item is governed by client requests while yours will read its data from your XML. To receive orders from clients we needed to implement a custom metadata adapter, you'll probably can use the same metadata adapter of the hello world demo (LiteralBasedProvider)

    You can find the sources and explanation of both data adapters in our distribution under DOCS-SDKs/sdk_adapter_java/examples/Portfolio_Adapters and DOCS-SDKs/sdk_adapter_java/examples/StockListDemo_DataAdapter

    HTH

 

 

Similar Threads

  1. Replies: 4
    Last Post: February 3rd, 2011, 10:33 AM
  2. How to debug .NET DataAdapter
    By msgiribabu in forum Adapter APIs
    Replies: 1
    Last Post: January 21st, 2011, 01:38 PM
  3. DataAdapter Service
    By brucehoch in forum Adapter APIs
    Replies: 3
    Last Post: October 14th, 2010, 11:22 PM
  4. DataAdapter using XML
    By amarin in forum Adapter APIs
    Replies: 4
    Last Post: August 21st, 2008, 09:23 AM
  5. Pre-Processor Snapshot vs DataAdapter Snapshot
    By sukhdev in forum Adapter APIs
    Replies: 3
    Last Post: August 1st, 2007, 08:44 AM

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
  •  
All times are GMT +1. The time now is 05:14 AM.