0

Hiho,

i guess, it's a pretty stupid question, but i had to switch to C/C++ recently and haven't done this in years. And right now I'm stuck on the following:

Given XML Element as a simple String:

<myns:factor>1000</myns:factor>

I have to parse the string, add the resulting Element to a surrounding MSXML2 DOM object within the same Namespace.

Right now i try it this way:

  HRESULT hr;

  MSXML2::IXMLDOMDocument2Ptr l_xmlFrame;
  MSXML2::IXMLDOMElementPtr l_xmlFrameDoc;

  hr = l_xmlFrame.CreateInstance(__uuidof(MSXML2::DOMDocument));

  if( !FAILED(hr) ) {

    l_xmlFrame->async = VARIANT_FALSE;
    l_xmlFrame->validateOnParse = VARIANT_TRUE;

    // p_strUnit holds the xml as a String
    l_xmlFrame->loadXML(p_strUnit);

  }

The loadXML(...) call just fails , but:

if i remove the namespace declarations and the element looks like this:

<factor>1000</factor>

the call works perfectly!

I really don't understand, why the loadXML function wont parse the string, when the Namespace declarations are set.

Any help appreciated!!!!! :)

Thanks!

3
  • I know for a fact that this does work with MSXML 6.0 (and probably earlier versions too). Could you gives a full XML document? Commented Sep 29, 2011 at 16:14
  • Could you please detail how you set the namespaces? Maybe the error is in there and it was rather complicated to parse namespaced XML even with MSXML6 as I recall. Commented Sep 29, 2011 at 19:56
  • @all: sry, real life intervened. I got a correct answer which actually concerned the setting of the namespaces. X-) ... Thanks anyway! Commented Sep 30, 2011 at 5:49

1 Answer 1

3

The Problem

The string

<myns:factor>1000</myns:factor>

is not well-formed XML (with regard to namespaces). That's why XML parsers generally won't load it.

It's not well-formed because it uses the namespace prefix "myns", which has not been declared.

The Solution

If you changed the XML to something like this, it would parse fine:

<myns:factor xmlns:myns="mynamespaceURI">1000</myns:factor>

The namespace declaration (xmlns:myns="mynamespaceURI") can go on the element where the namespace prefix is used, or any ancestor thereof.

If you can't change the input XML, I would then ask the supplier of the XML, "Why are you giving me broken XML?"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.