2016-12-29 3 views
1

特定のノードの異なる属性に従ってXMLファイルを分割したいファイルの上部に同じノードを持つ分離されたXMLファイルをすべて作成する+属性とその基礎となる内容をこのノードの終わりまで削除します。ノード属性に応じて異なるXMLファイル内のXMLファイルを分割する

すべての分離されたXMLファイルは、同様のエンドノードで終了する必要があります。

XMLファイルの例:

<?xml version=""1.0"" encoding=""UTF-8""?> 
<node1> 
    <node2> 
    <node3 attribute='1'>item</node3> 
    <node3 attribute='2'>item</node3> 
    <node3 attribute='3'>item</node3> 
    </node2> 
<node6 attribute='1'> 
    <node7>item = (node3 attribute2)</node7> 
    <node8>item = (node3 attribute3)</node8> 
</node6> 
<node6 attribute='2'> 
    <node9>item = (node3 attribute1)</node9> 
    <node10>item = (node3 attribute2)</node10> 
</node6> 
</node1> 

この例から、私は、新しいXMLファイルを作成するためのブレークポイントであることをnode6の属性を使用します。

分離XML 1:このように見て2つのXMLファイルに結果の

<?xml version=""1.0"" encoding=""UTF-8""?> 
<node1> 
    <node2> 
    <node3 attribute='1'>item</node3> 
    <node3 attribute='2'>item</node3> 
    <node3 attribute='3'>item</node3> 
    </node2> 
<node6 attribute='1'> 
    <node7>item = (node3 attribute2)</node7> 
    <node8>item = (node3 attribute3)</node8> 
</node6> 

分離XML 2:私は見ていると、すべてのこれらの答えでの作業が、彼らはそうしなかったされている

<?xml version=""1.0"" encoding=""UTF-8""?> 
<node1> 
    <node2> 
    <node3 attribute='1'>item</node3> 
    <node3 attribute='2'>item</node3> 
    <node3 attribute='3'>item</node3> 
    </node2> 
<node6 attribute='2'> 
    <node9>item = (node3 attribute1)</node9> 
    <node10>item = (node3 attribute2)</node10> 
</node6> 
</node1> 

上記のように適切なコードを見つけるのを手伝ってください。

https://stackoverflow.com/questions/30374533/split-xml-files-newbie

How to split an xml file in vb

Splitting Xml Document according to node

誰かが私がこれを行うための最善の方法は何であるかを見つけ出す手助けすることはできますか?

+0

あなたがXSLTに精通していますか?それはあなたのために仕事をすることができます。たとえばhttp://stackoverflow.com/questions/5578602/how-to-filter-nodes-in-xml-using-xsltを参照してください。私はVBのプログラマですが、このタイプのタスクには、このタイプまたは他の同様のプログラミング言語を使用することをお勧めしません(通常のソリューションを作成するのではなく、 XSLTをチェックしてVBの代わりに使用することをお勧めします。それは仕事のための適切なツールですので、少ない労力で結果を得ることができます。 – miroxlav

+0

情報miroxlavありがとう。私は残念ながら、XSLTについて全く知らない。私の目標は、別のユーザーがこのプログラムとXMLファイルを分割できるようにWindowsフォームプログラムを作成することです。 VB Windowsフォーム内にXSLTプログラムを実装することは可能ですか? – Woudi

答えて

0

感謝あなたのためにすべてのupport!

参考文献としてBibek Gautamのquestion and answerを参考にして、あなたと他の人からのフィードバックを得て、別のクラスの次の作業コードを使って、上記の例のXMLをcommonstringとファイル固有の文字列を含むXMLファイルに分割しました他のノード3の可能性を除いて、ノード3属性が共通文字列内になければならない基準としてのノード7,8,9および10を含む。

コードには、例XMLに記載されていない余分なノードがいくつか含まれています。

ほぼ完全なコードを投稿したので、同様の目的を持つ他の人がこれを参考に使用できます。

コード:

