I would like to fetch specific data in JSON Data : every links in href in this markup <div id='gallery-1'
For example with my JSON Data :
<p><strong style=\"font-size: 13px;\">22nd March</strong></p>\n
<p>Swell is 3 foot and clean but wind swing south west later. Get on the early</p>\n
<p><span id=\"more-113\"></span></p>\n
<p>High tide: 1922 2.6m <span style=\"color: #ff0000;\"> <a href=\"http://www.bundoransurfco.com/webcam/\">
<strong>CLICK HERE FOR LIVE PEAK WEBCAM</strong></a></span></p>\n
<p>Low Tide: 1249 -0.1m</p>\n<p><b>3 day forecast to March 23rd</b></p>\n
<p>Looks like a fun few days with light winds and a long period swell.</p>\n\n\t\t
<style type='text/css'>\n\t\t\t#gallery-1 {\n\t\t\t\tmargin: auto;\n\t\t\t}\n\t\t\t
#gallery-1 .gallery-item {\n\t\t\t\tfloat: left;\n\t\t\t\tmargin-top: 10px;\n\t\t\t\t
text-align: center;\n\t\t\t\twidth: 50%;\n\t\t\t}\n\t\t\t#gallery-1 img {\n\t\t\t\t
border: 2px solid #cfcfcf;\n\t\t\t}\n\t\t\t
#gallery-1 .gallery-caption {\n\t\t\t\t
margin-left: 0;\n\t\t\t}\n\t\t\t
/* see gallery_shortcode() in wp-includes/media.php */\n\t\t</style>\n\t\t
<div id='gallery-1' class='gallery galleryid-113 gallery-columns-2 gallery-size-medium'>
<dl class='gallery-item'>\n\t\t\t<dt class='gallery-icon portrait'>\n\t\t\t\t
<a rel=\"prettyPhoto[gallery-113]\" href='http://www.bundoransurfco.com/wp-content/uploads/2014/11/10411096_10152611456607000_886839954460588268_n.jpg'>
<img width=\"225\" height=\"300\" src=\"http://www.bundoransurfco.com/wp-content/uploads/2014/11/10411096_10152611456607000_886839954460588268_n-225x300.jpg\"
class=\"attachment-medium colorbox-113 \" alt=\"10411096_10152611456607000_886839954460588268_n\" /></a>\n\t\t\t
</dt></dl>\n\t\t\t
<br style='clear: both' />\n\t\t</div>\n\n
<p> </p>\n
<h1> Wind Charts</h1>\n<p><a href=\"http://www.windguru.cz/int/index.php?sc=103244\">
<img class=\"size-thumbnail wp-image-747 alignleft\" title=\"wind guru\" src=\"http://www.bundoransurfco.com/wp-content/uploads/2010/12/wind-guru-67x68.jpg\" alt=\"\" width=\"67\" height=\"68\" /></a> <img class=\"alignnone size-thumbnail wp-image-749\" title=\"xcweathersmall\" src=\"http://www.bundoransurfco.com/wp-content/uploads/2010/12/xcweathersmall2-67x68.jpg\" alt=\"\" width=\"67\" height=\"68\" /> <img class=\"alignnone size-thumbnail wp-image-750\" title=\"buoy weather\" src=\"http://www.bundoransurfco.com/wp-content/uploads/2010/12/buoy-weather-67x68.jpg\" alt=\"\" width=\"67\" height=\"68\" /> Wind Guru XC Weather Buoy Weather</p>\n<h1>Swell Charts</h1>\n<p><img class=\"alignnone size-thumbnail wp-image-753\" title=\"msw logo\" src=\"http://www.bundoransurfco.com/wp-content/uploads/2010/12/msw-logo-67x43.jpg\" alt=\"\" width=\"75\" height=\"43\" /> <img class=\"alignnone size-thumbnail wp-image-754\" title=\"magicseaweedwamchart\" src=\"http://www.bundoransurfco.com/wp-content/uploads/2010/12/magicseaweedwamchart1-67x68.png\" alt=\"\" width=\"67\" height=\"68\" /> <img class=\"alignnone wp-image-755 size-thumbnail\" title=\"marine institute irish bouy data\" src=\"http://www.bundoransurfco.com/wp-content/uploads/2010/12/marine-institute-irish-bouy-data-67x42.jpg\" alt=\"\" width=\"67\" height=\"42\" /> Magic Seaweed MSM WAM Marine Institute</p>\n<h1>Pressure, Weather, Tides</h1>\n<p><img class=\"alignnone size-thumbnail wp-image-756\" title=\"bbc pressure\" src=\"http://www.bundoransurfco.com/wp-content/uploads/2010/12/bbc-pressure-67x68.jpg\" alt=\"\" width=\"67\" height=\"68\" /> <img class=\"alignnone size-thumbnail wp-image-759\" title=\"met eireann\" src=\"http://www.bundoransurfco.com/wp-content/uploads/2010/12/met-eireann-67x68.jpg\" alt=\"\" width=\"67\" height=\"68\" /> BBC Pressure Met Eireann Irish Tide Tables</p>\n
Fetch only : http://www.bundoransurfco.com/wp-content/uploads/2014/11/10411096_10152611456607000_886839954460588268_n.jpg
I used <a.+?href=\"([^\"]+) before for fetching every href in <a> markup, but that's not what I want ...
Assuming your div is a string and there is only one href, you can use this code instead of a regex to get the start and stop locations of the href.
NSRange range = [divString rangeOfString:#"href"]; // start
[divString rangeOfString:#">" options:0 range:NSMakeRange(range.location, 100)]; // end (if your href is long you can replace 100 with something greater)
Then use divString substringWithRange: to get the portion you are interested in
Here's the solution based on answer of Alex. Works for multiple hrefs in single string:
NSString *target = #"<div id='gallery-1' class='gallery galleryid-113 gallery-columns-2 gallery-size-medium'><dl class='gallery-item'>\n\t\t\t<dt class='gallery-icon portrait'>\n\t\t\t\t<a rel=\"prettyPhoto[gallery-113]\" href='http://www.bundoransurfco.com/wp-content/uploads/2014/11/10411096_10152611456607000_886839954460588268_n.jpg'><img width=\"225\" height=\"300\" href='http://www.bundoransurfco.com/wp-content/uploads/2014/11/10411096_10152611456607000_886839954460588268_n-225x300.jpg' class=\"attachment-medium colorbox-113 \" alt=\"10411096_10152611456607000_886839954460588268_n\" /></a>\n\t\t\t</dt></dl>\n\t\t\t<br style='clear: both' />\n\t\t</div>";
NSMutableArray *hrefs = [NSMutableArray array];
NSRange hrefRange = NSMakeRange(0, 0);
while (hrefRange.location != NSNotFound){
hrefRange = [target rangeOfString:#"href='"
options:0
range:NSMakeRange(hrefRange.location, target.length - (hrefRange.location + hrefRange.length))];
if (hrefRange.location == NSNotFound) {
NSLog(#"Thats all");
continue;
}
NSRange endRange = [target rangeOfString:#"'"
options:0
range:NSMakeRange(hrefRange.location + hrefRange.length, target.length - (hrefRange.location + hrefRange.length))];
NSString *href = [target substringWithRange:NSMakeRange((hrefRange.location+hrefRange.length), endRange.location - (hrefRange.location + hrefRange.length))];
[hrefs addObject:href];
hrefRange.location = hrefRange.location+hrefRange.length;
}
This implementation, as you can see, is sensitive to quotes (single- or double-quoted href value).
P.S. May look kinda messy, its fast-coded and tested.
EDIT:
Here's also a variant with regular expression, but works only with a tag, and also be careful with quotes:
NSError *error;
NSString *target = #"<div id='gallery-1' class='gallery galleryid-113 gallery-columns-2 gallery-size-medium'><dl class='gallery-item'>\n\t\t\t<dt class='gallery-icon portrait'>\n\t\t\t\t<a rel=\"prettyPhoto[gallery-113]\" href=\"http://www.bundoransurfco.com/wp-content/uploads/2014/11/10411096_10152611456607000_886839954460588268_n.jpg\"><img width=\"225\" height=\"300\" href=\"http://www.bundoransurfco.com/wp-content/uploads/2014/11/10411096_10152611456607000_886839954460588268_n-225x300.jpg\" class=\"attachment-medium colorbox-113 \" alt=\"10411096_10152611456607000_886839954460588268_n\" /></a>\n\t\t\t</dt></dl>\n\t\t\t<br style='clear: both' />\n\t\t</div>";
NSRegularExpression *regEx = [NSRegularExpression regularExpressionWithPattern:#"<[a|img]\\s+(?:[^>]*?\\s+)?href=\"([^\"]*)\""
options:0
error:&error];
NSArray *array = [regEx matchesInString:target
options:0
range:NSMakeRange(0, target.length)];
for (NSTextCheckingResult *match in array){
NSRange range = [match rangeAtIndex:1];
NSString *result = [target substringWithRange:range];
NSLog(#"HREF = %#", result);
}
I also edited first variant to save all hrefs into array.
Related
Hello i'm trying to extract hex colors from a html page converted to a string.
Given the following string:
"<div style="color: #00A9F8"></div><div style="color: #00000"></div>"
How do you convert that into an array of hex codes like this using ruby?:
["#00A9F8", #00000]
I've tried to use regex ^#(?:[0-9a-fA-F]{3}){1,2}$ but couldn't figure it out.
Any ideas?
Your string contains only one Hex code ("#00A9F8"). Other one has only 5 characters after #.
Anyways, you can fetch the required array with the help of scan:
str = '<div style="color: #00A9F8"></div><div style="color: #000000"></div>'
regex = /#[A-Z0-9]{6}/
str.scan(regex)
=> ["#00A9F8", "#000000"]
Your regex is anchored. ^ matches the start of a line and $ matches the end of a line, so your regex is only matching a string where the hex code is on a line by itself:
regex = /^#(?:[0-9a-fA-F]{3}){1,2}$/
snippet = %{
<div style="color: #00A9F8"></div><div style="color: #000000"></div>
<div style="color: \n#00A9F8\n"></div><div style="color: \n#000000\n"></div>
}
p snippet.scan(regex) # => ["#00A9F8", "#000000"] (i.e., only the last 2)
So, all you want need to change is remove the anchor, so it can match anywhere in the string:
regex = /#(?:[0-9a-fA-F]{3}){1,2}/
p snippet.scan(regex) # => ["#00A9F8", "#000000", "#00A9F8", "#000000"]
I need to iterate two property in build.xml
<target name="sample">
<property name="modules" value="" />
<property name="env" value="" />
</target>
Can any one help me to write looping concept I need to iterate two property at same time. for example ( property " modules " having list of values like = " a, b ,c d )( property " env " having list of values like = x, y ,z . )
I need value to get = modules.env .. it will gives a.x or b.y in iteration loop. So can any one help how to loop at the same time?
You can always try javascript for non-trivial tasks
<project name="proj">
<property name="modules" value="a,b,c,d" />
<property name="env" value="x,y,z,w" />
<script language="javascript"> <![CDATA[
var modules = proj.getProperty("modules").split(",");
var env = proj.getProperty("env").split(",");
var size = Math.min(modules.length, env.length);
for(var i = 0; i < size; ++i) {
proj.setProperty("mp." + i, modules[i] + "." + env[i]);
}
]]></script>
<echo message="${mp.0}" />
<echo message="${mp.1}" />
<echo message="${mp.2}" />
<echo message="${mp.3}" />
</project>
I an trying to make a flow document and print with, I was able to adjust the data to required size, and I am getting the required output.
Below is the code for my Flow Document:
<Window x:Class="test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="test" Height="600" Width="500">
<Grid>
<FlowDocumentReader Width="330" Height="110" Name="DocumentRdr">
<FlowDocument FontSize="8" Name="Document" >
<Paragraph Margin="0">
<TextBlock Text="Brand:"/>
<Run Text="{Binding Brand}" />
</Paragraph>
<Paragraph Margin="0">
<TextBlock Text="Item:"/>
<Run Text="{Binding Cat1}" />
<TextBlock Text="Size:"/>
<Run Text="{Binding Size}" />
</Paragraph>
<Paragraph Margin="0">
Welcome
<Run Text="{Binding Brand}" />
</Paragraph>
<BlockUIContainer Margin="0">
<Image Source="{Binding BarCode}" Width="Auto" Height="Auto" Stretch="None" HorizontalAlignment="Left" />
</BlockUIContainer>
</FlowDocument>
</FlowDocumentReader>
</Grid>
</Window>
and the code I use for printing is as follows:
Dim data As New SampleData With {.Brand = "Some Brand", .Cat1 = "A Cat 1", .Size = "100-120"}
Dim k As Zen.Barcode.BarcodeDraw = Zen.Barcode.BarcodeDrawFactory.Code25InterleavedWithoutChecksum
Dim ms As New MemoryStream
k.Draw("1234", 25).Save(ms, System.Drawing.Imaging.ImageFormat.Png)
ms.Position = 0
Dim bi As New BitmapImage
bi.BeginInit()
bi.StreamSource = ms
bi.EndInit()
data.BarCode = bi
Dim temp As New test
temp.DataContext = data
Dim doc = temp.Document
doc.PageHeight = 110
Dim pd = New PrintDialog()
Dim dps As IDocumentPaginatorSource = doc
dps.DocumentPaginator.PageSize = New Windows.Size(100, 100)
If pd.ShowDialog() = True Then
dps.DocumentPaginator.PageSize = New Windows.Size(330, 110)
pd.PrintDocument(dps.DocumentPaginator, "Document")
End If
The Problem is, the text and image every thing comes in the size I want, but I am not able to change the size of the paper. I am trying to print labels, due to long page I am getting a print for every 10-12 labels, I want to change the paper size.
This print dialog is part of system.windows.control and not system.drawings.printing. I tied to change the code by keeping required size in every place in code where there is size, but not able to do. Could you please correct me, where I went wrong.
Tried the below code too:
pd.PrintQueue.DefaultPrintTicket.PageMediaSize = New System.Printing.PageMediaSize(10, 10)
I have the following data
<Table>
<Row>
<Cell><Data>Row Labels</Data></Cell>
<Cell><Data>Searches</Data></Cell>
<Cell><Data>Purchases</Data></Cell>
<Cell><Data>CRatio</Data></Cell>
<Cell><Data>AvgReturn</Data></Cell>
<Cell><Data>Commission</Data></Cell>
</Row>
<Row>
<Cell><Data>2013</Data></Cell>
<Cell><Data>2257224</Data></Cell>
<Cell><Data>18574</Data></Cell>
<Cell><Data>8.2286915255198427E-3</Data></Cell>
<Cell><Data>43.459007223015384</Data></Cell>
<Cell><Data>102390.82995242</Data></Cell>
</Row>
<Row>
<Cell><Data>2014</Data></Cell>
<Cell><Data>5351610</Data></Cell>
<Cell><Data>43035</Data></Cell>
<Cell><Data>8.0415052666393851E-3</Data></Cell>
<Cell><Data>40.420168915503282</Data></Cell>
<Cell><Data>280677.31032664998</Data></Cell>
</Row>
<Row>
<Cell><Data>Grand Total</Data></Cell>
<Cell><Data>7608834</Data></Cell>
<Cell><Data>61609</Data></Cell>
<Cell><Data>8.0970356299007173E-3</Data></Cell>
<Cell><Data>41.336323742293686</Data></Cell>
<Cell><Data>383068.14027907007</Data></Cell>
</Row>
Whats the best way using XPath to get one of data at a time of data and then to get at indiviual elements in that
I have tried the following ...
enter NSArray *rowNodes = [doc nodesForXPath:#"/Table/Row" error:&error];
BOOL doBreak = NO;
#try {
int iRow = 0;
for (GDataXMLElement *rowNode in rowNodes) {
iRow++;
//get cell elements of the row
NSArray* rowCellNodes = [rowNode nodesForXPath:#"//Cell/Data" error:&error];
However when I do this I get the rowNodes correctly, but then I get no results for rowCellNodes in the for loop.
Try to add a dot at the beginning of the 2nd XPath to make it recognized as relative XPath to current rowNode :
NSArray* rowCellNodes = [rowNode nodesForXPath:#".//Cell/Data" error:&error];
I have an xml file which i create myself.
<?xml version='1.0' encoding='utf-8' ?>
<Root>
<ChildElements>
<type name='xx' attr='false'>
<child id='1' regex='^[0-9]{11}$'>
<BBB>
<node1 name='aaa' regex='\w{5}'/>
<node2 name='bbb' regex='\w{3}'/>
</BBB>
</child>
<child>
.
.
.
</child>
</type>
</ChildElements>
</Root>
Something like this. I want to parse it to xml document object and iterate through its nodes, I'm using libxml2 for ios.
NSString *xmlPath = [[NSBundle mainBundle] pathForResource:#"xmlcontent" ofType:#"xml"];
NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
NSString* xml = [[NSString alloc ] initWithData:xmlData encoding:NSUTF8StringEncoding];
int size=[xml lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
xmlDocPtr docptr=xmlParseMemory([xml UTF8String], size);
but in docptr object there is only one node, which is Root node, and when I try to get Root node child nodes it returns an empty node which name is #"text".
NSXMLParser easily parses the same XML, but I don't know how to create a document and iterate through nodes by using NSXMLParser.
maybe the error is, that your XML isn't valide:
<?xml version='1.0' encoding='utf-8' ?>
<Root>
<ChildElements>
<type name='xx' attr='false' />
<child id='1' regex='^[0-9]{11}$'>
<BBB>
<node1 name='aaa' regex='w{5}' />
<node2 name='bbb' regex='w{3}' />
</BBB>
</child>
</ChildElements>
</Root>
you can validate your XML with an online XML Validator