这是我的第一篇文章,我有这种情况: 如何使用MSXML从xsd模式文件获取属性?这是可能的,或者我需要另一种方式。 我正在使用Delphi和MSXML 6.0。
答案 0 :(得分:8)
您可以使用IXMLDOMDocument
阅读xsd
架构文件。这是一个例子:
(该示例使用此XML Schema)
uses ComObj, MSXML;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
xmlDoc: IXMLDOMDocument;
node: IXMLDomNode;
begin
xmlDoc := CreateOleObject('Microsoft.XMLDOM') as IXMLDOMDocument;
xmlDoc.async := False;
xmlDoc.load('C:\po.xsd');
if xmlDoc.parseError.errorCode <> 0 then
raise Exception.Create('XML Load error:' + xmlDoc.parseError.reason);
node := xmlDoc.selectSingleNode('//xsd:schema/xsd:element[@name="purchaseOrder"]');
ShowMessage(node.attributes.getNamedItem('type').text); // "PurchaseOrderType"
end;
答案 1 :(得分:0)
是的,这是可能的;开始here,然后完成它。有代码示例,所以如果您正在阅读并仍然没有找到您需要的内容,请在示例上发布一个特定的问题,您的输出应该是什么样的,并且有人能够更有效地帮助您。
对于任何微不足道的事情,您应该始终使用专门的模式处理器,通常使用“SOM”缩写标记:模式对象模型。
下面的代码显示了从页面摘录的主要步骤(它是VB,但应该很容易转换为Deplhi)。
Set oSchemaCache = CreateObject("Msxml2.XMLSchemaCache.6.0")
' Load the schema.
nsTarget="http://www.example.microsoft.com/po"
oSchemaCache.add nsTarget, "po.xsd"
Set oSchema = oSchemaCache.getSchema(nsTarget)
For Each oA in oSchema.attributes
result = result + printAttr(oA, t)
Next
与复杂类型相关的属性可以与此类似地处理(属性位于底部):
Function processComplexType(oComplex, t)
Dim res As String
Dim strAny As String
Dim oAttr As ISchemaAttribute
res = printTab(t) + "<xsd:complexType"
If oComplex.name <> "" Then
res = res + " name='" + oComplex.name +"'"
End If
res = res + ">"
If oComplex.contentType = SCHEMACONTENTTYPE_EMPTY Then
res = res + printRemark("emtpy")
End If
If oComplex.contentType = SCHEMACONTENTTYPE_TEXTONLY Then
res = res + printRemark("textonly")
End If
If oComplex.contentType =SCHEMACONTENTTYPE_ELEMENTONLY Then
res = res + printRemark("elementonly")
res = res + processGroup(oComplex.contentModel, t+1)
End If
If oComplex.contentType = SCHEMACONTENTTYPE_MIXED Then
res = res + printRemark("mixed")
res = res + processGroup(oComplex.contentModel, t+1)
End If
res = res + vbNewline
If oComplex.baseTypes.length > 0 Then
res = res + printRestrictions(oComplex, t+1)
End If
On Error Resume Next
StrAny = oComplex.anyAttribute.name
If Err.number = 0 Then
res = res + oComplex.anyAttribute.name
End If
For Each oAttr in oComplex.attributes
res = res + printAttr(oAttr, t+1)
Next
processComplexType = res + printTab(t) + "</xsd:complexType>"+vbNewline
End Function