Delete XML nodes using Delphi

Source: Internet
Author: User

I encountered a problem and couldn't delete it. What happened to the problem ???


When deleting a node, You need to locate the node to be deleted. Before locating this node, many nodes may be of the same level, that is, you need to use the value of the node to locate. Before locating a node of the same level, you need to locate the parent node of the node to be deleted, that is, you only need to locate the parent node of the node to be deleted, and then search for the node to be deleted based on the node value.



Find an algorithm on the Internet.


(2) Delete A node
Function removenode (xmlname: string; proid: integer): integer;
VaR
Xmldoc: ixmldocument;
Rootnode, cNode, nnode: ixmlnode;
Ivalue, ireturn: integer;
Svalue: string;
Begin
Result: =-1;
If not fileexists (xmlname)
Then exit;
Xmldoc: = txmldocument. Create (NiL );
Xmldoc. Active: =
True; ireturn: =-1;
Try
Try

Xmldoc. loadfromfile (xmlname );

Xmldoc. encoding: = 'gb2312 ';

Rootnode: = xmldoc. childnodes. findnode ('document ');

CNode: = rootnode. childnodes. findnode ('product ');

If cNode <> nil then

Begin

Ivalue: = cNode. attributes ['proid'];

While (proid <> ivalue) and (cNode <> nil)
Do // find

Begin

Nnode: = rootnode. childnodes. findsibling (cNode, 1 );

CNode: = nnode;

If cNode <> nil then ivalue: = cNode. attributes ['proid'];

End;


End;

If cNode <> nil then
Rootnode. childnodes. Remove (cNode); // locate
, Delete


Xmldoc. savetofile (xmlname );

Ireturn: = 0;
Except

Ireturn: =-1;
End;
Finally
Xmldoc: =
Nil;
Result: =
Ireturn;
End;
End;


However, it is only necessary to make a transformation, and there is no Versatility at all. The person who designed this algorithm is really lazy. In addition, I still found many similar algorithms on pudn, but forget it. Just give it a try.

In addition, I am suddenly quite interested in XML grammar. I also need to design one by myself. In addition, the algorithm is not complex. You can use the stack to complete the pairing check of all nodes. Then the node can be stored in a tree. The root node corresponds to the first node, and the bottom is the leaf.

 

------------ I am a gorgeous split line -------------

<? XML version = "1.0" encoding = "gb2312"?>
<Params>
<Fileoperate>
<Setting>
<Type> 0 </type>
<Savepath> C:/u_0127 </savepath>
</Setting>
<History>
<Path> C:/u_0127/demo000002.csv </path>
<Path> C:/u_0127/demo000362.csv </path>
<Path> C:/u_0127/demo000362.csv </path>
<Path> C:/u_0127/demo000360.csv </path>
<Path> C:/u_0127/demo000362.csv </path>
<Path> C:/u_0127/demo000361.csv </path>
</History>
</Fileoperate>
</Params>
----------------- I am a gorgeous split line --------------

 

For example, in the XML above, after the syntax check, the generated tree should be like the following, and the linked list is used to store each node.

Param --
| Fileoperate --
| Setting --
| Type (0)
| Savepath (C:/u_0127)
| History --
| Path (C:/u_0127/demo000002.csv)
| Path (C:/u_0127/demo000362.csv)
| Path (C:/u_0127/demo000362.csv)
| Path (C:/u_0127/demo000360.csv)
| Path (C:/u_0127/demo000362.csv)
| Path (C:/u_0127/demo000361.csv)

 

 

In this way, you can perform various operations on the tree: Create an XML tree, add node/node attributes, query node values/attribute values, modify node values/attribute values, and delete node/node attributes, read the XML file and create the XML tree,

Output XML tree to file

.



 

 

----------------------- I am a gorgeous split line ----------------

For the XML file above, the algorithm to delete a node is:

First, determine the path of the node to be deleted, for example

<Path> C:/u_0127/demo000360.csv </path>,

The path is Params-> fileoperate-> history.

Then, find the child node of the node history, as long as the value is

C:/u_0127/demo000360.csv, you can delete it.

The deleted XML file is:

---------------- I am a gorgeous split line --------------

<? XML version = "1.0" encoding = "gb2312"?>
<Params>
<Fileoperate>
<Setting>
<Type> 0 </type>
<Savepath> C:/u_0127 </savepath>
</Setting>
<History>
<Path> C:/u_0127/demo000002.csv </path>
<Path> C:/u_0127/demo000362.csv </path>
<Path> C:/u_0127/demo000362.csv </path>
<Path> C:/u_0127/demo000362.csv </path>
<Path> C:/u_0127/demo000361.csv </path>
</History>
</Fileoperate>
</Params>
----------------- I am a gorgeous split line --------------

This completes a complete delete node operation.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.