为什么用XML接口来写xml还会出现不完整的元素?

蒋晟 2002-01-30 05:15:07
生成的文档:
<?xml version="1.0" encoding="gb2312" standalone="yes"?>
<FLEXREPORT><PAGE/></FLEXREPORT>
代码:
BOOL CFlexReportDoc::SaveToXMLDocument(LPCTSTR lpszPathName)
{
CFlxRptPage pageDefault;
GetDefaultsPageSetting(pageDefault);//get default setting
//if setting==property,skip attribute

HRESULT hr ;
CXMLDOMDocument2 pXMLDOMDocument2;
//
// Create an empty XML document.
//
try{
if(!pXMLDOMDocument2.CreateDispatch("Msxml2.DOMDocument.4.0"){
AfxMessageBox("Unable to create XML parser object");
return FALSE;
}
pXMLDOMDocument2.SetAsync(FALSE);
TRACE("Xml:%s\n",pXMLDOMDocument2.GetXml());
//output:Xml:
CXMLDOMElement pXmlElement=pXMLDOMDocument2.createProcessingInstruction(
"xml","version='1.0' encoding='gb2312' standalone='yes'");
pXMLDOMDocument2.appendChild(pXmlElement);
TRACE("Xml:%s\n",pXMLDOMDocument2.GetXml());
//output:Xml:<?xml version="1.0" standalone="yes"?>

CXMLDOMElement pRootElement=pXMLDOMDocument2.createElement("FLEXREPORT");
pXMLDOMDocument2.appendChild(pRootElement);


TRACE("Xml:%s\n",pXMLDOMDocument2.GetXml());
//output:Xml:<?xml version="1.0" standalone="yes"?><FLEXREPORT/>

CXMLDOMElement pPageElement=pXMLDOMDocument2.createElement("PAGE");
pRootElement.appendChild(pPageElement);
TRACE("Xml:%s\n",pXMLDOMDocument2.GetXml());
//output:Xml:<?xml version="1.0" standalone="yes"?><FLEXREPORT><PAGE/></FLEXREPORT>

_variant_t varString = lpszPathName;
pXMLDOMDocument2.save(varString);
}
catch(COleDispatchException*e){
e->ReportError();
e->Delete();
}

return TRUE;
}
...全文
160 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
蒋晟 2002-01-31
  • 打赏
  • 举报
回复
搞定了,用GetDocumentElement,然后遍历子节点树
但是现在还不知道怎么用Xpath访问:(
LLnju 2002-01-31
  • 打赏
  • 举报
回复
你的 selectSingleNode("PAGE") 用法好像不怎么地道,参考参考msdn。实在不行,你还可以遍历所有结点嘛。
为什么不用 ATL 封装的类,MFC的难看死了。

selectSingleNode Method [C/C++]
Applies the specified pattern-matching operation to this node's context and returns the first matching node.

Remarks
For a complete description of XML Path Language (XPath) syntax, see XPath Syntax.

The selectSingleNode method is similar to the selectNodes method, but returns only the first matching node rather than the list of all matching nodes.

This member is an extension of the Worldwide Web Consortium (W3C) Document Object Model (DOM).


XPath Examples
This topic reviews the examples that appear throughout this documentation. All are based on the Sample Data.

Find all author elements within the current context.

./author
Note that this is equivalent to the following.

author
Find all first.name elements.

first.name
Find the document element (bookstore) of this document.

/bookstore
Find all author elements that are children of the current context node (for example, /bookstore/book).

//author
Find all bookstores where the value of the specialty attribute is equal to "textbooks".

/bookstore[@specialty = "textbooks"]
Find all books where the value of the style attribute on the book is equal to the value of the specialty attribute of the bookstore element at the root of the document.

book[/bookstore/@specialty = @style]
Find all first-name elements within an author element. Note that the author children of the current context are found, and then first-name children are found relative to the context of the author elements.

author/first-name
Find all title elements one or more levels deep in the bookstore (arbitrary descendants).

bookstore//title
Note that this is different from the following pattern, which finds all title elements that are grandchildren of bookstore elements.

bookstore/*/title
Find emph elements anywhere inside book excerpts, anywhere inside the bookstore.

bookstore//book/excerpt//emph
Find all titles one or more levels deep in the current context. Note that this situation is essentially the only one in which the period notation is required.

.//title
Find all element children of author elements.

author/*
Find all last names that are grandchildren of books.

book/*/last-name
Find the grandchildren elements of the current context.

*/*
Find the book element from the "my" namespace.

my:book
Find all elements from the "my" namespace.

my:*
Find all elements with the specialty attribute.

*[@specialty]
Find the style attribute of the current element context.

@style
Find the exchange attribute on price elements within the current context.

price/@exchange
The following example does not return anything because attributes do not contain element children. It is allowed by the XML Path Language (XPath) grammar, but is not strictly valid.

price/@exchange/total
Find all books with style attributes.

book[@style]
Find the style attribute for all book elements.

book/@style
Find all attributes of the current element context.

@*
Find all attributes from the "my" namespace. This does not include unqualified attributes on elements from the "my" namespace.

@my:*
Find all first-name elements. The following examples are equivalent.

./first-name
first-name
Find all unqualified book elements.

book
For example, the following finds the first author element.

author[1]
The following finds the third author element that has a first-name.

author[first-name][3]
Note that indexes are relative to the parent. Consider the following data.

蒋晟 2002-01-30
  • 打赏
  • 举报
回复
// Machine generated IDispatch wrapper class(es) created with ClassWizard
/////////////////////////////////////////////////////////////////////////////
// CXMLDOMNode wrapper class

class CXMLDOMElement : public COleDispatchDriver
……
selectSingleNode 好像要一个XPath路径?
kimryo 2002-01-30
  • 打赏
  • 举报
回复
CXMLDOMElement是你自己包的?
蒋晟 2002-01-30
  • 打赏
  • 举报
回复
Class Wizard->Add Class->From A Type Library->msxml4.dll
CXMLDOMxxx都是COleDispatchDriver派生出来的
那我怎么得到<PAGE>这个节点?
kimryo 2002-01-30
  • 打赏
  • 举报
回复
不知那个pPage.m_lpDispatch是干什么的东西。
<farther>
<child>ty54t4wetfew</child>
</farther>

如是取得节点间的text话,farther见只有节点,没有text。

还有,你的各个element的声明呢???
蒋晟 2002-01-30
  • 打赏
  • 举报
回复
<?xml version='1.0'?>
<FLEXPEPORT>
<PAGE>
<RH>some text
</RH>
<PH>some text
</PH>
<B>some text
</B>
<RF>some text
</RF>
<PH>some text
</PH>
</PAGE>
这样的也读不出来啊
</FLEXPEPORT>
这样的也读不出来啊
蒋晟 2002-01-30
  • 打赏
  • 举报
回复
空节点读不出来?
lhj 2002-01-30
  • 打赏
  • 举报
回复
<PAGE/>表示空节点
蒋晟 2002-01-30
  • 打赏
  • 举报
回复
那偶怎么读不出来?
COleVariant var=lpszPathName;
pXMLDOMDocument2.load(var);
CXMLDOMElement pPage=pXMLDOMDocument2.selectSingleNode("PAGE");
if(pPage.m_lpDispatch){//这里没有读到<PAGE>啊
.....
}
lhj 2002-01-30
  • 打赏
  • 举报
回复
<PAGE>这里没有内容</PAGE>
则可以<PAGE/>
kimryo 2002-01-30
  • 打赏
  • 举报
回复
写成<PAGE/> IE编译没问题。

判断XML是否legal,拿给IE编译一下就知。
LLnju 2002-01-30
  • 打赏
  • 举报
回复
这个问题你竟然给了200分,满大方的,还没注意,呵呵呵呵,大家有空帮我看看:
分数由你定: http://www.csdn.net/expert/topic/503/503163.shtm
LLnju 2002-01-30
  • 打赏
  • 举报
回复
这是正确的写法,你可以查查任何讲xml的书,它都会这样告诉你,像这种没有正文的结点
<tagname attr .... ></tagname> 是可以简写为<tagname attr .... />的,M$别的不咋的,这个写法很标准的。
蒋晟 2002-01-30
  • 打赏
  • 举报
回复
<PAGE/>
<FLEXREPORT/>
:(
应该是<PAGE></PAGE>这样的


LLnju 2002-01-30
  • 打赏
  • 举报
回复
哪个元素不完整,没发现啊

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