2017-08-20 21 views
2

xmlドキュメントの詳細を表示してナビゲートしたい。 しかし、名前空間を持っています。この名前空間がMultiblockであるため、lists.lengthをゼロにしています(下記のvbaコードを参照してください)VBAを使用して名前空間が存在する場合のXMLノード値の取得

ご案内しています。読んでいただきありがとうございます。 :) 私のXML文書は次のとおりです。

<?xml version="1.0" encoding="UTF-16"?> 
<MultiBlock xmlns="x-schema:ConfigFileSchema.xml"> 
<ErdbVersion> 
    <DbVersion>14.0</DbVersion> 
    <DbDesc>ERDB Release EXP431.1-49.0</DbDesc> 
    <DbGUID>56CFAF87-53A9-4042-8B4F-4CF94868416E</DbGUID>   
<DbLangID>ENU</DbLangID> 
</ErdbVersion> 
<Block> 
    <BlockDef> 
     <BlockName>J60AOV1136</BlockName> 
     <EntityName>J60AOV1136</EntityName> 
     <BlockId>20031267</BlockId> 
     <BlockGUID>D11BF0DB-803D-49FC-A594-D234ABD1E156    
</BlockGUID> 
     <BlockDesc>Exported on (MM-DD-YY HH:MM) 07-31-    2017 
10:12</BlockDesc> 
     <TemplateName>SYSTEM:CONTROLMODULE</TemplateName>    
<ClassName>CONTROLMODULE</ClassName>       
<BaseTemplateName>SYSTEM:CONTROLMODULE      
</BaseTemplateName> 
     <CreateType> </CreateType> 
     <Attribute>1610613248</Attribute> 
     <LifeCycleState>Loaded</LifeCycleState>     
<AssignedTo>STFCEE8A_03</AssignedTo> 
     <Container></Container> 
    </BlockDef> 
    <Parameters> 
     <Parameter> 
      <ParamName>ALIASOPT</ParamName> 
      <ParamValue>OFF</ParamValue> 
     </Parameter> 
     <Parameter> 
      <ParamName>DISCOVORDER</ParamName>      
<ParamValue>"TPN"</ParamValue> 
     </Parameter> 
     <Parameter> 
      <ParamName>METHODSCOPE</ParamName>      
<ParamValue>"ALL"</ParamValue> 
     </Parameter> 
    </Parameters 
</Block 
</MultiBlock> 

私はxmlns="x-schema:ConfigFileSchema.xml"せずにノードの詳細情報を取得しようとしていたとき、私は、ノードをナビゲートし、値を取得することができています。

同じ文書とxmlns属性では、ナビゲートに問題があります。ここで

は私のVBAコードは次のとおりです。

Sub AOVXML() 
Dim XDoc As MSXML2.DOMDocument 
Dim firstNamefield As MSXML2.IXMLDOMNodeList 
Dim lists As MSXML2.IXMLDOMNodeList 
Dim i, j As Integer 
'Dim lists As MSXML2.IXMLDOMNode 

Set XDoc = CreateObject("MSXML2.DOMDocument") 
XDoc.async = False: XDoc.validateOnParse = False 
XDoc.Load (ThisWorkbook.Path & "\J60AOV1136.cnf.xml") 

'Reading the List Node 
Set lists = XDoc.SelectNodes("//MultiBlock/Block") 
MsgBox "Length of Lists nodes : " & lists.Length & vbCrLf & _ 
    "First XML List Node : " 


'Getting First Child node under Lists 
Set firstNamefield = lists(0).ChildNodes 
MsgBox firstNamefield.Length 

'Looping through all XML nodes under List node 
For i = 0 To firstNamefield.Length - 1 
MsgBox firstNamefield(i).XML 
Next i 

Set XDoc = Nothing 
End Sub 
+0

多分NS'はそれは長い時間以来をされている 'で終わる名前空間でのアクションを(実行するMSXML内の別々の方法があります?私はMSXMLを最後に使用しました。)。 MSXMLドキュメントのコピーを見つけることは、エクササイズとして残ります。 – Richard

+0

https://stackoverflow.com/questions/45404020/xpath-syntax-to-select-nodes-with-multiple-attributes-in-the-path/ 45404432#45404432 –

+0

こんにちは@TimWilliams、ありがとう、私はリンクを使用し、私のXML文書の書き込みを試みた。しかし、私は値を意図していません。ここに私のコードです: 'XDoc.setProperty" SelectionLanguage "、" XPath " 'デフォルトの名前空間を設定し、接頭辞(例:" xx ")を付けてください。 XDoc.setProperty" SelectionNamespaces "、_ " xmlns:xx =' x -schema:ConfigFileSchema.xml '" 'リストノードを読み取る リストを設定する= XDoc.SelectNodes(" // xx:MultiBlock/xx:Block ")' –

答えて

0

は、以下の修正されたサンプルを使用しJ60AOV1136.cnf.xml

<?xml version="1.0" encoding="utf-8"?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

と同じフォルダにConfigFileSchema.xmlをスキーマファイルを作成し、それに名前を付けます:

Option Explicit 
Sub AOVXML() 
    Dim XDoc As MSXML2.DOMDocument60 
    Dim firstNamefield As MSXML2.IXMLDOMNodeList 
    Dim lists As MSXML2.IXMLDOMNodeList 
    Dim i As Integer 
    Dim thePath As String 

    Set XDoc = CreateObject("MSXML2.DOMDocument.6.0") 'Must be 6 for schema parsing to pass 
    XDoc.async = False 
    thePath = ThisWorkbook.Path & "\J60AOV1136.cnf.xml" 
'* 
'* It is useful to have the reason for a possible error 
'* Sample code found here: 
'* https://msdn.microsoft.com/en-us/library/aa468547.aspx 
'* 
    If Not XDoc.Load(thePath) Then ' The document failed to load. 
     Dim strErrText As String 
     Dim xPE As MSXML2.IXMLDOMParseError 
     Set xPE = XDoc.parseError 
     With xPE 
     strErrText = "Your XML Document failed to load due the following error." & vbCrLf & _ 
      "Error #: " & .ErrorCode & ": " & xPE.reason & _ 
      "Line #: " & .Line & vbCrLf & _ 
      "Line Position: " & .linepos & vbCrLf & _ 
      "Position In File: " & .filepos & vbCrLf & _ 
      "Source Text: " & .srcText & vbCrLf & _ 
      "Document URL: " & .Url 
     End With 
     MsgBox strErrText 
     Exit Sub 
    End If 
'* 
'* Reading the List Node 
'* To accept all namespaces, use the form //*[local-name()='<nodename>'] 
'* 
    Set lists = XDoc.SelectNodes("//*[local-name()='Block']") '<- here is the major change 
    If lists.Length > 0 Then 
     MsgBox "Length of Lists nodes : " & lists.Length & vbCrLf & "First XML List Node : " 
'* 
'* Getting First Child node under Lists 
'* 
     Set firstNamefield = lists(0).ChildNodes 
     MsgBox firstNamefield.Length 
'* 
'* Looping through all XML nodes under List node 
'* 
     For i = 0 To firstNamefield.Length - 1 
      MsgBox firstNamefield(i).XML 
     Next i 
    End If 
    Set XDoc = Nothing 
End Sub 

への参照を追加する必要があることに注意してください。Microsoft XML、6.0このコードを使用するには

0

お手数ですがありがとうございます。 あなたが言ったように私はやった。 今、私はlocal-nameとxpathsを使ってどのようにアクセスできるかを理解しようとしています(可能な場合)。 あなたの知識を共有していただきありがとうございます。 :) :) :)

以下のコードを使用してXMLスキーマファイルを作成:

Sub Create_Schema() 
Dim StrMyXml As String, MyMap As XmlMap 
Dim StrMySchema As String 
' Book.xml is the file created in section one of this topic. 
StrMyXml = ThisWorkbook.Path & "\J60AOV1136.cnf.xml" 

' Turn off async loading. 
Application.DisplayAlerts = False 
' Add the string to the XmlMaps collection. 
Set MyMap = ThisWorkbook.XmlMaps.Add(StrMyXml) 
Application.DisplayAlerts = True 

' Create an empty file and output the schema. 
StrMySchema = ThisWorkbook.XmlMaps(1).Schemas(1).XML 
Open ThisWorkbook.Path & "\ConfigFileSchema.xml" For Output As #1 
Print #1, StrMySchema 
Close #1 
End Sub 
関連する問題