Tag Archive for xml

write and read XML

<?php
session_start();

$_SESSION['arrConfig'];

$docConfigXML = new DOMDocument;
$docConfigXML->preserveWhiteSpace = FALSE;
$docConfigXML->load( 'resources/xml/config.xml' );

$_SESSION['arrConfig']['fax'] = trim($docConfigXML->getElementsByTagName("fax")->item(0)->getAttribute('value'));
$_SESSION['arrConfig']['phone'] = trim($docConfigXML->getElementsByTagName("phone")->item(0)->nodeValue);

echo $_SESSION['arrConfig']['fax'];
echo "<br />";
echo $_SESSION['arrConfig']['phone'];

$strConfigXML = "";

$fax 	= "702-555-1faxxx";
$phone 	= '702-555-phone';

$strConfigXML =
<<<END
<?xml version="1.0" encoding="utf-8"?>
<config>
<fax value='$fax' />
<phone>
$phone
</phone>
</config>
END;

$file = "resources/xml/config.xml";

writeFile($file,$strConfigXML);

function writeFile($file,$strFileContent)
{
$fh = fopen($file, 'w' ) or die ("can't create file");
fwrite($fh, $strFileContent);
fclose($fh);
}
?>

source

Create a Firefox Search Plugin

<search
name="Digital Inspiration"
method="GET"
action="http://www.google.com/search"
queryCharset="utf-8"
>

<input name="q" user>
<input name="sitesearch" value="labnol.blogspot.com">
</search>

source

REXML Language Identification monkey patch

require 'rexml/document'

class REXML::Element

public
def lang
if self.attributes['xml:lang']
return self.attributes['xml:lang'].to_s
elsif self.parent != nil
return self.parent.lang
else
return nil
end
end

end

describe "XML library" do
it "should handle xml:lang inheritance properly" do
xmldoc = <<-EOF;
<?xml version="1.0" ?>
<foo xml:lang="en">
<bar>Hello World!</bar>
</foo>
EOF

xml = REXML::Document.new(xmldoc)
xml.elements[1].elements[1].lang.should == "en"

xmldoc2 = <<-EOF;
<?xml version="1.0" ?>
<foo>
<bar>Hello World!</bar>
</foo>
EOF

xml2 = REXML::Document.new(xmldoc2)
xml2.elements[1].elements[1].lang.should_not == "en"
xml2.elements[1].elements[1].lang.should == nil

xmldoc3 = <<-EOF;
<?xml version="1.0" ?>
<foo>
<bar xml:lang="en">Hello World!</bar>
</foo>
EOF

xml3 = REXML::Document.new(xmldoc3)
xml3.elements[1].elements[1].lang.should == "en"
end
end

source

Some eclipse plugins

<?xml version="1.0" encoding="UTF-8"?>
<bookmarks>
<site name="Subversive Connectors" url="http://download.eclipse.org/technology/subversive/0.7/update-site/" web="false" selected="true" local="false"/>
<site name="Subversive" url="http://www.polarion.org/projects/subversive/download/eclipse/2.0/update-site/" web="false" selected="true" local="false"/>
<site name="Aptana" url="http://update.aptana.com/update/studio/3.2/site.xml" web="false" selected="true" local="false"/>
<site name="PHP Development (PDT) Toolkit" url="http://download.eclipse.org/tools/pdt/updates/" web="false" selected="true" local="false"/>
<site name="Symfoclipse" url="http://noy.cc/symfoclipse" web="false" selected="true" local="false"/>
<site name="Europa Discovery Site" url="http://download.eclipse.org/releases/europa" web="false" selected="false" local="false"/>
</bookmarks>

source

XML Hello World

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<root>
<level0>
<a>1 </a>
<b> 2</b>
<c> 3</c>
<a>5 </a>
<level1>
<l1e>11</l1e>
</level1>
</level0>
<level0>
<a>7 </a>
<b> 7</b>
<b> 2</b>
<c> 3</c>
<a>70 </a>
<level1>
<l1e>44</l1e>
</level1>
</level0>
</root>

