Results 1 to 3 of 3
  1. #1
    JohnST is offline Member
    Join Date
    Dec 2009
    Posts
    20
    Rep Power
    0

    Default Ugly XML to parse

    I'm trying to pull "Wireless Dimmers" by name with DeviceID's from a XML on a home automation controller that's running in my house. The plan is to create a table to store the info which will be read by a program to create Jbuttons for lighting control. I don't know if I'm going about this the right way or not, maybe there is a better way to do this.

    Here's an example of what I'm sending to toggle a light

    Java Code:
    os.writeBytes("<c4soap name=\"SendToDeviceAsync\" async=\"True\"><param name=\"iddevice\" type=\"number\">");
            os.writeBytes(deviceId);
            os.writeBytes("</param><param name=\"data\" type=\"string\"><devicecommand owneridtype=\"\" owneriditem=\"-1\"><command>PUSH_TOGGLE_BUTTON</command><params></params></devicecommand></param></c4soap>\0");
    Here's an example of the xml which I want pull "Wireless Dimmers" and "Wireless Switches" from along with their corresponding deviceID #'s


    Java Code:
    <item>
    <id>97</id>
    <name>Wireless Switch</name>
    <type>6</type>
    <itemdata><large_image>devices_lg/dimmer.gif</large_image><small_image>devices_sm/dimmer.gif</small_image><config_data_file>light_ip_control4_ldz-5s1-w.c4i</config_data_file><tag>8b419d9b_7812_43b6_b23d_40c114322c23</tag></itemdata>
    <state>&lt;control4_light_properties&gt;&lt;hardware&gt;False&lt;/hardware&gt;&lt;/control4_light_properties&gt;</state><devicedata><c4id>14</c4id><copyright>Copyright 2004-2005 Control4 Corporation.  All rights reserved.</copyright><config/><name>Wireless Switch</name><small>devices_sm/dimmer.gif</small><large>devices_lg/dimmer.gif</large><control>light_ip_control4</control><composer_categories><category>Lighting</category></composer_categories><proxy>light</proxy><search_type>c4:light_ip_control4:LSZ-101</search_type><driver>100014</driver><states/><manufacturer>Control4</manufacturer><version>1.0</version><creator>Control4</creator><created>5/09/2005 12:30 PM</created><modified>11/03/2005 12:30 PM</modified><model>LSZ-101</model><front/><back/><top/><bottom/><left/><right/><connections><connection><id>5001</id><facing>6</facing><connectionname>Light</connectionname><type>2</type><consumer>False</consumer><audiosource>False</audiosource><videosource>False</videosource><linelevel>False</linelevel><classes><class><classname>LIGHT</classname></class></classes></connection><connection><id>300</id><facing>6</facing><connectionname>Top Button Link</connectionname><type>1</type><consumer>False</consumer><audiosource>False</audiosource><videosource>False</videosource><linelevel>True</linelevel><classes><class><classname>BUTTON_LINK</classname></class></classes></connection><connection><id>301</id><facing>6</facing><connectionname>Bottom Button Link</connectionname><type>1</type><consumer>False</consumer><audiosource>False</audiosource><videosource>False</videosource><linelevel>True</linelevel><classes><class><classname>BUTTON_LINK</classname></class></classes></connection><connection><id>302</id><facing>6</facing><connectionname>Toggle Button Link</connectionname><type>1</type><consumer>False</consumer><audiosource>False</audiosource><videosource>False</videosource><linelevel>True</linelevel><classes><class><classname>BUTTON_LINK</classname></class></classes></connection><connection><id>6001</id><facing>6</facing><connectionname>ZIGBEE</connectionname><type>4</type><consumer>True</consumer><audiosource>False</audiosource><videosource>False</videosource><linelevel>False</linelevel><classes><class><classname>ZIGBEE</classname></class></classes></connection></connections><capabilities><can_detach_buttons>True</can_detach_buttons><can_detach_led>True</can_detach_led><do_push>False</do_push><do_release>False</do_release><double_button>True</double_button><has_double_led>True</has_double_led><has_press_events>True</has_press_events><has_release_events>True</has_release_events><has_single_led>False</has_single_led><on_off>True</on_off><ramp_level>False</ramp_level><set_level>False</set_level><single_button>False</single_button><wireless>True</wireless></capabilities></devicedata><subitems>
    <item>
    <id>98</id>
    <name>Cieling Fan</name>
    <type>7</type>
    <itemdata><large_image>devices_lg/dimmer.gif</large_image><small_image>devices_sm/dimmer.gif</small_image><config_data_file>light.c4i</config_data_file><tag>8b419d9b_7812_43b6_b23d_40c114322c23</tag></itemdata>
    <devicedata><copyright>Copyright 2004-2005 Control4 Corporation.  All rights reserved.</copyright><creator>Control4</creator><created>5/09/2005 12:30 PM</created><modified>5/09/2005 12:30 PM</modified><commands><command><id>0</id><name>ON</name><description>Turn on the NAME</description></command><command><id>1</id><name>OFF</name><description>Turn off the NAME</description></command><command><id>2</id><name>SET_LEVEL</name><description>Set Level on the NAME to INTEGER</description></command><command><id>3</id><name>RAMP_TO_LEVEL</name><description>Ramp to Level INTEGER on the NAME over TIME STRING</description></command><command><id>4</id><name>TOGGLE</name><description>Toggle the NAME</description></command><command><id>5</id><name>SET_ALL_LED</name><description>Set all LEDs on the NAME to STRING</description></command><command><id>6</id><name>SET_TOP_LED_ON</name><description>Set top LED on the NAME to STRING when on</description></command><command><id>7</id><name>SET_TOP_LED_OFF</name><description>Set top LED on the NAME to STRING when off</description></command><command><id>8</id><name>SET_BOTTOM_LED_ON</name><description>Set bottom LED on the NAME to STRING when on</description></command><command><id>9</id><name>SET_BOTTOM_LED_OFF</name><description>Set bottom LED on the NAME to STRING when off</description></command><command><id>10</id><name>PUSH_TOP_BUTTON</name><description>Press the top button on the NAME</description></command><command><id>11</id><name>RELEASE_TOP_BUTTON</name><description>Release the top button on the NAME</description></command><command><id>12</id><name>PUSH_BOTTOM_BUTTON</name><description>Press the bottom button on the NAME</description></command><command><id>13</id><name>RELEASE_BOTTOM_BUTTON</name><description>Release the bottom button on the NAME</description></command><command><id>14</id><name>PUSH_TOGGLE_BUTTON</name><description>Push the toggle button on the NAME</description></command><command><id>15</id><name>RELEASE_TOGGLE_BUTTON</name><description>Release the toggle button on the NAME</description></command></commands><conditionals><conditional><id>0</id><name>ON</name><description>NAME is on</description></conditional><conditional><id>1</id><name>OFF</name><description>NAME is off</description></conditional><conditional><id>2</id><name>LEVEL</name><description>NAME level is INTEGER</description></conditional></conditionals><events><event><id>1</id><description>When NAME top button is pushed</description></event><event><id>2</id><description>When NAME top button is released</description></event><event><id>3</id><description>When NAME bottom button is pushed</description></event><event><id>4</id><description>When NAME bottom button is released</description></event><event><id>5</id><description>When NAME level changes</description></event></events><name>Light</name><small>devices_sm/dimmer.gif</small><large>devices_lg/dimmer.gif</large><control>light</control><composer_categories><category>Lighting</category></composer_categories><proxy/><driver>10</driver><powermanagementmethod>AlwaysOn</powermanagementmethod><states/><connections><connection><id>5001</id><facing>6</facing><connectionname>Light</connectionname><type>2</type><consumer>True</consumer><audiosource>False</audiosource><videosource>False</videosource><linelevel>False</linelevel><classes><class><classname>LIGHT</classname></class></classes></connection></connections><capabilities><can_detach_buttons>True</can_detach_buttons><can_detach_led>True</can_detach_led><do_push>False</do_push><do_release>False</do_release><double_button>True</double_button><has_double_led>True</has_double_led><has_press_events>True</has_press_events><has_release_events>True</has_release_events><has_single_led>False</has_single_led><on_off>True</on_off><ramp_level>False</ramp_level><set_level>False</set_level><single_button>False</single_button><wireless>True</wireless></capabilities></devicedata></item>
    </subitems>
    </item>
    I don't want
    <id>97</id>
    <name>Wireless Switch</name>

    The correct ID to control this switch is: 98

    <id>98</id>
    <name>Cieling Fan</name>


    Here is what I am using to list items along with DeviceId's but I would like to filter out Lighting only for now.

    Java Code:
    import java.io.File;
    import org.w3c.dom.Document;
    import org.w3c.dom.*;
    
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException; 
    
    public class parserTest{
    
       public static void main (String argv []){
        try {
    
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                 
        
    
                Document doc = docBuilder.parse (new File("GetItems.xml"));
    
                // normalize text representation
                doc.getDocumentElement ().normalize ();
                System.out.println ("Root element of the doc is " + 
                     doc.getDocumentElement().getNodeName());
    
    
                NodeList listOfItems = doc.getElementsByTagName("subitems");
                int totalItems = listOfItems.getLength();
                System.out.println("Total no of <item> tags found : " + totalItems);
    
                for(int s=0; s<listOfItems.getLength() ; s++){
    
    
                    Node firstItemNode = listOfItems.item(s);
                    if(firstItemNode.getNodeType() == Node.ELEMENT_NODE){
    
    
                        Element firstItemElement = (Element)firstItemNode;
    
                        //-------
                        NodeList firstIdList = firstItemElement.getElementsByTagName("id");
                        Element firstIdElement = (Element)firstIdList.item(0);
    
                        NodeList textIdList = firstIdElement.getChildNodes();
                        System.out.println("id   : " + 
                               ((Node)textIdList.item(0)).getNodeValue().trim());
    
                        //-------
                        NodeList nameList = firstItemElement.getElementsByTagName("name");
                        Element nameElement = (Element) nameList.item(0);
    
                        NodeList textNameList = nameElement.getChildNodes();
                        System.out.println("name : " + 
                               ((Node)textNameList.item(0)).getNodeValue().trim());
    
                        //----
                        NodeList typeList = firstItemElement.getElementsByTagName("type");
                        Element typeElement = (Element) typeList.item(0);
    
                        NodeList textTypeList = typeElement.getChildNodes();
                        System.out.println("type : " + 
                               ((Node)textTypeList.item(0)).getNodeValue().trim());
    
                        //------
    
    
                    }//end of if clause
    
    
                }//end of for loop with s var
    
    
            }catch (SAXParseException err) {
            System.out.println ("** Parsing error" + ", line " 
                 + err.getLineNumber () + ", uri " + err.getSystemId ());
            System.out.println(" " + err.getMessage ());
    
            }catch (SAXException e) {
            Exception x = e.getException ();
            ((x == null) ? e : x).printStackTrace ();
    
            }catch (Throwable t) {
            t.printStackTrace ();
            }
            //System.exit (0);
    
        }//end of main
    
    }
    Attached Files Attached Files

  2. #2
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    Sorry I only had time to look quickly - few things:

    You should be using XPath to search XML; the getElementsByTagName method is stupidly inefficient as it searches the entirety of the XML and if I recall correctly, creates live NodeLists each time you call it (although this may not be a problem for you, you should still use XPath).

  3. #3
    JohnST is offline Member
    Join Date
    Dec 2009
    Posts
    20
    Rep Power
    0

    Default

    Thanks for your advice, I am a noob so I have a lot to learn. I will look into XPath.

Similar Threads

  1. How to parse a .db file ?
    By aminov in forum JDBC
    Replies: 0
    Last Post: 08-06-2009, 04:11 PM
  2. SAX Parse
    By bluefloyd8 in forum New To Java
    Replies: 1
    Last Post: 01-25-2008, 03:57 PM
  3. how to parse an xml file
    By oregon in forum XML
    Replies: 3
    Last Post: 08-01-2007, 04:53 PM
  4. How to parse in html
    By paty in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 12:29 AM
  5. Parse error
    By Eric in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 06:32 PM

Posting Permissions

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