在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
XMLDocument; Xml.XMLIntf.hpp Xml.XMLDoc.hpp #include "Xml.Win.msxmldom.hpp" XMLDocument控件 Winapi.msxmlIntf.pas selectsinglenode只返回一个结点 xml cdata vNode:=doc.CreateNode('abc',ntCData,'xxxx');
DOM解析器的3种选择 MSXML MS Windows/Omni
解析器的选择 Xml.XMLDoc.TXMLDocument.DOMVendor http://docwiki.embarcadero.com/CodeExamples/Seattle/e/index.php?title=Category:C%2B%2B&pagefrom=SystemVarClear+%28C%2B%2B%29#mw-pages void CreateDocument() { _di_IXMLDocument document = interface_cast<Xmlintf::IXMLDocument> (new TXMLDocument(NULL)); document->Active = true; // Define document content. document->DocumentElement = document->CreateNode("ThisIsTheDocumentElement", ntElement, ""); document->DocumentElement->Attributes["attrName"] = "attrValue"; _di_IXMLNode nodeElement = document->DocumentElement->AddChild ("ThisElementHasText", -1); nodeElement->Text = "Inner text."; _di_IXMLNode nodeCData = document->CreateNode("any characters here", ntCData, ""); document->DocumentElement->ChildNodes->Add(nodeCData); _di_IXMLNode nodeText = document->CreateNode("This is a text node.", ntText, ""); document->DocumentElement->ChildNodes->Add(nodeText); document->SaveToFile(destPath); }
生成文件 http://overblue.blogbus.com/logs/13954596.html uses XMLDoc, XMLIntf; { 写入XML内容 } var XMLDoc : TXMLDocument; Node1 : IXMLNode; Node2 : IXMLNode; begin XMLDoc := TXMLDocument.Create(nil); try XMLDoc.Active := True; XMLDoc.Version := '1.0'; XMLDoc.Encoding := 'GB2312'; XMLDoc.Options := [doNodeAutoCreate,doNodeAutoIndent,doAttrNull,doAutoPrefix,doNamespaceDecl]; XMLDoc.DocumentElement := XMLDoc.CreateNode('ReportObjectContent'); Node1 := XMLDoc.DocumentElement; Node1 := Node1.AddChild('ReportObjectProperty'); Node2 := Node1.AddChild('ReportName'); Node2.SetAttributeNS('Value', '', ReportName); Node2 := Node1.AddChild('ReportType'); Node2.SetAttributeNS('Value', '', ReportType); Node2 := Node1.AddChild('DataViewName'); Node2.SetAttributeNS('Value', '', DataViewName); Node2 := Node1.AddChild('SQLStr'); Node2.SetAttributeNS('Value', '', SQLStr); XMLDoc.SaveToStream(Stream); finally XMLDoc.Free; end;
XMLDocument1.DOMDocument.childNodes['Config'].attributes['DataUpFlag']; xml->DocumentElement->AddChild("TableClassName")->Text = aClassName; if (node->ChildNodes->Get(i)->NodeType() == ntElement) 读解析文件 LoadFromFile LoadXMLData 读XML字符串 DocumentElement _di_IXMLNode snode = XMLDocument1->DocumentElement->ChildNodes->Nodes("row");
XMLDocument1->LoadFromFile(lxml); _di_IXMLNode snode = XMLDocument1->DocumentElement->ChildNodes->FindNode(aTableName); if (snode) { for (int i = 0; i < snode->ChildNodes->Count; i++) { _di_IXMLNode rownode = snode->ChildNodes->Get(i); String fn = rownode->Attributes["Field"]; } } XMLDocument1->Active = false;
增加或修改节点 xml->CreateElement xml->DocumentElement->AddChild _di_IXMLNode nodeElement = xml->DocumentElement->ChildNodes->FindNode("DAOPath"); if (nodeElement == NULL) nodeElement = xml->DocumentElement->AddChild("DAOPath"); nodeElement->Text = DaoPath;
缩进,格式化 XMLDocument1->Options = XMLDocument1->Options << doNodeAutoIndent; <?xml version="1.0" encoding="utf-8"?> <data> <config> <recent>aaa</recent> </config> <list> <row name="aa" port=""></row> <row name="bb" port=""></row> </list> </data>
读取recent的值 fangA = XMLDocument1->DocumentElement->ChildNodes->FindNode("config")->ChildNodes->FindNode("recent")->NodeValue; XMLDocument1->DocumentElement->ChildNodes->FindNode("config")->ChildNodes->FindNode("recent")->NodeValue="newstring";
Returns the interface for an XML document given the name of an XML file. Call LoadXMLDocument to load an XML document from a file on disk and obtain its interface. LoadXMLDocument creates a new TXMLDocument object and uses it to parse the file specified by the FileName parameter. The TXMLDocument component is not given an owner, which means that it behaves like an interfaced object. When the application releases the interface that LoadXMLDocument returns, the TXMLDocument instance is automatically freed.
selectSingleNode
procedure TForm4.Parxml( Sender : TObject ); var noderef : IXMLDOMNodeRef; root : IXMLDOMNode; Node : IXMLDOMNode; doc : IXMLDocument; begin doc := LoadXMLDocument( 'test.xml' ); noderef := doc.DocumentElement.DOMNode as IXMLDOMNodeRef; root := noderef.GetXMLDOMNode; // 获取跟节点 Node := root.selectSingleNode( 'ItemList/Item[@name="name2"]' ); // 根据路径的属性值获取节点 ShowMessage( Node.Attributes.getnameditem( 'title' ).Text ); // title2 Node.Attributes.getnameditem( 'title' ).Text := 'hello'; // <Item name="name2" title="title2"/> => <Item name="name2" title="hello"/> end; test.xml文档 : < ? Xml version = " 1.0 " encoding = " utf - 8 " ? > < test >< ItemList > < Item name = " name1 " title = " title1 " > < / Item > < Item name = " name2 " title = " title2 " / > < / ItemList > < / test > <?xml version ="1.0" encoding ="utf-8"?>
ptNodes: IXMLDOMNode; pas:= ptnodes.text;//读取节点值要用text,nodeValue取不到
解析C++builder工程文件 原始工程文件首行是<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 改成<Project>才能用selectSingleNode方法解析。 procedure TForm2.Button1Click(Sender: TObject); var path: string; Xml: IXMLDocument; noderef: IXMLDOMNodeRef; Root: IXMLDOMNode; Node: IXMLDOMNode; ConditionStr: string; list: TStringList; begin list := TStringList.Create; list.LoadFromFile('E:\test\tt.cbproj'); list.Strings[0] := '<Project>'; Xml := LoadXMLData(list.Text); noderef := Xml.DocumentElement.DOMNode as IXMLDOMNodeRef; Root := noderef.GetXMLDOMNode; path := 'PropertyGroup[@Condition="''$(Cfg_1_Win32)''!=''''"]/FinalOutputDir'; Node := Root.selectSingleNode(path); if not Assigned(Node) then self.Caption := 'not not ' else Caption := Node.nodeTypedValue; list.Free; end;
C++builder Winapi.msxmlIntf.hpp Xml.Win.msxmldom.hpp http://docwiki.embarcadero.com/Libraries/Berlin/en/Xml.Win.msxmldom.IXMLDOMNodeRef.GetXMLDOMNode _di_IXMLDOMDocument xdocm; _di_IXMLDOMNodeRef xdnodeRef;
noderef := xml.DocumentElement.domnode as IXMLDOMNodeRef; //c++读取属性节点 xdnodeRef = XMLDocument1->DocumentElement->DOMNode; xdnode = xdnodeRef->GetXMLDOMNode(); xdnode->selectSingleNode(L"MakeItems/Item[@Name='teacher']", &xdnode2); BSTR name; this->Caption = name;
xdnode->selectSingleNode
IXMLDOMDocument
_di_IDOMNode dnode; _di_IXMLDOMNodeList domNodeList; xdnode = dnode->firstChild; void Test_DOMVendor() { TXMLDocument *document; TDOMVendor *vendor; document = new TXMLDocument(NULL); // Get document vendor. vendor = document->DOMVendor; if (vendor != 0) { printf("Document DOM vendor is \"%ls\".\n", vendor->Description()); } else { printf("Document has no DOM vendor.\n"); } // Display registered DOM vendors with this application. const int numVendors = DOMVendors->Count(); printf("Number of registered DOM vendors with this application is %d.\n", numVendors); for (int i = 0; i < numVendors; i++) { vendor = DOMVendors->Vendors[i]; printf("\t%ls\n", vendor->Description()); } // Find a specific registered DOM vendor. vendor = DOMVendors->Find("MSXML"); if (vendor != 0) { // Set document DOM vendor. document->DOMVendor = vendor; printf("Document DOM vendor is \"%ls\".\n", document->DOMVendor->Description()); } else { printf("Could not find DOM vendor.\n"); } delete document; } 遍历节点 XmlNode: IXMLNode;
XmlNodeList := Xml.DocumentElement.ChildNodes;
保存文件 procedure TFrmPublicHealthMain.PNGButton1Click(Sender: TObject); var pNode,cNode: IXMLNode; {定义两个节点: 父节点、子节点} astr:string; ms:TMemoryStream ; ss:TStringStream; begin XMLDocument1.XML.Clear; XMLDocument1.Active := True; {必须先激活} XMLDocument1.Version := '1.0'; {设置版本} XMLDocument1.Encoding := 'utf-8'; {设置语言} pNode := XMLDocument1.AddChild('科室名单'); {添加的第一个节点是根节点, 现在的 pNode 是根节点} pNode.SetAttribute('备注', '测试'); {为根节点设置属性} pNode := pNode.AddChild('人员'); {为根节点添加子节点, 现在的 pNode 是 "人员" 节点} pNode.SetAttribute('职务', '科长'); {设置属性} pNode.SetAttribute('备注', '正局级'); cNode := pNode.AddChild('姓名'); {为 pNode 添加子节点, 返回值 cNode 指向了新添加的节点} cNode.Text := '张三'; cNode := pNode.AddChild('性别'); cNode.Text := '男'; cNode := pNode.AddChild('年龄'); cNode.Text := '34'; {查看} //缺少 Encoding ShowMessage(XMLDocument1.XML.Text); {保存} XMLDocument1.SaveToFile('2.xml'); XMLDocument1.SaveToXML(astr); self.mmoIn.Lines.Text := astr; //这样就有encoding了 ms:= TMemoryStream.Create; XMLDocument1.SaveToStream(ms); ss := TStringStream.Create; ms.Position:=0; ss.CopyFrom(ms,ms.Size); self.mmoIn.Lines.Text := ss.DataString; ss.Free; ms.Free; end; pNode.ChildValues['cfh'] := '023535325';
C# XmlTextWriter https://www.cnblogs.com/gc2013/p/4267636.html // XmlTextWriter 写文件 XmlTextWriter writeXml = new XmlTextWriter(@c:MyXml.xml,Encoding.UTF8); writeXml.WriteStartDocument(false); writeXml.WriteStartElement(NetWork); writeXml.WriteComment(网络配置信息); writeXml.WriteStartElement(configration); writeXml.WriteElementString(IpAddress,192.168.2.168); writeXml.WriteElementString(Netmask, 255.255.255.0); writeXml.WriteElementString(Gateway, 202.103.24.68); writeXml.WriteEndElement(); writeXml.WriteEndElement(); writeXml.Flush(); writeXml.Close(); 不用findNode的写法 Xml.DocumentElement.ChildNodes['makeCard'].ChildNodes['sfzCard'].NodeValue 遍历节点 self.XMLDocument1.LoadFromXML(Memo2.Lines.Text); for i := 0 to XMLDocument1.DocumentElement.ChildNodes.Count - 1 do begin self.Caption := XMLDocument1.DocumentElement.ChildNodes[i].NodeName; self.Caption := XMLDocument1.DocumentElement.ChildNodes[i].Text; end;
for (int i = 0; i < XMLDocument1->DocumentElement->ChildNodes->Count; i++) { s1 = XMLDocument1->DocumentElement->ChildNodes->Get(i)->NodeName; s2= XMLDocument1->DocumentElement->ChildNodes->Get(i)->Text;
ShowMessage(Node.Attributes.getnameditem('title').Text); // title2 Node.Attributes.getnameditem('title').Text:='hello'; // <Item name="name2" title="title2"/> => <Item name="name2" title="hello"/>
C# 保存时:
DataSet dataSet = new DataSet(); DataTable dataTable = new DataTable(); dataSet.Tables.Add(dataTable); dataSet.WriteXml("123.xml"); dataSet.ReadXml("123.xml"); DataTable dataTable = dataSet.Tables[0];
有了这个Schema,解析xml太牛了 xml数据文件,就直接到数据集了databable <?xml version="1.0" encoding="utf-8" standalone="no"?> <data1> <row> <AKB020>0010023</AKB020> <AKC190>000000053628455</AKC190> <BKC037>1</BKC037> <AKC220>0</AKC220> <AAE073>0001218982</AAE073> <AAE072>00002</AAE072> </row> <row> <AKB020>0010023</AKB020> <AKC190>000000053628455</AKC190> <BKC037>1</BKC037> <AKC220>0</AKC220> <AAE073>0001218982</AAE073> <AAE072>00002</AAE072> </row> XMLSchema
<?xml version="1.0" standalone="yes"?> <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="row" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="row"> <xs:complexType> <xs:sequence> <xs:element name="AKB020" type="xs:string" minOccurs="0" /> <xs:element name="AKC190" type="xs:string" minOccurs="0" /> <xs:element name="BKC037" type="xs:string" minOccurs="0" /> <xs:element name="AKC220" type="xs:string" minOccurs="0" /> <xs:element name="AAE073" type="xs:string" minOccurs="0" /> <xs:element name="AAE072" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema>
TNativeXml *DocXml = new TNativeXml(NULL); DocXml->Root->NodeByName("sqldata")->NodeByName("Row")->NodeByName("prm_sysdate")->ValueUnicode; c#写xml XmlTextWriter myXmlTextWriter = new XmlTextWriter(System.IO.Directory.GetCurrentDirectory()+@"\KPDZJKK.xml", null); //使用 Formatting 属性指定希望将 XML 设定为何种格式。 这样,子元素就可以通过使用 Indentation 和 IndentChar 属性来缩进。 myXmlTextWriter.Formatting = Formatting.Indented; myXmlTextWriter.WriteStartDocument(false); myXmlTextWriter.WriteStartElement("bookstore"); myXmlTextWriter.WriteStartElement("book"); myXmlTextWriter.WriteAttributeString("paname", paname); myXmlTextWriter.WriteAttributeString("sfzh", sfzh); myXmlTextWriter.WriteAttributeString("phone", phone); myXmlTextWriter.WriteAttributeString("sex", sex); myXmlTextWriter.WriteAttributeString("mz", mz); myXmlTextWriter.WriteAttributeString("VUId", VUId); myXmlTextWriter.WriteEndElement(); myXmlTextWriter.Flush(); myXmlTextWriter.Close(); c#读取xml string ykt_in_xml; ykt_in_xml = System.IO.Directory.GetCurrentDirectory() + @"\KPDZJKK_YKT.xml"; XmlDocument doc = new XmlDocument(); if (System.IO.File.Exists(ykt_in_xml)) { doc.Load(ykt_in_xml); nametxtbox.Text = doc.SelectSingleNode("/bookstore/book").Attributes["paname"].Value; sextxtbox.Text = doc.SelectSingleNode("/bookstore/book").Attributes["sex"].Value; mztxtbox.Text = doc.SelectSingleNode("/bookstore/book").Attributes["mz"].Value; phonetxtbox.Text = doc.SelectSingleNode("/bookstore/book").Attributes["phone"].Value; codetxtbox.Text = doc.SelectSingleNode("/bookstore/book").Attributes["sfzh"].Value;
对象转换为XML,类转换为XML 智能是class,因为record不支持这种rtti的反射。record用json的序列化是支持的,没时间研究官方的序列化怎么做到支持record的; TRttiField是字段,public里定义的就是字段能获取到。 function obj2xml(rootName:string;obj:TObject):string; var xmldcoc: IXMLDocument; pNode: IXMLNode; t: TRttiType; // p: TRttiProperty; f: TRttiField; r: TValue; begin t := TRttiContext.Create.GetType(obj.ClassType ); xmldcoc := NewXMLDocument(''); pNode := xmldcoc.AddChild(rootName); for f in t.GetFields do pNode.AddChild(f.name).Text := f.GetValue(obj).ToString; result := xmldcoc.Xml.Text; end; <request> 调用代码 procedure TForm1.Button15Click(Sender: TObject); var obj:tperson; begin obj := tperson.Create(); obj.name := '张飞'; obj.age := 28; obj.sex := '男'; self.Memo1.Text:= obj2xml('request',obj); obj.Free; end; tperson = class 还有个办法 nativexml 的 ObjectSaveToXmlString XML2ds <?xml version="1.0" encoding="gb2312"?> <!--文档版本信息, |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论