Solved

Adding attribute to a parent element

Posted on 2010-11-30
6
949 Views
Last Modified: 2012-08-13
Hi,

I am using libxml2 to generate XML files. In the attached code, I am expecting output to be like:

<TAG_A ATTR_A1="Val1"  ATTR_A2="Val3">
      <TAG_B ATTR_B="Val2"/>
</TAG_A>

But it is not able to add attribute to the parent, and is giving output as:

<TAG_A ATTR_A1="Val1">
      <TAG_B ATTR_B="Val2"/>
</TAG_A>

That is, ATTR_A2 is missing for the element TAG_A. Is there any other API/work around to solve this problem?
#include <libxml/xmlwriter.h>



int main(int argc, char *argv[])

{

    xmlTextWriterPtr writer;

    writer =xmlNewTextWriterFilename("test.xml", 0);

    xmlTextWriterStartElement(writer, "TAG_A");

    xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "ATTR_A1", "Val1");

    xmlTextWriterStartElement(writer, "TAG_B");

    xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "ATTR_B", "Val2");



    xmlTextWriterEndElement(writer);

    xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "ATTR_A2", "Val3");

    xmlTextWriterEndElement(writer);

    xmlFreeTextWriter(writer);

}

Open in new window

0
Comment
Question by:Harisha M G
  • 4
6 Comments
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 34238775
Try using
xmlTextWriterStartAttribute();

have a look at this:
http://xmlsoft.org/html/libxml-xmlwriter.html
0
 
LVL 37

Author Comment

by:Harisha M G
ID: 34239078
I tried that function this way, but it is not working:
#include <libxml/xmlwriter.h>



int main(int argc, char *argv[])

{

    xmlTextWriterPtr writer;

    writer =xmlNewTextWriterFilename("test.xml", 0);

    xmlTextWriterStartElement(writer, "TAG_A");

    xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "ATTR_A1", "Val1");

    xmlTextWriterStartElement(writer, "TAG_B");

    xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "ATTR_B", "Val2");



    xmlTextWriterEndElement(writer);

    xmlTextWriterStartAttribute(writer, BAD_CAST "ATTR_A2");

    xmlTextWriterEndElement(writer);

    xmlFreeTextWriter(writer);

}

Open in new window

0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 0 total points
ID: 34775871
I found that it can not be done.
0
 
LVL 37

Author Comment

by:Harisha M G
ID: 34775895
There is not solution for this. Answer is "it can't be done", as I have posted. Please accept my comment solution and make it PAQ
0
 
LVL 37

Author Closing Comment

by:Harisha M G
ID: 34829979
I found through practical experience that it can't be done
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XML namaspace 2 44
Word files to Wordpress Posts 2 43
Automating receiving uploaded files via HTTPS instead of SFTP 3 20
Transform XML to Excel using XSL 1 23
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now