Results 1 to 3 of 3
  1. #1
    tankhardrive is offline Member
    Join Date
    Oct 2008
    Posts
    10
    Rep Power
    0

    Exclamation [SOLVED]xml parsing help

    My problem is not reading in from the xml file as i can do that its i only want to read to a certain spot, then another reader will read from there. to help make this more clear i will post a sample of the xml im reading in.
    Java Code:
    <!--
     Automatically generated data from EVE-Central.com 
    -->
    −
    <evec_api version="2.0" method="quicklook">
    −
    <quicklook>
    <item>34</item>
    <itemname>Tritanium</itemname>
    <regions>
    
    
    </regions>
    <hours>360</hours>
    <minqty>0</minqty>
    −
    <sell_orders>
    −
     <order id="967264889">
      <region>10000022</region>
      <station>60014182</station>
    −
      <station_name>
      FV-SE8 VIII - Moon 1 - True Creations Logistic Support
      </station_name>
      <security>0.0</security>
      <range>-2</range>
      <price>1.8</price>
      <vol_remain>8403609</vol_remain>
      <min_volume>1</min_volume>
      <expires>2009-04-08</expires>
      <reported_time>01-10 19:31:18</reported_time>
     </order>
    </sell_orders>
    −
    <buy_orders>
    −
     <order id="898177625">
      <region>10000014</region>
      <station>61000007</station>
      <station_name>KDF-GY - ISS Marginis</station_name>
      <security>0.0</security>
      <range>-1</range>
      <price>100.0</price>
      <vol_remain>1</vol_remain>
      <min_volume>1</min_volume>
      <expires>2009-01-24</expires>
      <reported_time>01-07 22:01:27</reported_time>
     </order>
    </buy_orders>
    </quicklook>
    </evec_api>
    now this is just 2 small parts from it, but the whole xml is under <quicklook> and i want to read in <sell_orders> and <buy_orders> separately. currently i can read in all the info in the file but both of my arrays get the same data, the whole xml.

    below is my code for reading in the xml, the top 3 work fine and dont need to be touched the bottom 2 loops are where im having trouble. now i dont have any idea how many <orders> will be in either side so i need to be able to read from <sell_orders> to </sell_orders> and pull all of the information in between. same goes for <buy_orders>.

    Java Code:
    try {
    
                marketStat = new String[3][5];
                quickLookS = new String[5000][8];
                quickLookB = new String[5000][8];
                qlSSize = 0;
                qlBSize = 0;
    
                File fileMs = new File(itemName + "ms.xml");
                File fileQl = new File(itemName + "ql.xml");
    
                DocumentBuilderFactory dbfMs = DocumentBuilderFactory.newInstance();
                DocumentBuilderFactory dbfQl = DocumentBuilderFactory.newInstance();
                DocumentBuilder dbMs = dbfMs.newDocumentBuilder();
                DocumentBuilder dbQl = dbfQl.newDocumentBuilder();
    
                Document docMs = dbMs.parse(fileMs);
                Document docQl = dbQl.parse(fileQl);
    
                docMs.getDocumentElement().normalize();
                docQl.getDocumentElement().normalize();
                
                NodeList nodeListMsAll = docMs.getElementsByTagName("all");
                NodeList nodeListMsBuy = docMs.getElementsByTagName("buy");
                NodeList nodeListMsSell = docMs.getElementsByTagName("sell");
    
                NodeList nodeListQlSell = docQl.getElementsByTagName("sell_orders");
                NodeList nodeListQlBuy = docQl.getElementsByTagName("buy_orders");
    
                for(int i = 0; i < nodeListMsAll.getLength(); i++){
                    Node fstNode = nodeListMsAll.item(i);
    
                    if(fstNode.getNodeType() == Node.ELEMENT_NODE){                    
                        Element fstElmnt = (Element) fstNode;
                        
                        NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("volume");
                        Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
                        NodeList fstNm = fstNmElmnt.getChildNodes();
                        marketStat[0][0] = ((String) fstNm.item(0).getNodeValue());
                        
                        NodeList scdNmElmntLst = fstElmnt.getElementsByTagName("avg");
                        Element scdNmElmnt = (Element) scdNmElmntLst.item(0);
                        NodeList scdNm = scdNmElmnt.getChildNodes();
                        marketStat[0][1] = ((String) scdNm.item(0).getNodeValue());
    
                        NodeList n1 = fstElmnt.getElementsByTagName("max");
                        Element e1 = (Element) n1.item(0);
                        NodeList nl1 = e1.getChildNodes();
                        marketStat[0][2] = ((String) nl1.item(0).getNodeValue());
    
                        NodeList n2 = fstElmnt.getElementsByTagName("min");
                        Element e2 = (Element) n2.item(0);
                        NodeList nl2 = e2.getChildNodes();
                        marketStat[0][3] = ((String) nl2.item(0).getNodeValue());
    
                        NodeList n4 = fstElmnt.getElementsByTagName("median");
                        Element e4 = (Element) n4.item(0);
                        NodeList nl4 = e4.getChildNodes();
                        marketStat[0][4] = ((String) nl4.item(0).getNodeValue());
                    }
                    
                }
                for(int i = 0; i < nodeListMsBuy.getLength(); i++){
                    Node fstNode = nodeListMsBuy.item(i);
    
                    if(fstNode.getNodeType() == Node.ELEMENT_NODE){
                        Element fstElmnt = (Element) fstNode;
    
                        NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("volume");
                        Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
                        NodeList fstNm = fstNmElmnt.getChildNodes();
                        marketStat[1][0] = ((String) fstNm.item(0).getNodeValue());
    
                        NodeList scdNmElmntLst = fstElmnt.getElementsByTagName("avg");
                        Element scdNmElmnt = (Element) scdNmElmntLst.item(0);
                        NodeList scdNm = scdNmElmnt.getChildNodes();
                        marketStat[1][1] = ((String) scdNm.item(0).getNodeValue());
    
                        NodeList n1 = fstElmnt.getElementsByTagName("max");
                        Element e1 = (Element) n1.item(0);
                        NodeList nl1 = e1.getChildNodes();
                        marketStat[1][2] = ((String) nl1.item(0).getNodeValue());
    
                        NodeList n2 = fstElmnt.getElementsByTagName("min");
                        Element e2 = (Element) n2.item(0);
                        NodeList nl2 = e2.getChildNodes();
                        marketStat[1][3] = ((String) nl2.item(0).getNodeValue());
    
                        NodeList n4 = fstElmnt.getElementsByTagName("median");
                        Element e4 = (Element) n4.item(0);
                        NodeList nl4 = e4.getChildNodes();
                        marketStat[1][4] = ((String) nl4.item(0).getNodeValue());
                    }
    
                }
                for(int i = 0; i < nodeListMsSell.getLength(); i++){
                    Node fstNode = nodeListMsSell.item(i);
    
                    if(fstNode.getNodeType() == Node.ELEMENT_NODE){
                        Element fstElmnt = (Element) fstNode;
    
                        NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("volume");
                        Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
                        NodeList fstNm = fstNmElmnt.getChildNodes();
                        marketStat[2][0] = ((String) fstNm.item(0).getNodeValue());
    
                        NodeList scdNmElmntLst = fstElmnt.getElementsByTagName("avg");
                        Element scdNmElmnt = (Element) scdNmElmntLst.item(0);
                        NodeList scdNm = scdNmElmnt.getChildNodes();
                        marketStat[2][1] = ((String) scdNm.item(0).getNodeValue());
    
                        NodeList n1 = fstElmnt.getElementsByTagName("max");
                        Element e1 = (Element) n1.item(0);
                        NodeList nl1 = e1.getChildNodes();
                        marketStat[2][2] = ((String) nl1.item(0).getNodeValue());
    
                        NodeList n2 = fstElmnt.getElementsByTagName("min");
                        Element e2 = (Element) n2.item(0);
                        NodeList nl2 = e2.getChildNodes();
                        marketStat[2][3] = ((String) nl2.item(0).getNodeValue());
    
                        NodeList n4 = fstElmnt.getElementsByTagName("median");
                        Element e4 = (Element) n4.item(0);
                        NodeList nl4 = e4.getChildNodes();
                        marketStat[2][4] = ((String) nl4.item(0).getNodeValue());
                    }
    
                }
    
                JOptionPane.showMessageDialog(window,"market Stat read from xml!");
    
                for(int i = 0; i < nodeListQlSell.getLength(); i++){//starts reading in the sell orders
                    Node fstNode = nodeListQlSell.item(i);
                    
                    NodeList nlQlSell = docQl.getElementsByTagName("order");
    
                    for(int j = 0; j < nlQlSell.getLength(); j++){
                        Node node1 = nlQlSell.item(j);
    
                        if(node1.getNodeType() == Node.ELEMENT_NODE){
                          
                            Element el1 = (Element) node1;
    
                            NodeList n1 = el1.getElementsByTagName("region");
                            Element e1 = (Element) n1.item(0);
                            NodeList nl1 = e1.getChildNodes();
                            quickLookS[j][0] = ((String) nl1.item(0).getNodeValue());
    
                            /*NodeList n2 = el1.getElementsByTagName("station");
                            Element e2 = (Element) n2.item(0);
                            NodeList nl2 = e2.getChildNodes();
                            quickLookS[j][1] = ((String) nl2.item(0).getNodeValue());*/
    
                            NodeList n3 = el1.getElementsByTagName("station_name");
                            Element e3 = (Element) n3.item(0);
                            NodeList nl3 = e3.getChildNodes();
                            quickLookS[j][1] = ((String) nl3.item(0).getNodeValue());
    
                            NodeList n4 = el1.getElementsByTagName("security");
                            Element e4 = (Element) n4.item(0);
                            NodeList nl4 = e4.getChildNodes();
                            quickLookS[j][2] = ((String) nl4.item(0).getNodeValue());
    
                            NodeList n5 = el1.getElementsByTagName("range");
                            Element e5 = (Element) n5.item(0);
                            NodeList nl5 = e5.getChildNodes();
                            quickLookS[j][3] = ((String) nl5.item(0).getNodeValue());
    
                            NodeList n6 = el1.getElementsByTagName("price");
                            Element e6 = (Element) n6.item(0);
                            NodeList nl6 = e6.getChildNodes();
                            quickLookS[j][4] = ((String) nl6.item(0).getNodeValue());
    
                            NodeList n7 = el1.getElementsByTagName("vol_remain");
                            Element e7 = (Element) n7.item(0);
                            NodeList nl7 = e7.getChildNodes();
                            quickLookS[j][5] = ((String) nl7.item(0).getNodeValue());
    
                            NodeList n8 = el1.getElementsByTagName("min_volume");
                            Element e8 = (Element) n8.item(0);
                            NodeList nl8 = e8.getChildNodes();
                            quickLookS[j][6] = ((String) nl8.item(0).getNodeValue());
                            
                            NodeList n9 = el1.getElementsByTagName("expires");
                            Element e9 = (Element) n9.item(0);
                            NodeList nl9 = e9.getChildNodes();
                            quickLookS[j][7] = ((String) nl9.item(0).getNodeValue());
    
                            qlSSize++;
                        }
                    }
                    
                }
    
                for(int i = 0; i < nodeListQlBuy.getLength(); i++){//starts reading in the buy orders
                    Node fstNode = nodeListQlBuy.item(i);
    
                    NodeList nlQlBuy = docQl.getElementsByTagName("order");
    
                    
    
                    for(int j = 0; j < nlQlBuy.getLength(); j++){
                        Node node1 = nlQlBuy.item(j);
    
                        if(node1.getNodeType() == Node.ELEMENT_NODE){
                            Element el1 = (Element) node1;
    
                            NodeList n1 = el1.getElementsByTagName("region");
                            Element e1 = (Element) n1.item(0);
                            NodeList nl1 = e1.getChildNodes();
                            quickLookB[j][0] = ((String) nl1.item(0).getNodeValue());
    
                            /*NodeList n2 = el1.getElementsByTagName("station");
                            Element e2 = (Element) n2.item(0);
                            NodeList nl2 = e2.getChildNodes();
                            quickLookB[j][1] = ((String) nl2.item(0).getNodeValue());*/
    
                            NodeList n3 = el1.getElementsByTagName("station_name");
                            Element e3 = (Element) n3.item(0);
                            NodeList nl3 = e3.getChildNodes();
                            quickLookB[j][1] = ((String) nl3.item(0).getNodeValue());
    
                            NodeList n4 = el1.getElementsByTagName("security");
                            Element e4 = (Element) n4.item(0);
                            NodeList nl4 = e4.getChildNodes();
                            quickLookB[j][2] = ((String) nl4.item(0).getNodeValue());
    
                            NodeList n5 = el1.getElementsByTagName("range");
                            Element e5 = (Element) n5.item(0);
                            NodeList nl5 = e5.getChildNodes();
                            quickLookB[j][3] = ((String) nl5.item(0).getNodeValue());
    
                            NodeList n6 = el1.getElementsByTagName("price");
                            Element e6 = (Element) n6.item(0);
                            NodeList nl6 = e6.getChildNodes();
                            quickLookB[j][4] = ((String) nl6.item(0).getNodeValue());
    
                            NodeList n7 = el1.getElementsByTagName("vol_remain");
                            Element e7 = (Element) n7.item(0);
                            NodeList nl7 = e7.getChildNodes();
                            quickLookB[j][5] = ((String) nl7.item(0).getNodeValue());
    
                            NodeList n8 = el1.getElementsByTagName("min_volume");
                            Element e8 = (Element) n8.item(0);
                            NodeList nl8 = e8.getChildNodes();
                            quickLookB[j][6] = ((String) nl8.item(0).getNodeValue());
    
                            NodeList n9 = el1.getElementsByTagName("expires");
                            Element e9 = (Element) n9.item(0);
                            NodeList nl9 = e9.getChildNodes();
                            quickLookB[j][7] = ((String) nl9.item(0).getNodeValue());
    
                            qlBSize++;
                        }
                    }
                    
                }
    
                JOptionPane.showMessageDialog(window,"quick look XML read in!");
    
                
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }catch(org.xml.sax.SAXException e){}catch(java.io.IOException e){}
    
            
        }

    This is probably a stupid easy question but im new to java (just finished my first java class at school) and am trying to learn this myself with nothing but internet guides to go on. So take it easy on me, im sure the way im going about reading the xml in is horrible, but as long as it works it serves my purpose.

    thanks!
    Last edited by tankhardrive; 02-13-2009 at 04:31 PM.

  2. #2
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    in....

    NodeList nlQlSell = docQl.getElementsByTagName("order");
    and
    NodeList nlQlBuy = docQl.getElementsByTagName("order");

    you're doing the EXACT search.....how do you suppose to find different things???
    wee in this search you are searching for all the elements with the tag "order", it doesn't matter where they are....

    a simple resolution is to first find the elements with tag "sell_orders" or "buy_orders" and on this element do the search for "order"

    example:
    Java Code:
    NodeList sellOrders= docQl.getElementsByTagName("sell_orders");
    for (int i=0;i< sellOrders.getLength();i++){
    				
    	Node node = sellOrders.item(i);
            if(node.getNodeType() == Node.ELEMENT_NODE){
                    	
                  	Element elem = (Element) node;
                    NodeList list2 = elem.getElementsByTagName("order");
    		for (int j=0;i<list.getLength();i++){
            				
    			Node order = list2.item(j);
                            if(order.getNodeType() == Node.ELEMENT_NODE){
                            	// do things with orders
                            }
            	}
                    	
            }
    }
    or you can learn how to use XPath and then simply do a search for:
    /quicklook/sell_orders/order
    /quicklook/buy_orders/order

  3. #3
    tankhardrive is offline Member
    Join Date
    Oct 2008
    Posts
    10
    Rep Power
    0

    Default

    thanks for your reply, its helped a lot! I've never used xml parsing before so i was just taking a stab at it with out any examples besides very basic ones.

    thank you again!

Similar Threads

  1. xml parsing
    By gaurav65176 in forum XML
    Replies: 5
    Last Post: 11-12-2008, 12:07 PM
  2. XML Parsing using Spring
    By niteshsharma in forum Web Frameworks
    Replies: 4
    Last Post: 10-01-2008, 01:15 PM
  3. parsing problem
    By Pooja Deshpande in forum Advanced Java
    Replies: 4
    Last Post: 05-29-2008, 02:59 PM
  4. Using SAXParserFactory for parsing XML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-20-2008, 10:42 AM
  5. Parsing URL
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 11:16 AM

Posting Permissions

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