prewk/XmlStreamer

question

Closed this issue · 4 comments

hello.

I am tasting your parser. and it seams good but i cant figure out how to extract all values.

class SimpleXmlStreamer extends XmlStreamer {
public function processNode($xmlString, $elementName, $nodeIndex) {
$xml = simplexml_load_string($xmlString);
$propertynumber = (string)$xml->PropertyNumber;
$propertyname = (string)$xml->PropertyName;
echo "$nodeIndex: Extracted string '$propertynumber': '$propertyname' from parent node '$elementName'\n";
return true;
}
}

I think i should do a for or foreach to request all values but since it is just a chunk of the xml file i dont know how to do it.

thanx

Hi,

Could you provide me with a snippet of the XML file so I can get a better
understanding of what you're trying to extract?
The code you've provided me with in the e-mail tries to do this:

Expected XML file:

123 456 abc def

Will yield results:

0: Extracted string '123': '456 from parent node 'SomeNode'
1: Extracted string 'abc': 'def' from parent node 'SomeNode'

The parameters for the processNode method are:
$xmlString - The whole XML node (no chunks, a whole node) in string format
$elementName - The name of the node
$nodeIndex - 0-indexed number counting up for every node processed

You shouldn't need to worry about chunks at all, since the purpose of the
class is to trigger the processNode method only when a whole node has been
buffered.

I hope that helped, but if you want some more help I can take a look if you
can provide me with a snippet of XML that you'll be parsing!

/oskar.thornblad@gmail.com

2012/6/17 tragoro <
reply@reply.github.com

hello.

I am tasting your parser. and it seams good but i cant figure out how to
extract all values.

class SimpleXmlStreamer extends XmlStreamer {
public function processNode($xmlString, $elementName, $nodeIndex) {
$xml = simplexml_load_string($xmlString);
$propertynumber = (string)$xml->PropertyNumber;
$propertyname = (string)$xml->PropertyName;
echo "$nodeIndex: Extracted string '$propertynumber':
'$propertyname' from parent node '$elementName'\n";
return true;
}
}

I think i should do a for or foreach to request all values but since it is
just a chunk of the xml file i dont know how to do it.

thanx


Reply to this email directly or view it on GitHub:
#1

My xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<HotelProperties>
<Property>
  <PropertyNumber>1</PropertyNumber>
  <PropertyName></PropertyName>
  <ThumbnailPictureURL></ThumbnailPictureURL>
  <Address>
    <Street></Street>
    <PostalCode></PostalCode>
    <City></City>
    <MainRegion></MainRegion>
    <Country_Code></Country_Code>
    <CoordinateAccuracy></CoordinateAccuracy>
    <Latitude></Latitude>
    <Longitude></Longitude>
    <LocationNumber></LocationNumber>
  </Address>
  <HoteldeRating></HoteldeRating>
  <Descriptions>
    <HotelDescription>
      <Language></Language>
      <Text></Text>
      <ContentType></ContentType>
    </HotelDescription>
  </Descriptions>
</Property>  
<Property>
  <PropertyNumber>2</PropertyNumber>
  <PropertyName></PropertyName>
  <ThumbnailPictureURL></ThumbnailPictureURL>
  <Address>
    <Street></Street>
    <PostalCode></PostalCode>
    <City></City>
    <MainRegion></MainRegion>
    <Country_Code></Country_Code>
    <CoordinateAccuracy></CoordinateAccuracy>
    <Latitude></Latitude>
    <Longitude></Longitude>
    <LocationNumber></LocationNumber>
  </Address>
  <HoteldeRating></HoteldeRating>
  <Descriptions>
    <HotelDescription>
      <Language></Language>
      <Text></Text>
      <ContentType></ContentType>
    </HotelDescription>
  </Descriptions>
</Property>  
</HotelProperties>

I am trying to extract all property nodes one by one to insert the values in mysql but my problem is that the parser is getting only the first value.

I think I should modify your parser somehow for this.

Thank you for your reply,
Csongor

Hi again,
Sorry for the late answer.

This should work by parsing through the nodes one by one and saving them to
the class property $allProperties


require_once("XmlStreamer.php");

class SimpleXmlStreamer extends XmlStreamer {
public $allProperties = array();
public function processNode($xmlString, $elementName, $nodeIndex) {
$xml = simplexml_load_string($xmlString);
$propertynumber = (string)$xml->PropertyNumber;
$propertyname = (string)$xml->PropertyName;

           $this->allProperties[] = array("PropertyNumber" =>

$propertynumber, "PropertyName" => $propertyname);

           return true;
   }

}

// Construct instance
$streamer = new SimpleXmlStreamer(....................);
// Parse trough the whole xml (this will trigger the method processNode as
many times as the number of nodes in the XML)
$streamer->parse();

// Now we should have a populated array
$allProperties = $streamer->allProperties;

// You can go use this however you like
foreach ($allProperties as $index => $properties) {
$number = $properties["PropertyNumber"];
$name = $properties["PropertyName"];

// Maybe escape them ....
.......

// And maybe insert them into your database or however you'd like to use
the data
$sql = "INSERT INTO ............ (number, name) VALUES
('$numberEscaped', '$nameEscaped')";
mysql_query($sql);
}


Let me know if this approach works out for you!

/Oskar

2012/6/18 tragoro <
reply@reply.github.com

The xml again:

<HoteldeProperties>
<Property>
 <PropertyNumber>1</PropertyNumber>
 <PropertyName></PropertyName>
 <ThumbnailPictureURL></ThumbnailPictureURL>
 <Address>
   <Street></Street>
   <PostalCode></PostalCode>
   <City></City>
   <MainRegion></MainRegion>
   <Country_Code></Country_Code>
   <CoordinateAccuracy></CoordinateAccuracy>
   <Latitude></Latitude>
   <Longitude></Longitude>
   <LocationNumber></LocationNumber>
 </Address>
 <HoteldeRating></HoteldeRating>
 <Descriptions>
   <HotelDescription>
     <Language></Language>
     <Text></Text>
     <ContentType></ContentType>
       </HotelDescription>
 </Descriptions>
</Property>
<Property>
 <PropertyNumber>2</PropertyNumber>
 <PropertyName></PropertyName>
 <ThumbnailPictureURL></ThumbnailPictureURL>
 <Address>
   <Street></Street>
   <PostalCode></PostalCode>
   <City></City>
   <MainRegion></MainRegion>
   <Country_Code></Country_Code>
   <CoordinateAccuracy></CoordinateAccuracy>
   <Latitude></Latitude>
   <Longitude></Longitude>
   <LocationNumber></LocationNumber>
 </Address>
 <HoteldeRating></HoteldeRating>
 <Descriptions>
   <HotelDescription>
     <Language></Language>
     <Text></Text>
     <ContentType></ContentType>
       </HotelDescription>
 </Descriptions>
</Property>
</HoteldeProperties>

Reply to this email directly or view it on GitHub:
#1 (comment)

Sorry about the strange formatting, I replied by email.