Can anyone show me how I would parse this?
I am not exactly sure how I would go about doing this. Here is the
parsing code I have.

public class TwitterResponse {

        private static final String TOP_LEVEL_NODE_NAME = "status"; // the
top level <status> node to be read
        private static final String USER_NODE_NAME = "user";            // the 
<user>
node which has sub-nodes
        private static final String TEXT_NODE_NAME = "#text";           // basic
textual element

        DocumentBuilder builder;
        NodeList nodes;
        HashMap<Integer,TwitterEntry> entries = new
HashMap<Integer,TwitterEntry>();

        /**
         * Create JAXP based parser and read XML from an InputStream.
         *
         * @param xmlStream InputStream with XML data
         *
         * @throws SAXException
         * @throws IOException
         * @throws ParserConfigurationException
         */
        public TwitterResponse()
                throws SAXException, IOException, ParserConfigurationException {
                builder = 
DocumentBuilderFactory.newInstance().newDocumentBuilder();
        }

        public TwitterResponse parse(InputStream xmlStream)
                throws SAXException, IOException, ParseException,
MalformedURLException {

                Document d = builder.parse(xmlStream);
                nodes = d.getElementsByTagName(TOP_LEVEL_NODE_NAME);
                readEntries();
                return this;
        }

        public TwitterResponse parse(String xmlString)
                throws SAXException, IOException, ParseException,
MalformedURLException {

                Document d = builder.parse(new InputSource(new StringReader
(xmlString)));
                nodes = d.getElementsByTagName(TOP_LEVEL_NODE_NAME);
                readEntries();
                return this;
        }

        /**
         * Number of items read by this parser
         *
         * @return Get the total number of &lt;status&gt; nodes returned
         */
        public int getNumberOfItems() {
                return entries.size();
        }

        /**
         * Get the index'th &lt;status&gt node by walking the DOM
         *
         * @param index the index of the node you want to get
         * @return TwitterEntry representation of that node
         * @throws ParseException
         */
        public TwitterEntry getItemAt(int index) {
                return entries.get(index);
        }

        //

        private void readEntries()
                throws ParseException, MalformedURLException {

                for (int i = 0; i < nodes.getLength(); i++) {
                        entries.put(i, readEntry(i));
                }
        }

        // [FIXME] Need to rewrite the parsing code, does not support #8220;
like codes

        private TwitterEntry readEntry(int index)
                throws ParseException, MalformedURLException {

                TwitterEntry entry = new TwitterEntry();

                // nd is the list of child nodes of the <status> element with 
the
specified index
                NodeList nd = nodes.item(index).getChildNodes();

                // loop through all the children
                for(int i = 0; i < nd.getLength(); i++)
                {
                        // for some reason JAXP treats white space as #text 
nodes, so let's
skip them
                        if(!nd.item(i).getNodeName().equals(TEXT_NODE_NAME))
                        {
                                // the <user> element has subnodes so we need 
additional
processing
                                
if(nd.item(i).getNodeName().equals(USER_NODE_NAME))
                                {
                                        // nd_usr will contain all the child 
nodes of <user>
                                        NodeList nd_usr = 
nd.item(i).getChildNodes();

                                        for(int j=0; j<nd_usr.getLength(); j++)
                                        {
                                                // once again we are skipping 
blank lines and whitespace
                                                
if(!nd_usr.item(j).getNodeName().equals(TEXT_NODE_NAME))
                                                {
                                                        // [TODO] figure out 
how to prevent User ID from overwriting
post ID
                                                        String value ="", name 
= nd_usr.item(j).getNodeName();  // get
the name of the current node
                                                        
if(nd_usr.item(j).hasChildNodes())                                              
// if not empty, also
get the value
                                                                value = 
nd_usr.item(j).getFirstChild().getNodeValue();

                                                        
entry.addAttribute(name, value);
                                                }
                                        }
                                }
                                else
                                {
                                        String value ="", name = 
nd.item(i).getNodeName();
                                        if(nd.item(i).hasChildNodes())
                                                value = 
nd.item(i).getFirstChild().getNodeValue();

                                        entry.addAttribute(name, value);
                                }
                        }
                }

                return entry;
        }
}