Shared Sub CreateXML() 

    Dim xOrgXml As New XmlDocument 
    Dim pSavelocation As String = "mysavelocation" 
    Dim pProgressbar As ProgressBar = Form3.f3.ProgressBar1 
    Dim cCommonString As String 
    Dim dDocumentRootNodes As XmlNodeList 
    'implemented progessbar' 
    Dim mProgressBarMaximum As Integer   
    Dim mFoldername As String 

    Try 
     'Public class containing shared location of source XML' 
     xOrgXml.Load(ClsSharedProperties.filePath) 
     cCommonString = "<?xml version=""1.0""?>" & "<Node1>" 
     dDocumentRootNodes = xOrgXml.GetElementsByTagName("Node1") 
     mProgressBarMaximum = xOrgXml.GetElementsByTagName("Node6").Count + xOrgXml.GetElementsByTagName("Node3").Count 

     pProgressbar.Minimum = 0 
     pProgressbar.Maximum = mProgressBarMaximum 
     pProgressbar.Value = 0 
     pProgressbar.Visible = True 

     '===================================================================================================================' 
     'Building Common String' 
     '===================================================================================================================' 

     For Each Node1Node As XmlNode In dDocumentRootNodes 
      Dim Node1ChildNodes As XmlNodeList = Node1Node.ChildNodes 

      For Each Node1Childnode As XmlNode In Node1ChildNodes 
       If Node1Childnode.Name = "node4" Then 
        cCommonString = cCommonString & Node1Childnode.OuterXml 

       Else 
        If Node1Childnode.Name = "node5" Then 
         cCommonString = cCommonString & Node1Childnode.OuterXml 

        Else 
         If Node1Childnode.Name = "node12" Then 
          cCommonString = cCommonString & Node1Childnode.OuterXml 
         End If 
        End If 
       End If 
      Next 
     Next 

     Dim mXMLDocSave As XmlDocument 
     Dim mFileName As String 
     Dim fFullString As String 

     mXMLDocSave = New XmlDocument() 

     '==============================================================' 
     'Creating Directories and files For xml Getting Name and attribute value from Node6-NODE1node' 
     '===============================================================' 

     For Each Node1Node As XmlNode In dDocumentRootNodes 
      Dim Node1ChildNodes As XmlNodeList = Node1Node.ChildNodes 
      For Each NODE1node As XmlNode In Node1ChildNodes 

       If NODE1node.Name = "Node6" Then 

        Dim Node6Attribute As XmlAttributeCollection = NODE1node.Attributes 
        If Node6Attribute.GetNamedItem("attribute").Value = "1" Then 
         pProgressbar.Increment(1) 
         Dim cCommonStringNode6_1 As String = cCommonString 

         Dim i As Integer 
         Dim s As String 

         For i = 0 To (Form3.f3.CheckedListBox1.Items.Count - 1) 
          If Form3.f3.CheckedListBox1.GetItemChecked(i) = True Then 
           s = Form3.f3.CheckedListBox1.Items(i).ToString 
           If s = "EXAMPLE A" Then 
            mFoldername = "EXAMPLE A" 
            If (Not IO.Directory.Exists(pSavelocation & "\" & mFoldername)) Then 
             IO.Directory.CreateDirectory(pSavelocation & "\" & mFoldername) 
            End If 
           ElseIf s = "EXAMPLE B" Then 
            mFoldername = "EXAMPLE B" 
            If (Not IO.Directory.Exists(pSavelocation & "\" & mFoldername)) Then 
             IO.Directory.CreateDirectory(pSavelocation & "\" & mFoldername) 
            End If 
           End If 
          End If 
         Next 
         For i = 0 To (Form3.f3.CheckedListBox1.Items.Count - 1) 
          If Form3.f3.CheckedListBox1.GetItemChecked(i) = True Then 
           s = Form3.f3.CheckedListBox1.Items(i).ToString 
           If s = "EXAMPLE A" Then 
            mFileName = Date.Now.ToString("yyyyMMdd-HHmm") + "_" + NODE1node.Name.ToString + "_" + (Node6Attribute.GetNamedItem("attribute").Value).ToString + "_" + "EXAMPLE A" 
            mFileName = mFileName.Replace(".", "_").Replace(" ", "_").Replace("''", "_").Replace("<", "").Replace(">", "").Replace("d", "D") 
           ElseIf s = "EXAMPLE B" Then 
            mFileName = Date.Now.ToString("yyyyMMdd-HHmm") + "_" + NODE1node.Name.ToString + "_" + (Node6Attribute.GetNamedItem("attribute").Value).ToString + "_" + "EXAMPLE B" 
            mFileName = mFileName.Replace(".", "_").Replace(" ", "_").Replace("''", "_").Replace("<", "").Replace(">", "").Replace("d", "D") 

           End If 
          End If 
         Next 
         For Each Node1Node2 As XmlNode In dDocumentRootNodes 
          Dim Node1ChildNodes2 As XmlNodeList = Node1Node2.ChildNodes 
          For Each NODE1node2 As XmlNode In Node1ChildNodes2 

           If NODE1node2.Name = "Node3" Then 
            pProgressbar.Increment(1) 
            Dim xNode6Node3List As XmlNodeList = xOrgXml.SelectNodes("/Node1/Node6[@attribute='1']//Node3") 

            For Each Node6NODE3_Name As XmlNode In xNode6Node3List 
             pProgressbar.Increment(1) 
             If (Node6NODE3_Name.InnerText).ToString = (NODE1node2.Attributes("attribute").Value).ToString Then 

              Dim NODE1_NODE3_Node_String As String = NODE1node2.OuterXml.ToString 
              'check if node specific string already contains the selected node. If not add it else skip it' 
              If cCommonStringNode6_1.Contains(NODE1_NODE3_Node_String) = False Then 
               cCommonStringNode6_1 = cCommonStringNode6_1 & NODE1node2.OuterXml 
              End If 
             End If 
            Next 
           End If 
          Next 
         Next 
         'create the fullstring to be saved as new XML document' 
         fFullString = cCommonStringNode6_1 & NODE1node.OuterXml & "</Node1>" 
         mXMLDocSave.LoadXml(fFullString) 
         'Make all node6 attributes have value "1"' 
         For Each node2 As XmlAttribute In mXMLDocSave.SelectNodes("//Node6/@attribute") 
          node2.Value = "1" 
         Next 
         Dim countervalue As Integer = 0 
         For Each Node1Childnode As XmlNode In mXMLDocSave.SelectNodes("/Node1/Node3") 
          If Node1Childnode.Name = "Node3" Then 

           Dim NODE3_NodeList As XmlNodeList = Node1Childnode.ChildNodes 
           For Each NODE3_Node As XmlNode In NODE3_NodeList 

            If NODE3_Node.Name = "Node11" Then 
             countervalue += 1 
             NODE3_Node.InnerText = countervalue.ToString 
            End If 
           Next 
          End If 
         Next 
         mXMLDocSave.Save(pSavelocation & "\" & mFoldername & "\" & mFileName & ".xml") 
         mXMLDocSave = New XmlDocument() 
         fFullString = String.Empty 
         mFoldername = String.Empty 
         mFileName = String.Empty 
         End If 
       End If 
      Next 
     Next 
    Catch ex As Exception 
     MessageBox.Show(ex.Message & vbCrLf & "Stack Trace: " & vbCrLf & ex.StackTrace) 
    End Try 
0

具体的にはVBの解決策を尋ねられますが、ここではC#のものがあります。

using System; 
using System.Windows.Forms; 
using System.Xml.Linq; 
using System.IO; 

namespace SplitXmlFile_41385730 
{ 
    public partial class Form1 : Form 
    { 
     public static string incomingXML = @"M:\StackOverflowQuestionsAndAnswers\SplitXmlFile_41385730\SplitXmlFile_41385730\Samples\data.xml"; 
     public static string outgoingXML = @"M:\StackOverflowQuestionsAndAnswers\SplitXmlFile_41385730\SplitXmlFile_41385730\Samples\data_out.xml"; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      XElement theincomingDoc = new XElement(XDocument.Load(incomingXML).Root);//the incoming XML 

      //store the header of your files 
      XElement header = new XElement(theincomingDoc); 
      header.Elements("node6").Remove();//remove these nodes since they need to be parked in their own file 
      int fileCounter = 0;//hold on, we'll use this in a moment 

      //loop through the different nodes you're interested in 
      foreach (XElement item in theincomingDoc.Elements("node6")) 
      { 
       fileCounter++;//increment the file counter 
       string outfilename = Path.GetDirectoryName(outgoingXML) + "\\" + Path.GetFileNameWithoutExtension(outgoingXML) + fileCounter + Path.GetExtension(outgoingXML);//come up with a file name that suits your needs 
       XDocument newoutfile = new XDocument("", new XElement(header));//create a new document and start it with the header we already stored 
       newoutfile.Element("node1").Add(item);//now add the node you need separated 
       newoutfile.Save(outfilename, SaveOptions.None);//save the file 
      } 

     } 
    } 
} 

入力ファイルはこれです:このように見えた

<?xml version="1.0"?> 
<node1> 
    <node2> 
    <node3 attribute="1">item</node3> 
    <node3 attribute="2">item</node3> 
    <node3 attribute="3">item</node3> 
    </node2> 
<node6 attribute="1"> 
    <node7>item = (node3 attribute2)</node7> 
    <node8>item = (node3 attribute3)</node8> 
</node6> 
<node6 attribute="2"> 
    <node9>item = (node3 attribute1)</node9> 
    <node10>item = (node3 attribute2)</node10> 
</node6> 
</node1> 

ガット2つのファイルアウト: Data_out1.xml

<?xml version="1.0" encoding="utf-8"?> 
<node1> 
    <node2> 
    <node3 attribute="1">item</node3> 
    <node3 attribute="2">item</node3> 
    <node3 attribute="3">item</node3> 
    </node2> 
    <node6 attribute="1"> 
    <node7>item = (node3 attribute2)</node7> 
    <node8>item = (node3 attribute3)</node8> 
    </node6> 
</node1> 

data_out2.xml

<?xml version="1.0" encoding="utf-8"?> 
<node1> 
    <node2> 
    <node3 attribute="1">item</node3> 
    <node3 attribute="2">item</node3> 
    <node3 attribute="3">item</node3> 
    </node2> 
    <node6 attribute="2"> 
    <node9>item = (node3 attribute1)</node9> 
    <node10>item = (node3 attribute2)</node10> 
    </node6> 
</node1> 
+0

Blazeに感謝します。私は私の答えに記載されている実用的なソリューションを作成するために、あなたの入力(VBに変換)と他のフィードバックを使用しました。私はこれが、同様の挑戦をして将来のユーザーを助けることを願っています。 – Woudi

関連する問題