How to generate XML file using libxml2? - xsd

I have downloaded libxml2 from xmlsoft.org. I used this for parsing an xml file. Now I want to create my own xml file using the same libxml like this...
<?xml version="1.0"?>
<Powersettings>
<PowerScheme>Testing</PowerScheme>
<CPUSpeed>Adaptive</CPUSpeed>
<Screensaver>No</Screensaver>
<LockScreentime>0</LockScreentime>
<Shutdownflag>Yes</Shutdownflag>
<DownloadsFlag>No</DownloadsFlag>
<PPasswordprotected>No</PPasswordprotected>
<Appsensorname>(None)</Appsensorname>
</Powersettings>
I am using gcc compiler, C language to write program and whole data is in a Structure.
I'm looking for a Good tutorial or an example program to create xml document as above using libxml2.
Any help ?

Based on http://xmlsoft.org/examples/testWriter.c -- disclaimer: I have never used libxml2, haven't tested it, and there's no error detection:
xmlTextWriterPtr writer;
writer = xmlNewTextWriterFilename("test.xml", 0);
xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
xmlTextWriterStartElement(writer, "Powersettings");
xmlTextWriterWriteElement(writer, "PowerScheme", "Testing"); xmlTextWriterEndElement(writer);
xmlTextWriterWriteElement(writer, "CPUSpeed", "Adaptive"); xmlTextWriterEndElement(writer);
/* ... etc ... */
xmlTextWriterEndElement(writer);
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);

Related

How to HTML entity encode / decode in ballerina?

Currently I'm having a requirement of HTML entity encode / decode using Ballerina language. However I couldn't find any references for that.
If I have a string as following,
https://spdx.org/licenses/Apache-2.0.html
Expected result would be,
https://spdx.org/licenses/Apache-2.0.html
You can refer to the following screenshot where I have done the same thing using an online tool : https://mothereff.in/html-entities
How can we achieve the above?
There is no direct Ballerina module that provides this functionality. But it can be done using the Java interoperability feature in the Ballerina language.
The StringEscapeUtils class of Apache common-text library can be used for this.
Steps:
Create a new Ballerina project using bal new <project name> command.
Generate Ballerina bindings for StringEscapeUtils using below command.
bal bindgen -mvn org.apache.commons:commons-text:1.9 org.apache.commons.text.StringEscapeUtils
Build the project using bal build command.
Write the Ballerina code for encode and decode.
import ballerina/io;
import htmlTest.org.apache.commons.text as text;
public function main() {
string? decodedText = text:StringEscapeUtils_unescapeHtml3("https://spdx.org/licenses/Apache-2.0.html");
if decodedText is string {
io:println(decodedText); //Output : https://spdx.org/licenses/Apache-2.0.html
} else {
io:println("Error occured during conversion");
}
}
For more details refer to the Java interoperability guide on Ballerina.

Where is the source for parses ACL?

I'm looking at the parse server that was released, and looking at the spec I see a reference to methods:
equal(object.getACL().getReadAccess(user), true);
equal(object.getACL().getWriteAccess(user), true);
equal(object.getACL().getPublicReadAccess(), false);
equal(object.getACL().getPublicWriteAccess(), false);
ok(object.get("ACL"));
I searched the repo and I don't see any ACL related classes and I couldn't find a relevant library in package.json that it might be using.
Where is it getting this functionality from?
Reference:
https://github.com/ParsePlatform/parse-server/blob/master/spec/ParseACL.spec.js
It comes from the parse package in NPM. See here for the method definition: https://github.com/ParsePlatform/Parse-SDK-JS/blob/37f433152031cdb5c782d12fbcead2c97c49e0cf/src/ParseObject.js#L862
And here is the full ParseACL file: https://github.com/ParsePlatform/Parse-SDK-JS/blob/37f433152031cdb5c782d12fbcead2c97c49e0cf/src/ParseACL.js

C# get line number with extracted comment block

I want to extract the line number with the comment line from the source code with the codes below
foreach (Match match in re.Matches(FileText))
{
StackFrame CallStack = new StackFrame(0, true);
sb.Append(match.ToString() + CallStack.GetFileLineNumber() + System.Environment.NewLine);
}
return sb.ToString();
How do I capture each comment with line number e.g. /* Test Comment */ Line: 50
There is no "docs" in the assembly. The docs are generated in form of an XML file and should be distributed along with the assembly. From the call stack, you can get the names of the classes and methods. If you know where the XML files are sitting, then you can for example refer to http://jimblackler.net/blog/?p=49 they have the reading of docs per a method mostly done.
However, this is NOT the way the .Net does such things. For having extra compile-time annotations that will survive the compilation and that will be present during the runtime, the .Net allows you to introduce CustomAttributes that can be applied over methods, classes, fields, properties, enums, (....). See that link, look at the example of "Author" attribute and consider changing the magic comment into an attribute. This is the normal way of doing it in whole .Net, not only C#.
Once you read the MethodInfo from the callstack, you can invoke GetCustomAttributes on it, and read the data that you have written in them, see http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.aspx

