2016-05-11 12 views
0

ねえ、すべてのI私はクラスを見つけることの必要性で午前次のコードを持っている:解析XML

Dim nodelist As System.Xml.XmlNodeList = Nothing 
Dim doc As New System.Xml.XmlDocument() 

doc.LoadXml(tmpData) 
nodelist = doc.SelectNodes("//entry/content/sp_0:div/span/sp_0:span") 

For Each node As System.Xml.XmlElement In nodelist 
    Debug.print(node("OrderID").InnerText) 
Next 

そして、XMLは次のようになります。

<feed 
    xmlns:app="http://www.w3.org/2007/app" 
    xmlns:thr="http://purl.org/syndication/thread/1.0" 
    xmlns:fh="http://purl.org/syndication/history/1.0" 
    xmlns:snx="http://www.ibm.com/xmlns/prod/sn" 
    xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" 
    xmlns="http://www.w3.org/2005/Atom"> 
    <id>2006:feed</id> 
    <generator version="5.0.0.0" uri="http://www.ibm.com/xmlns/prod/sn">IBM Connections - Profiles</generator> 
    <title type="text">reporting chain for Bill Gates</title>   
    <opensearch:itemsPerPage>8</opensearch:itemsPerPage> 
    <fh:complete></fh:complete> 
    <link 
     href="http://..." 
     rel="self" 
     type="application/atom+xml"></link> 
    <entry> 
     <id>tag:profiles.ibm.com,2006</id> 
     <title type="text">Bill Gates</title> 
     <updated>2016-05-11T06:39:54.908Z</updated> 
     <category term="profile" scheme="http://www.ibm.com/xmlns/prod/sn/type"></category> 
     <contributor> 
      <name>Bill Gates</name> 
      <snx:userid>010101</snx:userid> 
      <email>[email protected]</email> 
      <snx:userState>active</snx:userState> 
      <snx:isExternal>false</snx:isExternal> 
     </contributor> 
     <link 
      href="http://..." 
      rel="http://www.ibm.com/xmlns/prod/sn/profile-type" 
      type="application/profile-type+xml"></link> 
     <thr:in-reply-to> 
      <app:accept>ref</app:accept> 
      <app:accept>tag:profiles.ibm.com,2006</app:accept> 
     </thr:in-reply-to> 
     <summary type="text">Profile information for Bill Gates</summary> 
     <content type="xhtml"> 
      <sp_0:div 
       xmlns="http://www.w3.org/1999/xhtml" 
       xmlns:sp_0="http://www.w3.org/1999/xhtml"> 
       <sp_0:span class="vcard"> 
        <sp_0:div class="x-groupwareMail" style="display:none"></sp_0:div> 
        <sp_0:div class="org"> 
         <sp_0:span class="organization-unit"></sp_0:span> 
        </sp_0:div> 
        <sp_0:div class="role"></sp_0:div> 
        <sp_0:div class="title">Applications Developer/Analyst</sp_0:div> 
        <sp_0:div class="uid">265418</sp_0:div> 
        <sp_0:div class="x-profile-uid">010101</sp_0:div> 
        <sp_0:div class="x-lconn-userid">265418</sp_0:div> 
       </sp_0:span> 
      </sp_0:div> 
     </content> 
    </entry> 
    <entry> 
     etc....    
    </entry> 
</feed> 

私はノードリスト= doc.SelectNodes( "//エントリ/コンテンツ/ sp_0:DIV /スパン/ sp_0:スパン")上のエラーを取得は言って:

名前空間マネージャーまたはXsltContextが必要です。このクエリには、接頭辞、変数、またはユーザー定義関数があります。

どのように内部テキストを得ることができますか?

+0

[ネームスペースを含むXMLを解析するためにXPathとVB.NETを使用する]の可能な複製(http://stackoverflow.com/questions/16949495/using-xpath-and-vb-net-to-parse-xml-containing-名前空間) – DWRoelands

+0

@DWRoelands **ではない**異なるクラス値の名前の上記の質問のために重複しています。 – StealthRT

答えて

1

あなたのXPathで使用する名前空間を定義するためにXmlNamespaceManagerクラスを使用する必要がある、とSelectNodes方法にこれを渡します。さらに、私は空の名前空間をデフォルトの名前空間(おそらくXPathでの制限?)で動かすことができなかったので、そのための接頭辞を定義しなければなりませんでした。

また、XPathがサンプルXMLと一致していないようです。

Dim nodelist As System.Xml.XmlNodeList = Nothing 
Dim doc As New System.Xml.XmlDocument() 

doc.LoadXml(tmpData) 

Dim nsmgr = New XmlNamespaceManager(doc.NameTable) 
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom") 
nsmgr.AddNamespace("sp_0", "http://www.w3.org/1999/xhtml") 

nodelist = doc.SelectNodes(
    "//atom:entry/atom:content/sp_0:div/sp_0:span/sp_0:div[@class='x-profile-uid']", 
    nsmgr 
) 

For Each node As System.Xml.XmlElement In nodelist 
    Debug.Print(node.InnerText) 
Next 

出力:

010101 

がうまくいけば、あなたのニーズにこれを適応させることができますが、<sp_0:div class="x-profile-uid">010101</sp_0:div>要素の値を取得したいとしましょう、あなたはこのような何かをしたいと思います。

+0

** sp_0:div **が** sp_0:div **、** sp_1:div **、** sp_2:div **などに変更されて以来、どのくらいのコードが変更されるでしょうか。 _xxそれはありますか? – StealthRT

+0

@StealthRT 'sp_0'、' sp_1'など異なる名前空間ですか、同じ名前空間の異なる接頭辞ですか? XPathは、XMLが使用するプレフィックスには関係なく、名前空間が一致するだけです。 – Mark

+0

それは* SELECTNODES *そう** //原子以内です:エントリー/アトム:コンテンツ/ sp_0:DIV/sp_0:スパン/ sp_0:divの[クラス@ = 'X-プロファイルのuid'] **は*のようなものになるだろう*/**:// // atom:entry/atom:content/sp_1 */div: :div/sp_1:span/sp_1:div [@ class = 'x-profile-uid'] **、** // atom:entry/atom:コンテンツ/ sp_2:div/sp_2:span/sp_2:div [クラス= 'X-プロファイルのuid'] **、など。 とも** nsmgr.AddNamespace( "sp_0"、「http://www.w3.org/1999/xhtml")**、* * nsmgr.AddNamespace( "sp_1"、 "http://www.w3.org/1999/xhtml")**など – StealthRT

0

ルート要素に他の名前空間(xmlns:app = "...")を定義したのと同じ方法で、名前空間 "sp_0"を定義する必要があります。

要素の最後の行を見てください。これはSP_0の名前空間を定義する方法です。

<feed 
xmlns:app="http://www.w3.org/2007/app" 
xmlns:thr="http://purl.org/syndication/thread/1.0" 
xmlns:fh="http://purl.org/syndication/history/1.0" 
xmlns:snx="http://www.ibm.com/xmlns/prod/sn" 
xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" 
xmlns="http://www.w3.org/2005/Atom" 
sp_0="http://www.w3.org/1999/xhtml"> 

+0

コード例を表示できますか? – StealthRT