C++请求Web Service与XML解析(2)

TiXmlElement *RootElement = xmlDocument->RootElement();
 TiXmlElement *fileHeaderElement = RootElement->FirstChildElement();
 TiXmlElement *fileBodyElement = fileHeaderElement->NextSiblingElement();

for (TiXmlElement *nodeElement = fileHeaderElement->FirstChildElement(); nodeElement; nodeElement = nodeElement->NextSiblingElement())
 {
  string strElementKey = nodeElement->Value();
  if (strElementKey.compare(XML_PARSE_CREATION_TIME) == 0)
  {
   strCreationTime = nodeElement->GetText();
  }
 }

for (TiXmlElement *nodeElement = fileBodyElement->FirstChildElement(); nodeElement; nodeElement = nodeElement->NextSiblingElement())
 {
  string strElementKey = nodeElement->Value();
  if (strElementKey.compare(XML_PARSE_JOB_ID) == 0)
  {
   strJobId = nodeElement->GetText();
  }

if (strElementKey.compare(XML_PARSE_JOB_TYPE) == 0)
  {
   strJobType = nodeElement->GetText();
  }

if (strElementKey.compare(XML_PARSE_JOB_NAME) == 0)
  {
   strJobName = nodeElement->GetText();
  }

if (strElementKey.compare(XML_PARSE_JOB_LEADER) == 0)
  {
   strJobLeader = nodeElement->GetText();
  }
 }

2) 创建、生成XML文件
以如下XML文件为例:

<?xml version=”1.0” encoding=”gb2312”>
<InterFaceFile>
    <FileHeader>
          <MessageType>ProxyMiddleWareJobSearch</MessageType>
          <Originator>WetLand</Originator>
    <Recipient>Platform</Recipient>
          <CreationTime>2014-9-18 10:25:20</CreationTime>
    </FileHeader>
    <FileBody>
          <UserName>...</UserName>
          <UserRoleID>13</UserRoleID>
          <JobType></JobType>
    </FileBody>
</InterFaceFile>

//! 构建XML字符串
 TiXmlDocument *pDoc = new TiXmlDocument;
 TiXmlDeclaration *pDeclaration = new TiXmlDeclaration("1.0", "gb2312", "");
 pDoc->LinkEndChild(pDeclaration);

TiXmlElement *pEleRoot = new TiXmlElement("InterFaceFile");
 pDoc->LinkEndChild(pEleRoot);

TiXmlElement *pEleFileHeader = new TiXmlElement("FileHeader");
 TiXmlElement *pEleFileBody = new TiXmlElement("FileBody");
 pEleRoot->LinkEndChild(pEleFileHeader);
 pEleRoot->LinkEndChild(pEleFileBody);

TiXmlElement *pEleMessageType = new TiXmlElement("MessageType");
 TiXmlElement *pEleOriginator = new TiXmlElement("Originator");
 TiXmlElement *pEleRecipient = new TiXmlElement("Recipient");
 TiXmlElement *pEleCreationTime = new TiXmlElement("CreationTime");
 pEleFileHeader->LinkEndChild(pEleMessageType);
 pEleFileHeader->LinkEndChild(pEleOriginator);
 pEleFileHeader->LinkEndChild(pEleRecipient);
 pEleFileHeader->LinkEndChild(pEleCreationTime);

TiXmlText *pEleMessageTypeText = new TiXmlText("ProxyMiddleWareJobSearch");
 TiXmlText *pEleOriginatorText = new TiXmlText("WetLand");
 TiXmlText *pEleRecipientText = new TiXmlText("Platform");
 TiXmlText *pEleCreationTimeText = new TiXmlText("2014-9-18 10:25:20");

pEleMessageType->LinkEndChild(pEleMessageTypeText);
 pEleOriginator->LinkEndChild(pEleOriginatorText);
 pEleRecipient->LinkEndChild(pEleRecipientText);
 pEleCreationTime->LinkEndChild(pEleCreationTimeText);

//!
 TiXmlElement *pEleUserName = new TiXmlElement("UserName");
 TiXmlElement *pEleUserRoleID = new TiXmlElement("UserRoleID");
 TiXmlElement *pEleJobType = new TiXmlElement("JobType");
 pEleFileBody->LinkEndChild(pEleUserName);
 pEleFileBody->LinkEndChild(pEleUserRoleID);
 pEleFileBody->LinkEndChild(pEleJobType);

TiXmlText *pEleUserNameText = new TiXmlText("...");
 TiXmlText *pEleUserRoleIDText = new TiXmlText("13");
 TiXmlText *pEleJobTypeText = new TiXmlText("");

pEleUserName->LinkEndChild(pEleUserNameText);
 pEleUserRoleID->LinkEndChild(pEleUserRoleIDText);
 pEleJobType->LinkEndChild(pEleJobTypeText);

pDoc->SaveFile("E:/houqd.xml");

3) 生成XML字符串
生成XML字符串只需要将如上的pDoc->SaveFile(“E:/houqd.xml”)替换为如下:

//!生成string字符串
 TiXmlPrinter printer;
 pDoc->Accept(&printer);

string strRequest(printer.CStr()); 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/e98e9ab534dd92a62722080e0ff623fa.html