这个XML"有效"吗?
<?xml version="1.0"?>
<p class="leaders">
Todd
<span class="leader-type">.</span>
R
<span class="leader-type">.</span>
Colas
</p>
Run Code Online (Sandbox Code Playgroud)
我从来没有见过像这样的节点有多个"值"的XML文档<p>
.
如何解析<p>
TXMLDocument 的三个值?以及如何遍历<span>
节点?
最后......如何使用TXMLDocument创建这样的XML文档????
救命!!!!
我使用XML绑定向导来创建TXMLDocument的后代.此类生成的文件将在根节点中声明命名空间,并为文档的其余部分创建简单,朴素的节点.
<?xml version="1.0"?>
<RootNode xmlns="URL" xmlns:xsi="URL" xsi:schemaLocation="URL">
<SomeNode>
<AnotherNode>Value</AnotherNode>
</SomeNode>
</RootNode>
Run Code Online (Sandbox Code Playgroud)
我完全没有阅读或验证这一点.但是,现在发送这些文件的处理器要求每个节点都有前缀的命名空间,以便正确处理文件.
<?xml version="1.0"?>
<NS:RootNode xmlns:NS="URL" xmlns:xsi="URL" xsi:schemaLocation="URL">
<NS:SomeNode>
<NS:AnotherNode>Value</NS:AnotherNode>
</NS:SomeNode>
</NS:RootNode>
Run Code Online (Sandbox Code Playgroud)
如何使用TXMLDocument后代完成此操作?我希望它不涉及手动编辑10000行生成的代码.
我一直在使用 IndyTIdTCPServer
对象并TXMLDocument
在TIdTCPServer.OnExecute
活动期间实例化对象实例。xml.Active
我发现当设置为 true时出现异常非常令人惊讶:
未安装 Microsoft MSXML
procedure TForm4.tcpRXExecute(AContext: TIdContext);
var
sResponseXML : string;
xml:IXMLDocument;
begin
// get message from client
sResponseXML := AContext.Connection.IOHandler.ReadLn;
xml:=TXMLDocument.Create(nil);
// error here: "Microsoft MSXML is not installed"
xml.Active:=true;
xml.Encoding:='UTF-8';
xml.LoadFromXML(sResponseXML);
// use the xml document
//AContext.Connection.IOHandler.WriteLn('... message sent from server :)');
end;
Run Code Online (Sandbox Code Playgroud)
更深入地观察,我发现发生异常的原因TMSXMLDOMDocumentFactory.TryCoCreateInstance()
是无法创建正确的文档对象实例,尽管GuidList
从主线程接收到了与应用程序其他部分中接收到的相同的实例。我不明白为什么如果从组件的线程调用该对象则不会实例化。
以下是应实例化对象的 Embarcadero 代码:
class function TMSXMLDOMDocumentFactory.TryCoCreateInstance(const GuidList: array of TGUID): IUnknown;
var
I: Integer;
Status: HResult;
begin
for I := …
Run Code Online (Sandbox Code Playgroud) 我尝试在 Delphi 中使用 TXMLDocument 来枚举 XML 文件。但是每当我退出 ProcessFile 过程时,就会出现“访问冲突”异常。
我将 XMLFile 声明为接口对象 IXMLDocument,而不是 TXMLDocument,并且不手动调用 free 它。
下面是代码:
// Invoke ProcessFile in the main app
ProcessFile('C:\Myfile.xlf', True);
// Process the node recursively
procedure ProcessNode1(Node: IXMLNode);
var
Index: Integer;
begin
if (Node.NodeName = 'trans-unit') then
begin
// Process 'trans-unit'
end
else
for Index := 0 to Node.ChildNodes.Count - 1 do
ProcessNode1(Node.ChildNodes[Index]);
end;
procedure ProcessFile(const SrcFileName: string; const FixMode: Boolean);
var
XmlFile: IXMLDocument;
MainNode, FileNode: IXMLNode;
OriginalFileName, SrcLang, DstLang: string;
begin
// Initialize …
Run Code Online (Sandbox Code Playgroud) 我试图从twitter atom/xml feed获取正确的数据.我在txmldocument中有twitter数据,并试图从中获取一些特定信息.
以下是数据的截断示例:
<entry>
<link type="text/html" rel="alternate" href="http://twitter.com/blub/statuses/1501068" />
<title>title of twitter post goes here</title>
<link type="image/png" rel="image" href="http://s3.amazonaws.com/twitter_production/profile_images/234870532/normal.jpg" />
</entry>
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我试图获取配置文件图像url(第二个链接标记的href属性).
如果我使用这样的代码:
i:=xmldocument1.DocumentElement.ChildNodes['entry'];
text:=(i.ChildNodes['link'].GetAttributeNS('href',''));
Run Code Online (Sandbox Code Playgroud)
我得到的是FIRST链接标签的href值,但我想要SECOND链接标签,我不知道该怎么做.有人有什么想法吗?
谢谢.
每次我尝试访问文档的根时,以下代码都会抛出"InvalidPointer"异常XMLDocument.DocumentElement;
.
begin
XMLDocument := TXMLDocument.Create(nil); // nil since we don't need an owner
AStream := TStream.Create; // stream for output as string
XMLDocument.loadFromXML(xml);// load string
if NOT (XMLDocument.IsEmptyDoc) then
begin
XMLDocument.Active := true; // actually automatically done by 'loadFromXML'
// get document root
HeadNode := XMLDocument.DocumentElement;
// add <id>-element, set ID as text
idNode := HeadNode.AddChild(XML_ID_PLAIN);
idNode.Text := id;
// ...
end;
end;
Run Code Online (Sandbox Code Playgroud)
传递给的字符串"xml" loadFromXML(string)
是有效的XML,但XMLDocument的属性"XML"和"DOMDocument"始终是nil
,即使对象本身和它的"IsEmptyDoc"属性都不是.德尔福版本仍然是2007年.
有谁知道是什么原因引起的?提前致谢.
这在Delphi XE2中对我不起作用.
Var
XMLDoc : IXMLDOCUMENT;
begin
XMLDoc := NewXMLDocument;
XMLDoc.Active := True;
XMLDoc.Version := '1.0';
XMLDoc.Encoding := 'utf-8';
XMLDoc.Options := [doNodeAutoIndent];
Memo1.Text := XMLDoc.XML.Text;
Run Code Online (Sandbox Code Playgroud)
我仍然没有得到encoding="utf-8"?>
生成的文档.但如果我说
XMLDoc.Encoding := 'utf-16';
Run Code Online (Sandbox Code Playgroud)
那么我确实得到encoding="utf-16"?>
了最终的文档.
有任何想法吗?任何人?
在FullDebugMode中使用带有FastMM4(版本4.992)的Delphi XE8会产生奇怪的效果.
要重现效果,只需要创建一个新的TForm的应用程序,把FastMM4在DPR文件的第一行,放一个按钮在窗体上,并把下面的代码在clickHandler:
(您需要安装FastMM 4,必须在FastMM4Options.inc文件中启用FullDebugMode,并且FullDebugMode.dll必须位于程序的输出文件夹中!)
procedure TForm3.Button4Click(Sender: TObject);
var
dm: tdatamodule;
doc: txmldocument;
begin
//this causes the messagebox to be shown directly after clicking the button.
//without it the box is shown when the program is exited.
FastMM4.FullDebugModeScanMemoryPoolBeforeEveryOperation := true;
//prepare a xml document
dm := tdatamodule.create(nil);
doc := txmldocument.create(dm);
doc.LoadFromXml('<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?><root/>');
//doc.Active := true; //no need to set active to true. doc is already active at this point!
//the following does matter. ChildNodes needs to be accessed …
Run Code Online (Sandbox Code Playgroud) 我使用Delphi 2010只有库存标准VCL库.目标系统是win32.
如果我有一个IXMLDocument的引用,我怎样才能获得IXMLDocument包装的底层对象的IXMLDOMDocument2接口?
我正在从XML文件中的节点读取浮点属性值TXMLDocument
:
<MyApp_Favorites version="1.0">
Run Code Online (Sandbox Code Playgroud)
......发表声明:
var
ThisRootNode: IXMLNode;
ThisVersion: Single;
// ...
ThisVersion := ThisRootNode.Attributes['version'];
CodeSite.Send('ThisVersion', ThisVersion);
Run Code Online (Sandbox Code Playgroud)
但是,在我的德语系统上,我得到了这个版本值:
ThisVersion = 10,00
...在我的区域设置中,逗号","被定义为小数分隔符,而不是点"." 就像在XML文件中一样.但是使用英语区域设置 - 其中点最可能被定义为小数分隔符设置 - 结果将正确为"1.0".
那么如何确保独立于区域设置,1.0的读取值将始终相同?(将版本值读取为字符串,然后将其转换为float似乎不是一个非常优雅的方法).
xml delphi txmldocument regional-settings delphi-10.1-berlin
我注意到我在使用 Indy Client 的应用程序中的 XML 文件有无效字符(我实际上使用了 IdHttp 的默认参数)
这是我的代码:
ts := TStringList.Create;
try
ts.Add('XML=' + AXMLDoc.XML.Text));
HTTPString := IdHTTPClient.Post('http://' + FHost + ':' + IntToStr(FPort) + FHttpRoot, ts);
finally
ts.Free;
end;
Run Code Online (Sandbox Code Playgroud)
我的 XML 文件是 UTF-8 编码的。
我必须做什么才能在我的服务器上获得良好的编码(我也使用 Indy 作为服务器)?
使用Delphi XE2 TXMLDocument和MSXML
<description>£1.00 x 100</description> parsing error as does
<description><![CDATA[£1.00 x 100]]></description> parsing error as does
Run Code Online (Sandbox Code Playgroud)
£字符未标记为无效字符.
这是一个问题吗?更重要的是建议的解决方法.
TIA
以法莲
delphi ×12
txmldocument ×12
xml ×7
delphi-2007 ×1
delphi-xe2 ×1
delphi-xe4 ×1
fastmm ×1
idhttp ×1
indy ×1
indy10 ×1
root ×1
win32com ×1