how to read XML file using xml reader?

Question 1: Assume that i am reading XmlNodeType.Text and I would like to know its tag node name. How do you do that without moving cursor up or down? Also How can I know parent tag of current node tag?
Question 2: Assume that I am reading xml file and I would like to start at particular node tag. How can do that?
Question 3: if you have xsd file, is there easy way to upload xml file? I am using C# 3.5 .net and sql server 2008.
This is what i wrote so far:
XmlTextReader reader = new XmlTextReader("datafile.xml");
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
Console.Write(reader.Name);
}
else if (reader.NodeType == XmlNodeType.Text)
{
Console.Write("/"+reader.Name+"/" + reader.Value+"/");
}
else
{
if (reader.NodeType == XmlNodeType.EndElement)
{
Console.WriteLine(reader.Name);
Console.ReadLine();
}
}
}
reader.Close();
Please let me know if you need more clarification
XmlReader is stateless and only retains information about the current node, so if you are reading the content of an element and wish to know the elements name you need to make sure that when you read the start element node you somehow retain the element name.
Again if you want to know the name of the parent element you need to retain this information / state yourself as you read through the xml document.
If you wish to start reading at a particular node you should go through and read the xml document node by node until you read the node you wish to start at.
Ultimately reading xml via the XmlReader class is more difficult than the alternatives, generally speaking you would only use XmlReader if the the xml document is very large, in most other cases using one of the alternatives:
Linq to XML
The XmlDocument class
Using XSD.exe to generate a .Net class from a XSD file that can be used to serialise and deserialise xml via the XmlSerializer class.
For more information see XML Serialization in the .NET Framework
If you really want to use XmlReader then you should read Using the XmlReader Class .

JAXB Unable To Handle Attribute with Colon (:) in name?

I am attempting to use JAXB to unmarshall an XML files whose schema is defined by a DTD (ugh!).
The external provider of the DTD has specified one of the element attributes as xml:lang:
<!ATTLIST langSet
id ID #IMPLIED
xml:lang CDATA #REQUIRED
>
This comes into the xjc-generated class (standard generation; no *.xjb magic) as:
#XmlAttribute(name = "xml:lang", required = true)
#XmlJavaTypeAdapter(NormalizedStringAdapter.class)
protected String xmlLang;
However, when unmarshalling valid XML files with JAXB, the xmlLang attribute is always null.
When I edited the XML file, replacing xml:lang with lang and changed the #XmlAttribute to match, unmarshalling was successful (i.e. attributes were non-null).
I did find this http://old.nabble.com/unmarshalling-ignores-element-attribute-%27xml%27-td22558466.html. But, the resolution there was to convert to XML Schema, etc. My strong preference is to go straight from an un-altered DTD (since it is externally provided and defined by an ISO standard).
Is this a JAXB bug? Am I missing something about "namespaces" in attribute names?
FWIW, java -version = "build 1.6.0_20-b02" and xjc -version = "xjc version "JAXB 2.1.10 in JDK 6""
Solved the issue by changing replacing xml: with a namespace declaration in the JAXB-generated class:
#XmlAttribute(name = "lang", namespace="http://www.w3.org/XML/1998/namespace", required = true)
Which makes sense, in a way.
Without this kind of guidance, how would JAXB know how to interpret the otherwise-undefined namespace xml:? Unless, of course, it implemented some special-case internal handling to xml: as done in http://java.sun.com/javase/6/docs/api/javax/xml/stream/XMLStreamReader.html#getNamespaceURI%28java.lang.String%29 (see the first NOTE:)
Whether it's a bug in xjc's generation of the annotated objects or a bug in the unmarhaller, or simply requires a mapping somewhere in the xjc process is still an open question in my mind.
For now, it's working and all it requires is a little xjc magic, so I'm reasonably happy.
Disclaimer: Although 8 years late, I am adding this answer for lost souls such as myself trying to understand auto generation of java files from a DTD.
You can set project wide namespaces for the unmarshaller to work with directly in the project-info.java file via the #XmlSchema option.
This file should be automatically generated by xjc when generating classes from a schema, however it appears xjc does not automatically generate the package-info.java file when generating from a DTD!
However, you can manually make this file, and add it to the same package as the files generated by xjc.
The file would look like the following:
package-info.java :
#XmlSchema(
elementFormDefault=XmlNsForm.QUALIFIED,
xmlns = {
#XmlNs(prefix="xlink", namespaceURI="http://www.w3c.org/1999/xlink"),
#XmlNs(prefix="namespace2", namespaceURI="http://www.w3c.org/1999/namespace2")
})
package your.generated.package.hierarchy;
import javax.xml.bind.annotation.*;
You can add as many namespaces as required, simply add a new line in the form:
#XmlNs(prefix="namespace", namespaceURI="http://www.uri.to.namespace.com")
The benefit of doing it this way, rather than compared to editing the generated #XmlAttribute is that you do not need to change each generated XmlAttribute, and you do not need to manually remove the namespaces from the XmlAttribute name variable.

Resources