On Apr 16, 5:10 pm, Abraham Williams <[email protected]> wrote:
> http://code.google.com/p/twitter-api/issues/detail?id=75
>
>
>
> On Thu, Apr 16, 2009 at 16:54, Chad Etzel <[email protected]> wrote:
>
> > On Thu, Apr 16, 2009 at 5:11 PM, Travis James
> > <[email protected]> wrote:
>
> > > Thank you Doug. That is where I was wrong. Is there anyway to excuse
> > > the HTML and just get the Application Name?
>
> > I believe they've stated that will happen in API v2.  Right now you
> > just have to parse through the HTML to grok out the app name.
>
> > -Chad
>
> > > On Apr 16, 12:01 pm, Doug Williams <[email protected]> wrote:
> > >> Source parameters that come from outside apps are encoded HTML. Are you
> > >> accounting for this Travis? See the "source" heading on the Return
> > Values
> > >> page [1]
>
> > >> 1.http://apiwiki.twitter.com/Return-Values
>
> > >> Doug Williams
> > >> Twitter API Supporthttp://twitter.com/dougw
>
> > >> On Thu, Apr 16, 2009 at 9:47 AM, Chad Etzel <[email protected]>
> > wrote:
>
> > >> > On Thu, Apr 16, 2009 at 12:35 PM, Doug Williams <[email protected]>
> > wrote:
> > >> > > What is the source parameter you are passing with your application?
>
> > >> > I don't think that's what he's asking.  I think he's having trouble
> > >> > parsing the source info of tweets coming from *other* apps.  I looked
> > >> > through the Java and didn't really see where it is doing the parsing
> > >> > so I must be missing it.  I'm assuming it is looking at XML version of
> > >> > the data?  Is this for REST or Search API?
> > >> > -Chad
>
> > >> > > Doug Williams
> > >> > > Twitter API Support
> > >> > >http://twitter.com/dougw
>
> > >> > > On Thu, Apr 16, 2009 at 7:34 AM, Travis James <
> > >> > [email protected]>
> > >> > > wrote:
>
> > >> > >> package jtwitter;
>
> > >> > >> import java.net.MalformedURLException;
> > >> > >> import java.text.ParseException;
> > >> > >> import java.text.SimpleDateFormat;
> > >> > >> import java.util.Date;
> > >> > >> import java.util.Locale;
>
> > >> > >> public class TwitterEntry {
>
> > >> > >>        // Twitter Entry Nodes (each corresponds to a XML node with
> > the
> > >> > >> same
> > >> > >> name)
> > >> > >>        public static final String CREATED_AT = "created_at";
> > >> > >>        public static final String ID = "id";
> > >> > >>        public static final String TEXT = "text";
> > >> > >>        public static final String SOURCE = "source";
>
> > >> > >>        private Date createdAt;
> > >> > >>        private int id;
> > >> > >>        private String text;
> > >> > >>        private String source;
> > >> > >>        private TwitterUser user;
>
> > >> > >>        //This is currently the date format used by twitter
> > >> > >>        public static final String TWITTER_DATE_FORMAT = "EEE MMM dd
> > >> > >> kk:mm:ss
> > >> > >> Z yyyy";
>
> > >> > >>        public TwitterEntry(Date createdAt, int id, String text,
> > String
> > >> > >> source, TwitterUser user) {
> > >> > >>                super();
> > >> > >>                this.createdAt = createdAt;
> > >> > >>                this.id = id;
> > >> > >>                this.text = text;
> > >> > >>                this.source = source;
> > >> > >>                this.user = user;
> > >> > >>        }
>
> > >> > >>        public TwitterEntry() {
> > >> > >>                this.user = new TwitterUser();
> > >> > >>        }
>
> > >> > >>        public Date getCreatedAt()
> > >> > >>        {
> > >> > >>                return createdAt;
> > >> > >>        }
>
> > >> > >>        public void setCreatedAt(Date createdAt)
> > >> > >>        {
> > >> > >>                this.createdAt = createdAt;
> > >> > >>        }
>
> > >> > >>        public int getId()
> > >> > >>        {
> > >> > >>                return id;
> > >> > >>        }
>
> > >> > >>        public void setId(int id)
> > >> > >>        {
> > >> > >>                this.id = id;
> > >> > >>        }
>
> > >> > >>        public String getText()
> > >> > >>        {
> > >> > >>                return text;
> > >> > >>        }
>
> > >> > >>        public void setText(String text)
> > >> > >>        {
> > >> > >>                this.text = text;
> > >> > >>        }
>
> > >> > >>        public String getSource()
> > >> > >>        {
> > >> > >>                return source;
> > >> > >>        }
>
> > >> > >>        public void setSource(String source)
> > >> > >>        {
> > >> > >>                this.source = source;
> > >> > >>        }
>
> > >> > >>        public TwitterUser getUser()
> > >> > >>        {
> > >> > >>                return user;
> > >> > >>        }
>
> > >> > >>        public void setUser(TwitterUser user)
> > >> > >>        {
> > >> > >>                this.user = user;
> > >> > >>        }
>
> > >> > >>       �...@override
> > >> > >>        public int hashCode()
> > >> > >>        {
> > >> > >>                final int PRIME = 31;
> > >> > >>                int result = 1;
> > >> > >>                result = PRIME * result + id;
> > >> > >>                return result;
> > >> > >>        }
>
> > >> > >>       �...@override
> > >> > >>        public boolean equals(Object obj)
> > >> > >>        {
> > >> > >>                if (this == obj)
> > >> > >>                        return true;
> > >> > >>                if (obj == null)
> > >> > >>                        return false;
> > >> > >>                if (getClass() != obj.getClass())
> > >> > >>                        return false;
> > >> > >>                final TwitterEntry other = (TwitterEntry) obj;
> > >> > >>                if (id != other.id)
> > >> > >>                        return false;
> > >> > >>                return true;
> > >> > >>        }
>
> > >> > >>        public void addAttribute(String key, String value)
> > >> > >>                throws ParseException, MalformedURLException {
>
> > >> > >>                if(key.equals(CREATED_AT))
> > >> > >>                        this.setCreatedAt(makeDate(value));
> > >> > >>                else if(key.equals(ID))
> > >> > >>                        this.setId(Integer.parseInt(value));
> > >> > >>                else if (key.equals(TEXT))
> > >> > >>                        this.setText(value);
> > >> > >>                else if (key.equals(SOURCE))
> > >> > >>                        this.setSource(value);
> > >> > >>                else if (key.equals(TwitterUser.NAME))
> > >> > >>                        this.getUser().setName(value);
> > >> > >>                else if (key.equals(TwitterUser.SCREEN_NAME))
> > >> > >>                        this.getUser().setScreenName(value);
> > >> > >>                else if (key.equals(TwitterUser.LOCATION))
> > >> > >>                        this.getUser().setLocation(value);
> > >> > >>                else if (key.equals(TwitterUser.DESCRIPTION))
> > >> > >>                        this.getUser().setDescription(value);
> > >> > >>                else if (key.equals(TwitterUser.PROFILE_IMAGE_URL))
> > >> > >>                        this.getUser().setProfileImageURL(value);
> > >> > >>                else if (key.equals(TwitterUser.URL))
> > >> > >>                        this.getUser().setUrl(value);
> > >> > >>                else if (key.equals(TwitterUser.IS_PROTECTED))
>
> > >> > >>  this.getUser().setProtected(Boolean.parseBoolean(value));
> > >> > >>        }
>
> > >> > >>        public String toString() {
> > >> > >>                return "Created At: " + this.getCreatedAt() + "; "
> > >> > >>                + "Status: " + this.getText() + "; "
> > >> > >>                + "User: " + this.getUser();
> > >> > >>        }
>
> > >> > >>        private Date makeDate(String date)
> > >> > >>                throws ParseException {
> > >> > >>                return new SimpleDateFormat(TWITTER_DATE_FORMAT,
> > >> > >> Locale.US).parse
> > >> > >> (date);
> > >> > >>        }
>
> > >> > >> }
>
> > >> > >> On Apr 16, 7:41 am, Cameron Kaiser <[email protected]> wrote:
> > >> > >> > > I have a Twitter application that I created in Java and I am
> > able to
> > >> > >> > > get the source parameter by using XMLParser.
>
> > >> > >> > > I have my application to where it displays "Username from
> > Source,
> > >> > >> > > Status Text"
>
> > >> > >> > > Username works perfectly, but source only displays when a user
> > >> > updates
> > >> > >> > > from the web, if they updated from a different application, all
> > it
> > >> > >> > > displays is "<".
>
> > >> > >> > > Can anyone shed some light on this?
>
> > >> > >> > A relevant section of code is always helpful.
>
> > >> > >> > --
> > >> > >> > ------------------------------------
> > >> > >> > personal:http://www.cameronkaiser.com/--
> > >> > >> >   Cameron Kaiser * Floodgap Systems *www.floodgap.com*
> > >> > >> > [email protected]
> > >> > >> > -- Put down your guns, it's Weasel Stomping Day!
> > >> > >> > ------------------------------
>
> --
> Abraham Williams |http://the.hackerconundrum.com
> Hacker |http://abrah.am|http://twitter.com/abraham
> Web608 | Community Evangelist |http://web608.org
> This email is: [ ] blogable [x] ask first [ ] private.
> Sent from Madison, Wisconsin, United States

Reply via email to