/    Sign up×
Community /Pin to ProfileBookmark

How do I validate XML

How do I validate XML inside a string? Suppose I had the following:

[code]
$x = ‘<fruits>
<fruit>
<type>apple</type>
<color>red</color>
</fruit>
<fruit>
<type>lemon</type>
<color>green</color>
</fruit>
</fruits>’;
[/code]

Is there a way to validate that? Thanks.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@pyroJan 04.2004 — Validate? As in...?
Copy linkTweet thisAlerts:
@jessjennauthorJan 04.2004 — Thanks for the reply. I meant checking for errors. Being able to see if it's correctly structured:

Given the following XML data:<i>
</i>$x = '&lt;fruits&gt;
&lt;fruit&gt;
&lt;type&gt;apple&lt;/type&gt;
&lt;color&gt;red&lt;/color&gt;
&lt;/fruit&gt;
&lt;fruit&gt;
&lt;type&gt;lemon&lt;/type&gt;
&lt;color&gt;green&lt;/color&gt;
&lt;/fruit&gt;
&lt;/fruits&gt;';
This is how I'm validating it:[code=php]
$success = xml_parse($parser, $x);

if($success == 0) {
$error_code = xml_get_error_code($parser);
echo "error: ".xml_error_string($error_code)."<BR>";
echo "row number: ".xml_get_current_line_number($parser)."<BR>";
echo "column number: ".xml_get_current_column_number($parser)."<BR>";
}
else { // validated, now display values
foreach($fruits as $value){
echo $value['type']."|".$value['color']."<br>";
}
}
[/code]


Just want to make sure if this is how you validate it. Thanks!
Copy linkTweet thisAlerts:
@jessjennauthorJan 04.2004 — Oh by the way, do you know off hand how I could add these characters < > ' " & inside the XML tags? I'm escaping them with their entities (&gt; &lt; etc.) and nothing. When they print out, they print out blank. Any ideas?

This is how I'm parsing it:

[code=php]<?
$x = '<fruits>
<fruit>
<type>apple</type>
<color>red</color>
</fruit>
<fruit>
<type>lemon</type>
<color>green</color>
</fruit>
</fruits>';


#$x = '<fruits><fruit><type>apple</type><color>red</color></fruit><fruit><type>lemon</type><color>green</color></fruit></fruits>';

$fruits = array();
$current = '';

// creating a resource parse
$parser = xml_parser_create();

xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($parser, 'startElement', 'endElement');
xml_set_character_data_handler($parser, 'charHandler');

function startElement(&$parser, $name, $attributes) {
global $fruits, $current;
if ($name == 'fruit') {
$fruits[] = array();
}

if (in_array($name, array('type', 'color'))) {
$fruits[count($fruits) - 1][$name] = '';
$current = $name;
}
}

function endElement(&$parser, $name) {
// empty
}

function charHandler(&$parser, $data) {
global $fruits, $current;
if (in_array($current, array('type', 'color')) ) {
$fruits[count($fruits) - 1][$current] = $data;
$current = '';
}
}

$success = xml_parse($parser, $x);
#var_dump($fruits);

if($success == 0) {
$error_code = xml_get_error_code($parser);
echo "error: ".xml_error_string($error_code)."<BR>";
echo "row number: ".xml_get_current_line_number($parser)."<BR>";
echo "column number: ".xml_get_current_column_number($parser)."<BR>";
}
else {

foreach($fruits as $value){
// insert sql statement: insert into...
echo $value['type']."|".$value['color']."<br>";
}
}

?>[/code]
×

Success!

Help @jessjenn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...