0

Can anyone advise on the fastest, least resource intensive method by which I can see whether 'Column Name=Error' exists please?

I don't want to parse the document, but simply check if elements exist.

Thanks in advance,

<?xml version="1.0" encoding="UTF-8"?>
<Table>
<Columns Items="4">
<Column Name="Error" Type="String" />
<Column Name="Description" Type="String" />
<Column Name="Cause" Type="String" />
<Column Name="Resolution" Type="String" />
</Columns>
<Rows Items="1">
<Row Error="2" Description="Unknown key" Cause="Unknown key" Resolution="Please check     the key is correct, it should be the in form AA11-AA11-AA11-AA11." />
</Rows>
</Table>

2 Answers 2

2

As others have mentioned, the least resource intensive would be a simple strpos() call, though that's subject to error if the exact format of the XML ever changes. A fullproof way is to use DOM, then you could try an xpath query ...

$xml = '...'
$dom = new DomDocument();
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);

// returns NULL if no columns found with name="error"
$err = $xpath->query('//Column[@Name="Error"]')->item(0);

if ($err) {
  // there is a column with attribute Name="Error"
}
Sign up to request clarification or add additional context in comments.

Comments

0

have you thought of simply using strpos?

$xml= //xml data

if(strpos($xml,'<Column Name="Error"') !== false){
    // its been found
}

edit: added !==false to allow for the 0 index situation (which shouldnt happen here anyways)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.