Parsing XML from the Net - Using the SAXParser

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Postby songotho » Tue Apr 14, 2009 6:52 pm

:D sorry padde,

I want some difference tags because I want to get any data where I want.
If I have multiple difference father tags but child tags's the same. And I want get only data in the tag second.
But when I use your function, this output's always the last tag.
How can I do to get data in the tag second?

Thanks,
--Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Top

Postby padde » Tue Apr 14, 2009 8:17 pm

I think it would help if you could post a real example of those xml file you dealing with
and descripe what exactly you whant to do with them. I still dont get it why you want multiple
EntityX tags which stores same data. For sorting purposes or something like that i would suggest
you give those tags an ID attribute and sort the vector afterwards accordingly.
If you try to filter some given xml files and know that you want to handle every second time a tag
appears i would suggest a counter that triggers whether the data gets stored or ignored.
Right now the opening Entity tag just triggers creation of a new dataset and the closing entity tag
triggers adding the current dataset onto the vector.


Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby songotho » Tue Apr 14, 2009 9:04 pm

Hi, Padde

So would I contact with you? I wanna ask you something about my project.

Thanks,
--Alex
Last edited by songotho on Thu Apr 23, 2009 8:08 am, edited 1 time in total.
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

XML complex data

Postby marcovic » Sun Apr 19, 2009 11:03 am

Hi,
great tutorial but unfortunatly i've tried to apply initial code with a complex xml file and i met some problems.
Here an extract of my file:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <Action jcr:path="/Actions" xsn:name="Action" children="2" totalChildren="2">
  2. <xsn:icon jcr:primaryType="xsn:image" jcr:path="/Actions/xsn:icon" xsn:mimeType="image/png" xsn:length="25319"/>
  3.         <Item2344 jcr:primaryType="xsn:text" jcr:path="/texts/20090325/Item2344" xsn:name="MyText1" xsn:version="1.0" xsn:description="No description available!" xsn:longDescription="No long description available!">
  4.                 <xsn:icon jcr:primaryType="xsn:image" jcr:path="/texts/20090325/Item2344/pic/xsn:icon" xsn:mimeType="image/jpeg" xsn:length="50385"/>
  5.                 <xsn:screenshots/>
  6.                 <xsn:review children="2" totalChildren="2">
  7.                         <xsn:review jcr:primaryType="xsn:review" jcr:path="/texts/20090325/Item2344/xsn:review"/>
  8.                 </xsn:review>
  9.                 <xsn:related children="1" totalChildren="1">
  10.                         <ItemRelated2555 jcr:primaryType="xsn:text" jcr:path="/texts/20090325/ItemRelated2555"/>
  11.                 </xsn:related>
  12.         </Item2344>
  13.         <Item234477 jcr:primaryType="xsn:text" jcr:path="/texts/20090325/Item234477" xsn:name="MyText17" xsn:version="1.0" xsn:description="No description available!" xsn:longDescription="No long description available!">
  14.                 <xsn:icon jcr:primaryType="xsn:image" jcr:path="/texts/20090325/Item234477/pic/xsn:icon" xsn:mimeType="image/jpeg" xsn:length="50385"/>
  15.                 <xsn:screenshots/>
  16.                 <xsn:review children="2" totalChildren="2">
  17.                         <xsn:review jcr:primaryType="xsn:review" jcr:path="/texts/20090325/Item234477/xsn:review"/>
  18.                 </xsn:review>
  19.                 <xsn:related children="1" totalChildren="1">
  20.                         <ItemRelated2555 jcr:primaryType="xsn:text" jcr:path="/texts/20090325/ItemRelated255577"/>
  21.                 </xsn:related>
  22.         </Item234477>
  23. </Action>
  24.  
Parsed in 0.007 seconds, using GeSHi 1.0.8.4