source

Set Comos Workload XML

System.Collections.ICollection col = this.GetSpecificationCollection("RES.ADDR");
foreach (IComosDSpecification e in col)
{
e.XMLString = s;
//WrapperDebug.WriteLine("Arbeitszeit " + e.XMLString);
}

source

C# XML Processing

static void Csharp_HelloWorld_XML()
{
XmlDocument doc = new XmlDocument();
XmlNode myRoot;
XmlAttribute myAttribute;

myRoot = doc.CreateElement("HelloXMLWorld");
myRoot.InnerText = "ROOT Element";
doc.AppendChild(myRoot);
myAttribute = doc.CreateAttribute("Attribute1");
myAttribute.InnerText = "AttributeText1";
myRoot.Attributes.Append(myAttribute);
//doc.Save(@"c:helloxmlworld.xml");
Console.WriteLine(doc.OuterXml);

XmlNode connSntringNode = root.SelectNodes(@"//ConnectionString")[0];
}

source

PHP XML 解析 XMLシリアライザーphp4

// XML解析
require_once'../lib/XML/Unserializer.php';
$xml_filename = "http://192.168.11.29/chopsticks/data/bloger_xml.php";
$xml = join("",file($xml_filename)); // xml_str にファイルを読み込み

$Unserializer =& new XML_Unserializer();
$Unserializer->setOption('parseAttributes', TRUE);
$status = $Unserializer->unserialize($xml);
if (PEAR::isError($status)) {
die($status->getMessage());
}
print_r($Unserializer->getUnserializedData());

source

GPath

//GPath can be used to easily query XML-based documents or even Object Graphs.
//We start with looking at an XML GPath example.

//Let's load the Grails Pocast RSS Feed first.
println "new XmlSluper".center(75, '*')
def feed = new XmlSlurper().parse('http://hansamann.podhost.de/rss')
assert feed.getClass().name == 'groovy.util.slurpersupport.NodeChild'

//Like using XPath expressions, we can now navigate the tre
//This accesses the first iem element of the RSS channel:
println "Access to node content".center(75, '*')
println feed.channel.item[0].title
println feed.channel.item[0]['title']

//As in XPath, attributes are accessed with the @ syntax.
println "Access to attribute content".center(75, '*')
println feed.channel.item[0].enclosure.@url
println feed.channel.item[0].enclosure['@url']

//Let's see how many podcasts whe have done so far.
println "item element count".center(75, '*')
println feed.channel.item.size()

//We can iterate over all items with this
println "All titles below".center(75, '*')
feed.channel.item.each { item ->
println item.title
}

//hier will ich die items nach Groovy Series filtern...
//def groovySeries = feed.channel.item.findAll{ return (it.title.indexOf('Groovy Series')) ? true : false;} //wieso geht das nicht... println it.title geht
//println "Found ${groovySeries.size()}"

//playing with methods of GPathResult. NodeChild is an extension of GPathResult
assert feed.channel.item[0].enclosure[0].getClass().name == 'groovy.util.slurpersupport.NodeChild'

//to call the attributes method, we neet to macke sure that we are operating on a single Node, not a set
feed.channel.item[0].enclosure[0].attributes().each{println it} //works
try {
feed.channel.item[0].enclosure.attributes().each{println it}
} catch (MissingMethodException e)
{
assert feed.channel.item[0].enclosure.getClass().name == 'groovy.util.slurpersupport.NodeChildren'
//above returns a NodeChildren object as it is a path expression for all enclosure tags of the first item tag!
}

//let's print all children of an item.
println "All children of item[0]".center(75, '*')
feed.channel.item[0].children().each { println it.name() } //will print tag names contained in item: title, description, author, pubDate, guid, link, enclosure
assert feed.channel.item[0].children().size() == 7

//Beispiel mit GPathResult->find / findAll

//evtl. xmlslurper dazu verwenden ein xml-doc zu veraendern??

source

dump SQL data into an xml file

python manage.py dumpdata --format=xml app_name > apps/app_name/initial_data.xml

source