4

I don't know if there is a method to search in xml file. For instance. I want to get the Value using the Name from AttrList and the ProductCode. Is it possible ? This is how my xml look likes:

<Product>
    <ProductCode>70-14UF44-00</ProductCode>
    <Vendor>NBM</Vendor>
    <ProductType>Soft. Unsorted application</ProductType>
    <ProductCategory>Software</ProductCategory>
    <ProductDescription>{Bluetooth Driver IVT V.1.4.9.3, 1pk, Full Package, OEM, 1pk for 12M3W/15G3WS, 1pk, 1pk}</ProductDescription>
    <Image>https://www.it4profit.com/catalogimg/wic/1/70-14UF44-00</Image>
    <ProductCard>https://content.it4profit.com/itshop/itemcard_cs.jsp?ITEM=50409104050320315&amp;THEME=asbis&amp;LANG=ro</ProductCard>
    <AttrList>
      <element Name="Tipul licentei" Value="Full Package"/>
      <element Name="License Conditions" Value="OEM"/>
      <element Name="Produs de baza(1)" Value="12M3W/15G3WS"/>
      <element Name="Greutatea bruta a pachetului" Value="1.546 kg"/>
      <element Name="Bucati in pachet" Value="1"/>
    </AttrList>
    <MarketingInfo>
      <element></element>
    </MarketingInfo>
    <Images/>
  </Product>

Im using the SimpleXML libraries from PHP Trying with DOMDocument:

$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->Load('produse_catalog.xml');
$xpath = new DOMXPath($doc);
$query = '//ProductCatalog/Product/ProductCode[. = "PMP5297C_QUAD"]';

$entries = $xpath->query($query);
foreach ($entries as $entry) {
    echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
    " by {$entry->previousSibling->nodeValue}\n";
}

The result is: Notice: Trying to get property of non-object. What am i doing wrong ?

2
  • Take a look at SimpleXMLElement::xpath php.net/manual/en/simplexmlelement.xpath.php Commented Oct 24, 2014 at 11:43
  • I looked over it, but i don't undestand how could it be possible to integrate this in my case. Could you post o sample? i would really appreaciate it Commented Oct 24, 2014 at 11:47

2 Answers 2

8

Yes you can use simplexml with xpath in this case:

$xml = simplexml_load_file('path/to/xml/file.xml');
$name = 'Tipul licentei';
$product_code = '70-14UF44-00';
$products = $xml->xpath("//Product/ProductCode[contains(text(), '$product_code')]/following-sibling::AttrList/element[@Name='$name']");
if(count($products) > 0) { // if found

    $value = (string) $products[0]->attributes()->Value;
    echo $value; // Full Package

}

Sample Output

Also possible with DOMDocument:

$dom = new DOMDocument();
$dom->load('path/to/xml/file.xml');
$xpath = new DOMXpath($dom);

$name = 'Tipul licentei';
$product_code = '70-14UF44-00';
$value = $xpath->evaluate("string(//Product/ProductCode[contains(text(), '$product_code')]/following-sibling::AttrList/element[@Name='$name']/@Value)");
echo $value; // Full Package
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for suggesting simplexml_load_file over using DOMDocument.
@killneel actually domdocument is also okay in this case. its possible with both. you can choose which one you're comfortable with
2

You can use the DOMXPath for this purpose. Example taken from php.net

  $doc = new DOMDocument;
  $doc->preserveWhiteSpace = false;
  $doc->Load('book.xml');
  $xpath = new DOMXPath($doc);
  // We starts from the root element
 $query = '//book/chapter/para/informaltable/tgroup/tbody/row/entry[. = "en"]';

 $entries = $xpath->query($query);

 foreach ($entries as $entry) {
   echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
     " by {$entry->previousSibling->nodeValue}\n";
  }

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.