0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MSXML(その2)

Posted at

なんかひらめいた(別に大したことではない)ので、やってみたらできた。
※いったい何に苦戦していたのか謎すぎる。。。

やりたかったこと
理想はタグ単位で改行してほしいけど、そこまでは最悪いいかな。。。

save.xml
<?xml version="1.0" encoding="UTF-8"?>
<Content><header><item1>item1</item1></header><body><item2>item2</item2></body></Content>

ソース

test.cpp

#include "stdafx.h"
#include "resource.h"

#include <iostream> // ostream
#include <string>   // string
#include <vector>   // vector
#include <cassert>  // assert
#include <cstdlib>  // atoi, _itoa
#include <atlacc.h>
#include <atlcom.h>
#include <atlcomcli.h>


#import "msxml3.dll" named_guids raw_interfaces_only

bool AssembledData()
{
	IXMLDOMDocumentPtr pDoc("MSXML2.DOMDocument.3.0");
	IXMLDOMProcessingInstructionPtr pPi;
	pDoc->createProcessingInstruction(bstr_t("xml"), bstr_t("version='1.0' encoding='UTF-8'"), &pPi);
	pDoc->appendChild(pPi, NULL);

	IXMLDOMNodePtr     pNodeContent;
	IXMLDOMNodePtr     pNodeHeader;
	IXMLDOMNodePtr     pNodeHeaderItem1;
	IXMLDOMTextPtr     pTextItem1;
	IXMLDOMNodePtr     pNodeBody;
	IXMLDOMNodePtr     pNodeBodyItem2;
	IXMLDOMTextPtr     pTextItem2;

	
	//NG:BSTRだと文字化けする。理由は謎
	//pDoc->createNode(_variant_t(NODE_ELEMENT), BSTR("Content"), BSTR(""), &pNodeContent);
	//OK:_bstr_tだと文字化けしないっぽい
	pDoc->createNode(_variant_t(NODE_ELEMENT), _bstr_t("Content"), _bstr_t(""), &pNodeContent);		//<Content>を作成
	pDoc->appendChild(pNodeContent,NULL);															//XMLに<Content>を追加

	pDoc->createNode(_variant_t(NODE_ELEMENT), _bstr_t("header"), _bstr_t(""), &pNodeHeader);		//<header>を作成
	pNodeContent->appendChild(pNodeHeader, NULL);													//<Content>に<header>を追加

	pDoc->createNode(_variant_t(NODE_ELEMENT), _bstr_t("item1"), _bstr_t(""), &pNodeHeaderItem1);	//<item1>を作成
	pDoc->createTextNode(_bstr_t("item1"), &pTextItem1);											//<item1>に設定するデータを作成
	pNodeHeaderItem1->appendChild(pTextItem1, NULL);												//<item1>にデータを追加
	pNodeHeader->appendChild(pNodeHeaderItem1, NULL);												//<header>に<item1>を追加

	pDoc->createNode(_variant_t(NODE_ELEMENT), _bstr_t("body"), _bstr_t(""), &pNodeBody);
	pNodeContent->appendChild(pNodeBody, NULL);

	pDoc->createNode(_variant_t(NODE_ELEMENT), _bstr_t("item2"), _bstr_t(""), &pNodeBodyItem2);		//<item2>を作成
	pDoc->createTextNode(_bstr_t("item2"), &pTextItem2);											//<item2>に設定するデータを作成
	pNodeBodyItem2->appendChild(pTextItem2, NULL);													//<item2>にデータを追加
	pNodeBody->appendChild(pNodeBodyItem2, NULL);													//<body>に<item2>を追加

	pDoc->save(_variant_t("save.xml"));

	return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
	CoInitialize(NULL);	// COMの初期化処理

	AssembledData();

	CoUninitialize();	// COMの終了処理

	return 0;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?