如何从 xml 字符串中提取特定值?
How to extract specific value from xml string?
我想在 <P>
标签下提取第一个和第二个句子。
例如(输入字符串):
<P align=justify><STRONG>Pricings<BR></STRONG>It was another active week for names leaving the database. The week's prints consisted of two ILS, and sever ITS.</P>
需要的输出字符串:
It was another active week for names leaving the database. The week's prints consisted of two ILS, and sever ITS.
目前,我下面的函数抛出以下错误:
System.Xml.XmlException: 'justify' is an unexpected token. The expected token is '"' or ''
price = bottom.Substring(bottom.IndexOf("Pricings"), 8);
XmlDocument doc = new XmlDocument();
doc.LoadXml(bottom);
XmlNodeList pList = doc.SelectNodes("/P[@align='justify']/strong");
foreach (XmlNode pValue in pList)
{
string innerText = pValue.ChildNodes[0].InnerText;
innerText = result;
}
我不太清楚如何解决这个问题。感谢您提供任何进一步的帮助。
它不是XML字符串,而是HTML一个。
由于 HTML 本身通常可能格式不正确(在您的情况下它格式不正确),通常您不能使用 XML 解析器来解析 HTML.
相反,您可以使用 HTML Agility Pack(推荐方式),或使用正则表达式解析此文本(通常不推荐,但有时可能)。
以下是如何使用 HtmlAgility 包获取数据的示例代码:
var s = "<P align=justify><STRONG>Pricings<BR></STRONG>It was another active week for names leaving the database. The week's prints consisted of two ILS, and sever ITS.</P>";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(s);
string result;
var p = doc.DocumentNode.SelectSingleNode("p");
if (p.ChildNodes.Count == 2)
result = p.ChildNodes[1].InnerText;
注意:Html敏捷包也可作为 NuGet 包在 Visual Studio 中提供。
我只是在php/magento中做,试试这个来解决。
$xml = simplexml_load_file("../app/etc/local.xml") or die("X");$host = $xml->xpath('global/resources/default_setup/connection/host');$host = $host[0][0];$usernm = $xml->xpath('global/resources/default_setup/connection/username');$usernm = $usernm[0][0];$pwd = $xml->xpath('global/resources/default_setup/connection/password');$pwd = $pwd[0][0];$db = $xml->xpath('global/resources/default_setup/connection/dbname');$db = $db[0][0];$link = mysql_connect($host, $usernm, $pwd);
If (!$link) { die ('Could not connect: ' . mysql_error()); }
mysql_select_db($db) or die ('Unable to select database');
$result = mysql_query("SELECT * FROM catalog_product_flat_1 Where shipping_price IS NULL AND type_id='simple'");
$noOfRecord = mysql_num_rows($result);
我使用 xml 文件作为位于 magento/app/etc/local.xml..
的 magento local.xml 文件
我想在 <P>
标签下提取第一个和第二个句子。
例如(输入字符串):
<P align=justify><STRONG>Pricings<BR></STRONG>It was another active week for names leaving the database. The week's prints consisted of two ILS, and sever ITS.</P>
需要的输出字符串:
It was another active week for names leaving the database. The week's prints consisted of two ILS, and sever ITS.
目前,我下面的函数抛出以下错误:
System.Xml.XmlException: 'justify' is an unexpected token. The expected token is '"' or ''
price = bottom.Substring(bottom.IndexOf("Pricings"), 8);
XmlDocument doc = new XmlDocument();
doc.LoadXml(bottom);
XmlNodeList pList = doc.SelectNodes("/P[@align='justify']/strong");
foreach (XmlNode pValue in pList)
{
string innerText = pValue.ChildNodes[0].InnerText;
innerText = result;
}
我不太清楚如何解决这个问题。感谢您提供任何进一步的帮助。
它不是XML字符串,而是HTML一个。
由于 HTML 本身通常可能格式不正确(在您的情况下它格式不正确),通常您不能使用 XML 解析器来解析 HTML.
相反,您可以使用 HTML Agility Pack(推荐方式),或使用正则表达式解析此文本(通常不推荐,但有时可能)。
以下是如何使用 HtmlAgility 包获取数据的示例代码:
var s = "<P align=justify><STRONG>Pricings<BR></STRONG>It was another active week for names leaving the database. The week's prints consisted of two ILS, and sever ITS.</P>";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(s);
string result;
var p = doc.DocumentNode.SelectSingleNode("p");
if (p.ChildNodes.Count == 2)
result = p.ChildNodes[1].InnerText;
注意:Html敏捷包也可作为 NuGet 包在 Visual Studio 中提供。
我只是在php/magento中做,试试这个来解决。
$xml = simplexml_load_file("../app/etc/local.xml") or die("X");$host = $xml->xpath('global/resources/default_setup/connection/host');$host = $host[0][0];$usernm = $xml->xpath('global/resources/default_setup/connection/username');$usernm = $usernm[0][0];$pwd = $xml->xpath('global/resources/default_setup/connection/password');$pwd = $pwd[0][0];$db = $xml->xpath('global/resources/default_setup/connection/dbname');$db = $db[0][0];$link = mysql_connect($host, $usernm, $pwd);
If (!$link) { die ('Could not connect: ' . mysql_error()); }
mysql_select_db($db) or die ('Unable to select database');
$result = mysql_query("SELECT * FROM catalog_product_flat_1 Where shipping_price IS NULL AND type_id='simple'");
$noOfRecord = mysql_num_rows($result);
我使用 xml 文件作为位于 magento/app/etc/local.xml..
的 magento local.xml 文件