简体   繁体   中英

how to categorize xml files basing on attributes using libxml2 in c

My assignment is to differentiate xml content in to groups , for example i have two xml files

<?xml version="1.0"?>
<marks>
<firstname>john</firstname>
<lastname>peter</lastname>
<subject name="english" marks="30">pass</heading>
</marks>

<?xml version="1.0"?>
<marks>
<firstname>james</firstname>
<lastname>cameron</lastname>
<subject name="english" marks="30" degree="masters">pass</heading>
</marks>

So i need to parse above example xml files and need to divide them in to groups, and calculate the number of master students, phd students, bachelor students

Each group will have and additional properties in element section, like for masters students subject element" will have an property degree for phd students they will an property of specialization

I can get the properties of the xml files and code it in the c function saying if we have property name degree consider them as masters students but is there any other way round like construct the DTD based on the xml requests and just using libxml2 to validate the DTD and differentiate requests (which i have to figure it out by using libxml2 library, don't know yet)

As the number of different type of xml request increases, i cannot hard code them writing multiple if loops.

it would be possible to just create a dtd for particular type of request and just using one line of code to validate against that request but now sure is it really possible..

request you to give your thoughts.

I understand the question as given by the title: "how to validate xml based on the DTD by using libxml2 xml parse in c". It's quite easy to answer that. It is actually a duplicate of Dtd validation with libxml2 and should be closed for that reason.

But then comes the description and it turns out that the question is quite different. You probably want some help with the design of your project. Maybe you should change the title a bit?

As to your design: why don't you want to use if s? You want it to be possible to change it without recompiling the app? So you need to put down all the cases in a configuration file. Read the file and act accordingly.

Trying to answer the design part:

A separate dtd for each kind of request would also do, but then you would have to validate the xml against all the dtd sequentially. If the performance cost is acceptable, then you may go this way. All the dtds (or xsd schemas) may be read into memory in initialization phase and then be tried against the xml sample until it validates.

To examine whether your xml contains given attribute in given element you may also use XPath from libxml. See this question and XPath examples . Pay attention to xmlXPathEvalExpression usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM