2017-08-18 2 views
1

私はwxStyledTextCtrlから派生したクラスを作成しています。空白以外のものを追加することなく、与えられたXMLをあらかじめ確認したいと思います。私は簡単な解決策を見つけることができません。 wxStyledTextCtrl、wxXmlDocument、およびlibxml2のみ使用できます。wxWidgetsでかなり印刷されたXML

私が目指していた結果がwxStringテキスト

<!-- comment1 --> <!-- comment2 --> <node><emptynode/> <othernode>value</othernode></node> 

制御以下含むとのsetTextを呼び出した後、それは私はほとんどこれを達成するために管理のlibxml2を使用して

<!-- comment1 --> 
<!-- comment2 --> 
<node> 
    <emptynode/> 
    <othernode>value</othernode> 
</node> 

を示すが、すべきであるということですXML宣言(例えば、<?xml version="1.0" encoding="UTF-8"?>)も表示されますが、私はこれを望ましくありません。

inb4、私はシンプルでクリーンな解決策を探しています - 私は手動で

は、この使用して、指定されたツールへの簡単な解決策はありますフォーマットされたXMLの最初の行を削除したくありませんか?私は何かが欠けているように感じる。

答えて

0

簡単な解決策はありますか?いいえ、しかし、あなた自身がきれいな印刷機能を書いたければ、基本的にxmlドキュメントツリー上に深みのある最初の繰り返しを作り、印刷する必要があります。タグを閉じるタイミングを知る必要があるという点で、少し複雑です。

これは、wxWidgets xmlクラスのみを使用してこれを行う方法の不完全な例です。現在、属性、自己閉じ要素(サンプルテキストの ''など)、またはその他の特殊な要素型は処理されません。完全なかわいいプリンタはそれらのものを追加する必要があります。

#include <stack> 
#include <set> 
#include <wx/xml/xml.h> 
#include <wx/sstream.h> 

wxString PrettyPrint(const wxString& in) 
{ 
    wxStringInputStream string_stream(in); 
    wxXmlDocument doc(string_stream); 
    wxString pretty_print; 

    if (doc.IsOk()) 
    { 
     std::stack<wxXmlNode*> nodes_in_progress; 
     std::set<wxXmlNode*> visited_nodes; 

     nodes_in_progress.push(doc.GetDocumentNode()); 

     while (!nodes_in_progress.empty()) 
     { 
      wxXmlNode* cur_node = nodes_in_progress.top(); 
      nodes_in_progress.pop(); 
      int depth = cur_node->GetDepth(); 

      for (int i=1;i<depth;++i) 
      { 
       pretty_print << "\t"; 
      } 

      if (visited_nodes.find(cur_node)!=visited_nodes.end()) 
      { 
       pretty_print << "</" << cur_node->GetName() << ">\n"; 
      } 
      else if (!cur_node->GetNodeContent().IsEmpty()) 
      { 
       //If the node has content, just print it now 
       pretty_print << "<" << cur_node->GetName() << ">"; 
       pretty_print << cur_node->GetNodeContent() ; 
       pretty_print << "</" << cur_node->GetName() << ">\n"; 
      } 
      else if (cur_node==doc.GetDocumentNode()) 
      { 
       std::stack<wxXmlNode *> nodes_to_add; 
       wxXmlNode *child = cur_node->GetChildren(); 
       while (child) 
       { 
        nodes_to_add.push(child); 
        child = child->GetNext(); 
       } 

       while (!nodes_to_add.empty()) 
       { 
        nodes_in_progress.push(nodes_to_add.top()); 
        nodes_to_add.pop(); 
       } 
      } 
      else if (cur_node->GetType()==wxXML_COMMENT_NODE) 
      { 
       pretty_print << "<!-- " << cur_node->GetContent() << " -->\n"; 
      } 
      //insert checks for other types of nodes with special 
      //printing requirements here 
      else 
      { 
       //otherwise, mark the node as visited and then put it back 
       visited_nodes.insert(cur_node); 
       nodes_in_progress.push(cur_node); 

       //If we push the children in order, they'll be popped 
       //in reverse order. 
       std::stack<wxXmlNode *> nodes_to_add; 
       wxXmlNode *child = cur_node->GetChildren(); 
       while (child) 
       { 
        nodes_to_add.push(child); 
        child = child->GetNext(); 
       } 

       while (!nodes_to_add.empty()) 
       { 
        nodes_in_progress.push(nodes_to_add.top()); 
        nodes_to_add.pop(); 
       } 

       pretty_print <<"<" << cur_node->GetName() << ">\n"; 
      } 
     } 
    } 

    return pretty_print; 
} 
関連する問題