Of course, what i want to do is extract both tags and attribute.
The first problem was that if i try to read tag name as in:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if (localName.equals("xsn:icon")
  2.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


it doesn't work. How can i do that?

Going ahead i could meet a tag like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <xsn:screenshots/>
  2.  
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


that is "self-closed" and not in this form:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <xsn:screenshots>...</xsn:screenshots>
  2.  
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Does "endElement" method works in the same way?

Last but not the least...
Imagine that i have more childs - better - an undefinied number of childs (Item2344, Item234477, Item23442232...) every one with a different name; is it possible to go through my xml file without list every tag in startElement (and endElement) so going recursively on my file?

Many thanks in advance,

marcovic
marcovic
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Nov 24, 2008 5:34 pm

Postby songotho » Sun Apr 19, 2009 11:17 am

Hi,

Your file is not xml file, it's the xslt file. I think that you need read data in your xml file because the structure in xslt and xml are difference.

Regards,

--Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Parsing HTML

Postby BeBoo » Wed Apr 22, 2009 9:42 pm

Hi there. Thanks for this. It works great. I do have one question, tho. I am using the XML parser to parse HTML (as I was told that would be the easiest way). Let's say I have this:

Code: Select all
<div>foo <a href="#">bar</a> foo</div>


I am sucessful in grabbing all the divs without links in them but this one causes issues because it seems to treat "foo" "bar" and "foo" as three different div instances. I am am basically outputting this to a textview and am seeing it appear as:

foo
bar
foo


Is there a way to tell it to ignore other <> between <div> and </div>? Is XML parsing really the easiest way? Should I use some form of regex instead?

Thanks!

Brandon
BeBoo
Freshman
Freshman
 
Posts: 3
Joined: Wed Apr 22, 2009 9:31 pm

Top

Postby songotho » Fri Apr 24, 2009 8:27 am

Hi,

So What do you want to get in tag? You want output foo or bar in your example?
Code: Select all
<div>foo <a href="#">bar</a> foo</div>

You can use some conditions to get data you want.

Cheer,

--Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Postby BeBoo » Fri Apr 24, 2009 1:37 pm

songotho wrote:Hi,

So What do you want to get in tag? You want output foo or bar in your example?
Code: Select all
<div>foo <a href="#">bar</a> foo</div>

You can use some conditions to get data you want.

Cheer,

--Alex


Thanks for getting back to me. I'd like it to be "foobarfoo" instead of three instances since it's all in the div. I'm currently using regex with replaceall which is very slow compared to xml parsing so if this is possible, i'd like to go back to it.

Thanks again,
Brandon
BeBoo
Freshman
Freshman
 
Posts: 3
Joined: Wed Apr 22, 2009 9:31 pm

parsing xml

Postby myster23 » Mon May 04, 2009 1:02 pm

I read the source code of Parsing XML from the NET - Using the SAX Parser but I want to parse the xml file from C:\example.xml, so I replaced the code URL url to this:
InputSource in=new InputSource( );

in.setCharacterStream(new StringReader("C:\example.xml"));

xr.parse(in);

and the screen displays the following error:
line 1, column 2: not well formed
(invalid token)

What does it mean? and how to fix it so that the screen displays the contents of the each tag?
myster23
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Apr 23, 2009 7:53 am

Postby kryptonum » Thu May 07, 2009 10:38 am

Nice sample!
kryptonum
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Mar 18, 2009 3:21 pm
Location: Shanghai

Postby b9507 » Tue May 26, 2009 6:02 pm

hi , songotho

I have the same problem as yours. Maybe we can exchange msn to discuss it more .

Because of your xml data, i think we are probably in the same school (NTUST?) :D

My school email address : B9507017@mail.ntust.edu.tw



regard,

Ben Tseng
b9507
Once Poster
Once Poster
 
Posts: 1
Joined: Tue May 26, 2009 5:52 pm

Getting trouble in fetching the data inside the Tag

Postby rajashri » Fri May 29, 2009 9:05 am

Hello Plusminus,
I went through this tutorial. It is really helpful. Biu I face some problem in fetching the data inside the <tag></tag>.
the XML from which i am fetching data is having multiple rows. I need to get the attributes as well as data.
here is the problem
1) If there is any &amp; or &nbsp; like value present inside the <tag></tag> in the xml, the value is not getting retrieved. I tried to replace them with & and space but no results.
2) the <tag></tag> is having more than one line. Problem is when a word i ending with a character and the next word begins with the same character, its not fetching properly. For example if the data is like "Hello olive i am marker. How are you doing, good?"

Please help me out on this.
Thanks
R Dash
rajashri
Once Poster
Once Poster
 
Posts: 1
Joined: Fri May 29, 2009 8:42 am

Postby kali » Sat May 30, 2009 10:01 am

hai i m trying to connect to server bu it is not, but a blanck screen is displaying.
hai is devloper.android.com is working or not becuse i cant access from my pc.
hai friends reply me imiditeally.
kali
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Tue Jan 27, 2009 1:31 pm

Postby kali » Mon Jun 01, 2009 6:06 am

hai plusminus
i read this tutorial. it was too good.
and then i made a program but it gives error "unknown error"
when i debug it i find that SAXFactory spf is null and so xr is also nul thus cannot parrse the data
and error will generated
Last edited by kali on Tue Jun 02, 2009 4:41 am, edited 1 time in total.
kali
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Tue Jan 27, 2009 1:31 pm

Postby arams » Mon Jun 01, 2009 8:08 am

Hello rajashri ,
String a;
xml tag like this:
<innertag>tom & jerry</innertag>

Expected output:tom & jerry
you get the output:tom


@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("outertag")) {
this.in_outertag = true;
}else if (localName.equals("innertag")) {

a=qName; :roll: this.in_innertag = true;
}else if (localName.equals("mytag")) {
this.in_mytag = true;
}else if (localName.equals("tagwithnumber")) {
String attrValue = atts.getValue("thenumber");
int i = Integer.parseInt(attrValue);
myParsedExampleDataSet.setExtractedInt(i);
}
}




@Override
public void characters(char ch[], int start, int length) {
if(this.innertag){
a+=String.valueOf(ch,start,length);
}
}
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests