猪
猪头哥哥
Unregistered / Unconfirmed
GUEST, unregistred user!
XML原文
<?xml version="1.0" encoding="gb2312" ?>
- <CRS.CommandSet.PNR>
<Success />
<Errors />
<PNR>TBGYF</PNR>
<AgentOffice />
- <Passengers>
<Passenger ElementNo="1" PsgID="1" Name="CHEN/SHUNHAO" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
<Passenger ElementNo="2" PsgID="2" Name="LAN/XIAOFANG" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
<Passenger ElementNo="3" PsgID="3" Name="TADASHI/NISHIKAWA" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
<Passenger ElementNo="4" PsgID="4" Name="WU/SIPING" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
</Passengers>
- <Flights>
<Flight ElementNo="5" ID="1" Type="0" Carrier="CZ" Flight="389" BoardPoint="CAN" OffPoint="KIX" Week="TH" DepartureDate="2007-04-05" DepartureTime="09:20" ArriveDate="2007-04-05" ArriveTime="13:50" Class="H" ActionCode="RR" Seats="4" Meal="" Stops="" Avail="" Night="" ETKT="" Changed="" LinkLevel="" Allow="" />
<Flight ElementNo="6" ID="2" Type="0" Carrier="CZ" Flight="390" BoardPoint="KIX" OffPoint="CAN" Week="SU" DepartureDate="2007-04-08" DepartureTime="14:50" ArriveDate="2007-04-08" ArriveTime="17:20" Class="H" ActionCode="RR" Seats="4" Meal="" Stops="" Avail="" Night="" ETKT="" Changed="" LinkLevel="" Allow="" />
</Flights>
+ <Contacts>
<Contacts No="CAN/T CAN/020-87013606/GUANGDONG NEW GENERATION BUSINESS MANAGEMENT CO.,LTD /WANGLIMING ABCDEFG" />
<Contacts No="34712064" />
</Contacts>
<PNRs />
<TicketNos />
<RMKs />
<SSRs />
<OSIs />
- <Segments>
<Segment ElementNo="10" Text="SSR OTHS 1E *PLS ADVISE PAX TO RCFM CZ FLT NO LATER THAN 72HRS BF" />
<Segment ElementNo="11" Text="SSR PSPT" />
<Segment ElementNo="12" Text="SSR PSPT" />
<Segment ElementNo="13" Text="SSR PSPT" />
<Segment ElementNo="14" Text="SSR PSPT" />
</Segments>
</CRS.CommandSet.PNR>
----------------------------------------------------------------
解析方法:
function getnodefromIXMLNodeList(childnodes: IXMLNodeList; nodename: string): IXMLNode;
var
i: Integer;
begin
for i := 1 to childnodes.Count do begin
if (childnodes.Get(i - 1).NodeName = nodename) then begin
result := childnodes[i - 1];
exit;
end;
end;
end;
{-------------------------------------------------------------------------------
Fun/Pro: GetXMLNodeSpecialValue
@Date: 2004.12.11
@Param: xmlFile xml文件
@Param: xmlnodepath 节点
@Param: xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
@Param: XMLSpecialName 要查找的节点中属性名
@Param: XMLSpecialValue 要查找的节点中某属性对应的值
@Param: dep 节点的参数的分隔符,默认为.
@Return: 某属性的值
-------------------------------------------------------------------------------}
function GetXMLNodeSpecialValue(strEntityEngineFile:String; XMLNodePath:String;
const XMLAttrName:String='; const XMLSpecialName:String='; const XMLSpecialValue:String='; const dep:Char ='.'):String;
var
xmlDocument :IXMLDocument;
node :IXMLNode;
xmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
begin
//xml节点路径
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:=dep;
xmlnodeList.DelimitedText:=xmlnodepath;
urlcount:=xmlnodeList.Count;
//xml对象
xmlDocument :=TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active:=true;
try
node:= xmlDocument.DocumentElement;
if(node.NodeName = xmlnodeList[0]) then begin
//扫描节点
for i := 1 to urlcount-1 do begin
if(node<>nil) then
begin
node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList);
end
else Break;
end;
if(node=nil)then begin
result:=';
end else begin
//判断是取属性还是取节点内容
if(Trim(xmlattrname)=') then
result:=node.Text
else
begin
result := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue; //这里不想再声明一个临时变量了,就用result来比较,可能有隐患。
while ((result <> XMLSpecialValue)) do
begin
node := node.NextSibling;
while (node.NodeName = '#comment') do
begin
node:= node.NextSibling;
end;
result := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;
end;
result:=node.AttributeNodes.Nodes[XMLAttrName].NodeValue;
end;
end;
end else begin
result:=';
end;
except
result:='error';
end;
xmlDocument.Active:=false;
end;
问题:
1:AttributeNodes 是什么作用?每次到这里都运行不下去.看了Delphi帮助E文看的不是很明白.
2:难点在于下面这段,不懂如何循环解析.
<Passenger ElementNo="1" PsgID="1" Name="CHEN/SHUNHAO" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
<Passenger ElementNo="2" PsgID="2" Name="LAN/XIAOFANG" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
<Passenger ElementNo="3" PsgID="3" Name="TADASHI/NISHIKAWA" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
<Passenger ElementNo="4" PsgID="4" Name="WU/SIPING" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
3:期待各位高手能给出调用例子.谢谢.
<?xml version="1.0" encoding="gb2312" ?>
- <CRS.CommandSet.PNR>
<Success />
<Errors />
<PNR>TBGYF</PNR>
<AgentOffice />
- <Passengers>
<Passenger ElementNo="1" PsgID="1" Name="CHEN/SHUNHAO" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
<Passenger ElementNo="2" PsgID="2" Name="LAN/XIAOFANG" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
<Passenger ElementNo="3" PsgID="3" Name="TADASHI/NISHIKAWA" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
<Passenger ElementNo="4" PsgID="4" Name="WU/SIPING" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
</Passengers>
- <Flights>
<Flight ElementNo="5" ID="1" Type="0" Carrier="CZ" Flight="389" BoardPoint="CAN" OffPoint="KIX" Week="TH" DepartureDate="2007-04-05" DepartureTime="09:20" ArriveDate="2007-04-05" ArriveTime="13:50" Class="H" ActionCode="RR" Seats="4" Meal="" Stops="" Avail="" Night="" ETKT="" Changed="" LinkLevel="" Allow="" />
<Flight ElementNo="6" ID="2" Type="0" Carrier="CZ" Flight="390" BoardPoint="KIX" OffPoint="CAN" Week="SU" DepartureDate="2007-04-08" DepartureTime="14:50" ArriveDate="2007-04-08" ArriveTime="17:20" Class="H" ActionCode="RR" Seats="4" Meal="" Stops="" Avail="" Night="" ETKT="" Changed="" LinkLevel="" Allow="" />
</Flights>
+ <Contacts>
<Contacts No="CAN/T CAN/020-87013606/GUANGDONG NEW GENERATION BUSINESS MANAGEMENT CO.,LTD /WANGLIMING ABCDEFG" />
<Contacts No="34712064" />
</Contacts>
<PNRs />
<TicketNos />
<RMKs />
<SSRs />
<OSIs />
- <Segments>
<Segment ElementNo="10" Text="SSR OTHS 1E *PLS ADVISE PAX TO RCFM CZ FLT NO LATER THAN 72HRS BF" />
<Segment ElementNo="11" Text="SSR PSPT" />
<Segment ElementNo="12" Text="SSR PSPT" />
<Segment ElementNo="13" Text="SSR PSPT" />
<Segment ElementNo="14" Text="SSR PSPT" />
</Segments>
</CRS.CommandSet.PNR>
----------------------------------------------------------------
解析方法:
function getnodefromIXMLNodeList(childnodes: IXMLNodeList; nodename: string): IXMLNode;
var
i: Integer;
begin
for i := 1 to childnodes.Count do begin
if (childnodes.Get(i - 1).NodeName = nodename) then begin
result := childnodes[i - 1];
exit;
end;
end;
end;
{-------------------------------------------------------------------------------
Fun/Pro: GetXMLNodeSpecialValue
@Date: 2004.12.11
@Param: xmlFile xml文件
@Param: xmlnodepath 节点
@Param: xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
@Param: XMLSpecialName 要查找的节点中属性名
@Param: XMLSpecialValue 要查找的节点中某属性对应的值
@Param: dep 节点的参数的分隔符,默认为.
@Return: 某属性的值
-------------------------------------------------------------------------------}
function GetXMLNodeSpecialValue(strEntityEngineFile:String; XMLNodePath:String;
const XMLAttrName:String='; const XMLSpecialName:String='; const XMLSpecialValue:String='; const dep:Char ='.'):String;
var
xmlDocument :IXMLDocument;
node :IXMLNode;
xmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
begin
//xml节点路径
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:=dep;
xmlnodeList.DelimitedText:=xmlnodepath;
urlcount:=xmlnodeList.Count;
//xml对象
xmlDocument :=TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active:=true;
try
node:= xmlDocument.DocumentElement;
if(node.NodeName = xmlnodeList[0]) then begin
//扫描节点
for i := 1 to urlcount-1 do begin
if(node<>nil) then
begin
node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList);
end
else Break;
end;
if(node=nil)then begin
result:=';
end else begin
//判断是取属性还是取节点内容
if(Trim(xmlattrname)=') then
result:=node.Text
else
begin
result := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue; //这里不想再声明一个临时变量了,就用result来比较,可能有隐患。
while ((result <> XMLSpecialValue)) do
begin
node := node.NextSibling;
while (node.NodeName = '#comment') do
begin
node:= node.NextSibling;
end;
result := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;
end;
result:=node.AttributeNodes.Nodes[XMLAttrName].NodeValue;
end;
end;
end else begin
result:=';
end;
except
result:='error';
end;
xmlDocument.Active:=false;
end;
问题:
1:AttributeNodes 是什么作用?每次到这里都运行不下去.看了Delphi帮助E文看的不是很明白.
2:难点在于下面这段,不懂如何循环解析.
<Passenger ElementNo="1" PsgID="1" Name="CHEN/SHUNHAO" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
<Passenger ElementNo="2" PsgID="2" Name="LAN/XIAOFANG" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
<Passenger ElementNo="3" PsgID="3" Name="TADASHI/NISHIKAWA" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
<Passenger ElementNo="4" PsgID="4" Name="WU/SIPING" Type="0" CardType="" CardNo="" BirthDay="" CarrierPsgID="" Country="" />
3:期待各位高手能给出调用例子.谢谢.