Asked  7 Months ago    Answers:  5   Viewed   29 times

I'm wondering how to parse values in XML that appear to have : in their name. I've been using:

$response   = file_get_contents($url);
$data = simplexml_load_string($response);

then doing a:

foreach($data->item as $key => $current){

However, one of the latest feeds that I've been given has colons in the name of the feed as seen in the example below:


When i try to create a $current->ccc:bid php does not get to happy (rightfully so). Is there any way to get around this?



the usage of ccc:fid is an extension of the namespace which needs to be declared in xml in order to be able to use it in libraries like simplexml.

Here are some examples of descriptions of usin a namespace:


Hope that helps, albeit it is a little complicated.

Wednesday, March 31, 2021
answered 7 Months ago

Are you using SimpleXML?

A solution for you seems to exist right here.


Answer copied here for posterity's sake

Access the children by their XML namespace.

$dcChildren = $node->children( '' );

$title = $dcChildren->title;
Wednesday, March 31, 2021
answered 7 Months ago

Usually, people use children().

$rss = simplexml_load_string(
    '<?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0" xmlns:moshtix="">

foreach ($rss->channel as $channel)
    echo 'link: ', $channel->link, "n";
    echo 'genre: ', $channel->children('moshtix', true)->genre, "n";
Tuesday, June 8, 2021
answered 5 Months ago

You should use the NSXMLParser in your completion handler for the request:

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
    (data, response, error) in

    if data == nil {
        println("dataTaskWithRequest error: (error)")

    let parser = NSXMLParser(data: data)
    parser.delegate = self

    // you can now check the value of the `success` variable here

// but obviously don't try to use it here here

Clearly, the above assumes that you have (a) defined your view controller to conform to NSXMLParserDelegate and (b) have implemented the NSXMLParserDelegate methods, e.g. something like:

var elementValue: String?
var success = false

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
    if elementName == "success" {
        elementValue = String()

func parser(parser: NSXMLParser, foundCharacters string: String?) {
    if elementValue != nil {
        elementValue! += string

func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
    if elementName == "success" {
        if elementValue == "true" {
            success = true
        elementValue = nil

func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError) {
    println("parseErrorOccurred: (parseError)")
Tuesday, August 10, 2021
answered 2 Months ago

Originally posted as a comment, since I haven't verified it, but:

<Prop1>Some Value</Prop1>

is not the same as

<ns0:Prop1>Some Value</ns0:Prop1>

So to get it working, you probably just need:

public string Prop1 { get; set; }

public string Prop2 { get; set; }
Wednesday, August 25, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :