0

非常に単純な XML ファイルを解析しています。

<?xml version="1.0" encoding="utf-8"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications with XML.</description>
   </book>
</catalog>

そして、デフォルト設定、つまり parse<0>() を使用して解析すると、次のようになります。

    ifstream file(xmlFile.c_str(),  ifstream::in );
int length;
file.seekg (0, ios::end);
length = (int)file.tellg();
file.seekg (0, ios::beg);
char xmlBuf[ length + 1 ];
file.read( &xmlBuf[0], length );
xmlBuf[length] = '\0';

document.parse<0>(xmlBuf);

すべてのノードは正しい場所にあり、xml_node.value_size() または xml_node.name_size() を使用してクエリを実行すると正しい長さになりますが、実際には名前または値を文字列に取得したり、単に printf() を取得したりすると、多くの値が返されます。次のようなもの:

(........./.(..............-.   <titlK.

他の誰かがこれに遭遇しましたか?

4

2 に答える 2

2

C++ を使用している場合、C++ の方法を使用してみませんか?

int main(int argc, char* argv[]) 
{
  std::string buf;
  std::string line;
  std::ifstream in("file.xml");

  // read file into buf
  while(std::getline(in,line))
    buf += line;

  // After that, take a look on the traverse_xml() function implemented
  // here: http://www.ffuts.org/blog/quick-notes-on-how-to-use-rapidxml/
  // It has great stuff!

  return 0;
}
于 2010-10-01T02:25:04.687 に答える
0

私の問題は、解析宣言を次のように変更しただけのようです。

document.parse<parse_full>();

http://www.ffuts.org/blog/quick-notes-on-how-to-use-rapidxml/を教えてくれた Karl に感謝します。これは優れたリソースです。

于 2010-10-01T13:27:13.733 に答える