时间紧,来不及研究了。帮我看看怎么取XMLDocument的值。 ( 积分: 50 )

  • 主题发起人 主题发起人 mfj
  • 开始时间 开始时间
M

mfj

Unregistered / Unconfirmed
GUEST, unregistred user!
<Reference>
<Tags>
<Tag name="A">
<Attributes>
<Attribute name="accesskey" type="1"><description><![CDATA[Sets or retrieves the accelerator key for the <A>.]]></description></Attribute>
<Attribute name="class" type="1"><description><![CDATA[Sets or retrieves the class of the <A>.]]></description></Attribute>
<Events>
<Event name="onbeforecopy"><description><![CDATA[onbeforecopy]]></description></Event>
<Event name="onbeforecut"><description><![CDATA[onbeforecut]]></description></Event>
</Tag>

<Tag name="ACRONYM">
<Attributes>
<Attribute name="accesskey" type="1"><description><![CDATA[Sets or retrieves the accelerator key for the <ACRONYM>.]]></description></Attribute>
<Attribute name="class" type="1"><description><![CDATA[Sets or retrieves the class of the <ACRONYM>.]]></description></Attribute>
<Event name="onkeydown"><description><![CDATA[onkeydown]]></description></Event>
<Event name="onkeypress"><description><![CDATA[onkeypress]]></description></Event>
</Tag>
</ Tags >
< /Reference >
大家帮我看看怎么取去Tag name和Attribute name的值.
比如"A""ACRONYM"和"accesskey""class""onbeforecopy""onbeforecut"
 
哦,说得不大明白.再说下.我是用XMLDocument来读取XML文件.
 
下班了,明天再来.
 
// 看看结果吧
procedure TfrmM.Button1Click(Sender: TObject);

function GetAttr(Node :IXMLNode):string;
var
i :integer;
begin
Result := '';
for i:=0 to Node.AttributeNodes.Count -1 do
Result := Result + Format('%s = %s,',
[Node.AttributeNodes.NodeName,
Node.AttributeNodes.NodeValue]);
if Result <>'' then
Result := '('+Copy(Result,1,Length(Result)-1)+')';
end;

function GetNodeText(xmlNodeList :IXMLNodeList;Level :integer=0):string;
var
i :integer;
s :string;
begin
Result := '';
for i:=0 to xmlNodeList.Count- 1 do
begin
//bHasname := xmlNodeList.Nodes.HasAttribute('name');

if xmlNodeList.Nodes.NodeType in [ntText,ntCData] then
s := '[' + xmlNodeList.Nodes.NodeValue + ']'
else
s := '';
mAttachment.Lines.Add(
Format('%d%s %s,%s, %d %s %s',[Level,
StringOfChar(' ',Level*2),
TypInfo.GetEnumName(TypeInfo(TNodeType),
integer(xmlNodeList.Nodes.NodeType)), // 节点类型
xmlNodeList.Nodes.NodeName, // 节点名
xmlNodeList.Nodes.ChildNodes.Count, // 子节点数量
//BoolToStr(bHasname,True),
//IfThen(bHasname,'name = '+ VarToStr(xmlNodeList.Nodes.Attributes['name'])),
ifThen(xmlNodeList.Nodes.AttributeNodes.Count>0,
GetAttr(xmlNodeList.Nodes)), // 属性值
s ] // 值
));
if xmlNodeList.Nodes.ChildNodes.Count>0 then
GetNodeText(xmlNodeList.Nodes.ChildNodes,Level +1);
end; // for..end
end;

var
xml : IXMLDocument;
s :string;
begin
if Sender = Button1 then
s := '<Reference><Tags>' +
'<Tag name="A">' +
'<Attributes>' +
//
'<Attribute name="accesskey" type="1">' +
'<description><![CDATA[Sets or retrieves the accelerator key for the <A>.]]></description>'+
'</Attribute>' +
'<Attribute name="class" type="1">' + #13#10 +
'<description><![CDATA[Sets or retrieves the class of the <A>.]]></description>' + #13#10 +
'</Attribute>' +
// '<Events>' + #13#10 +
'<Event name="onbeforecopy"><description><![CDATA[onbeforecopy]]></description></Event>' + #13#10 +
'<Event name="onbeforecut"><description><![CDATA[onbeforecut]]></description></Event>' + #13#10 +
// '</Events>' +
'</Attributes>'+
'<myText>This my Text.'+
'</myText>'+
'</Tag>' +
'</Tags></Reference>'
else if Sender = Button2 then
s := mBody.Text; // TMemo

// uses TypInfo, XMLDoc, XMLIntf, StrUtils;
mBody.Text := FormatXMLData(s); // 格式化,对齐

xml := TXMLDocument.Create(nil); //LoadXMLData(s);
xml.LoadFromXML(s);
GetNodeText(xml.ChildNodes);
xml := nil;
end;
 
anode := xml.ChildNodes.FindNode('...')
str := anode.ChildValues['...']
这两句就可以了
 
谢谢楼上回答的二位,我现在已经做出来了.方法都差不多.不过分还是要给的.
 

Similar threads

后退
顶部