• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

XML questions - Libxml2

cabri

Diamond Member
Been handed a project that is tied with Libxml2

Issue is that there are existing XMLs that have become outdated and need to have additional fields added to support new hardware. Those field are expected to be added in each "block/group" However, the blocks/groups are not delineated but grouped by the first 6 characters label and 2 digit number itself

Sample of the XML being used.

Code:
<aspusystem devicetype="ASPU" vercreated="1.0.1.0" version="100" verupdated="1.0.1.0">
<Alert1Interrupting>0</Alert1Interrupting>
<Alert1InterruptibleType>0</Alert1InterruptibleType>
<Alert2Interrupting>0</Alert2Interrupting>
<Alert2InterruptibleType>0</Alert2InterruptibleType>
...
<Alert#Interrupting>0</Alert#Interrupting>
<Alert#InterruptibleType>0</Alert#InterruptibleType>
</aspusystem



I want to insert an XML line within each block as shown below
Bolding is my doing for emphasis.

Code:
<aspusystem devicetype="ASPU" vercreated="1.0.1.0" version="100" verupdated="1.0.1.0">
<Alert1Interrupting>0</Alert1Interrupting>
[COLOR="red"][B]<Alert1Immediate>0</Alert1Immediate>[/B][/COLOR]
<Alert1InterruptibleType>0</Alert1InterruptibleType>
<Alert2Interrupting>0</Alert2Interrupting>
[COLOR="red"][B]<Alert2Immediate>0</Alert2Immediate>[/B][/COLOR]
<Alert2InterruptibleType>0</Alert2InterruptibleType>
...
<Alert#Interrupting>0</Alert#Interrupting>
[COLOR="Red"][B]<Alert#Immediate>0</Alert#Immediate>[/B][/COLOR]
<Alert#InterruptibleType>0</Alert#InterruptibleType>
</aspusystem>

If I use the xmlNewChild the fields are appended to the end of the file.
Functionally, this will work, but cosmetically, it is very confusing to anyone that views the XML file.
Code:
xmlNodePtr newNode = NULL;
newNode = xmlNewChild(RootNode,
			NULL,
			xSTRCHR(sAlertField),
			xSTRCHR(sContent));


so if I use the xmlAddNextSibling the first insertion happens,

Code:
xmlNodePtr nodeRef = GetNode(sInsertionReference);

xmlNodePtr newNode = NULL;
newNode = xmlNewChild(RootNode,
			NULL,
			xSTRCHR(sAlertField),
			xSTRCHR(sContent));

newNode = xmlAddNextSibling (nodeRef,newNode);
but the XML output gets truncated as shown
Code:
<aspusystem devicetype="ASPU" vercreated="1.0.1.0" version="100" verupdated="1.0.1.0">
<Alert1Interrupting>0</Alert1Interrupting>
[COLOR="red"][B]<Alert1Immediate>0</Alert1Immediate>[/B][/COLOR]
</aspusystem>

what am I missing to get the insertion to happen and avoid the truncation.

There is only one parent/root
If I use the Sibling vs Child will that help?

I am unable to locate information that explains the difference/use of the two.
 
I think your basic problem is that you have a logical grouping that isn't reflected in the XML's structure. I have only used the libxml python wrapper, but I doubt that you can enforce order among siblings. I'm not saying that if you insert nodes into an empty parent in a certain order and then write the whole thing to a file that the order will change. I think it could change, but in that case it probably won't. In your case you're trying to insert nodes into a specific slot between two siblings.

I would probably either read the whole tree in, and then walk the nodes out of it into a new tree in order, doing the insertions based on node name as I go, or just handle it line by line as a text file.
 
I think your basic problem is that you have a logical grouping that isn't reflected in the XML's structure. I have only used the libxml python wrapper, but I doubt that you can enforce order among siblings. I'm not saying that if you insert nodes into an empty parent in a certain order and then write the whole thing to a file that the order will change. I think it could change, but in that case it probably won't. In your case you're trying to insert nodes into a specific slot between two siblings.

I would probably either read the whole tree in, and then walk the nodes out of it into a new tree in order, doing the insertions based on node name as I go, or just handle it line by line as a text file.

There is a base template for the new XML file.

Last night I figured that I will write out the new "proper/empty of data" file, load it in and then populate it with the original copy in memory. then write out the modified structure into the original file that was to be altered.
 
Back
Top