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:

<item>
  <title>foo</title>
  <description>foo</description>           
  <ccc:fid>10</ccc:fid>
  <ccc:bid>6</ccc:bid>
 </item>

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?

 Answers

66

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:

  • http://www.w3.org/TR/1999/REC-xml-names-19990114/
  • http://www.w3.org/TR/xml-names/

Hope that helps, albeit it is a little complicated.

Wednesday, March 31, 2021
 
hakre
answered 7 Months ago
88

Are you using SimpleXML?

A solution for you seems to exist right here.

EDIT

Answer copied here for posterity's sake

Access the children by their XML namespace.

$dcChildren = $node->children( 'http://purl.org/dc/elements/1.1/' );

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

Usually, people use children().

$rss = simplexml_load_string(
    '<?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0" xmlns:moshtix="http://www.moshtix.com.au">
        <channel>
            <link>qweqwe</link>
            <moshtix:genre>asdasd</moshtix:genre>
        </channel>
    </rss>'
);

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

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)")
        return
    }

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

    // you can now check the value of the `success` variable here
}
task.resume()

// 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
 
RahulG
answered 2 Months ago
16

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:

[XmlElement(Namespace="")]
public string Prop1 { get; set; }

[XmlElement(Namespace="")]
public string Prop2 { get; set; }
Wednesday, August 25, 2021
 
rblarsen
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 :