I'm trying to parse the next xml which i attach. The problem is that i can't parse the next lines with PHP 5.6. I want to extract the attributes.
<vuln:vulnerable-configuration id="http://www.nist.gov/">
<cpe-lang:logical-test operator="OR" negate="false">
<cpe-lang:fact-ref name="cpe:/o:microsoft:windows_8:-"/>
<cpe-lang:fact-ref name="cpe:/o:microsoft:windows_8.1:-"/>
<cpe-lang:fact-ref name="cpe:/o:microsoft:windows_server_2012:-:gold"/>
<cpe-lang:fact-ref name="cpe:/o:microsoft:windows_rt:-:gold"/>
<cpe-lang:fact-ref name="cpe:/o:microsoft:windows_rt_8.1:-"/>
</cpe-lang:logical-test>
</vuln:vulnerable-configuration>
I have used the next code but i cannot capture de cpe-lang field. Also i have tried using namespaces but i cannot get the children of something inside the vuln namespace.
[$source = file_get_contents('C:/xampp/htdocs/prueba/products1.xml');
$xml = new SimpleXMLElement($source);
$entries = $xml->entry;
foreach ($entries as $entry) {
$namespace = $entry->getNameSpaces(true);
$tmp = $entry->children($namespace\['cpe-lang'\],false);
print_r($tmp)
$aux={'fact-ref'};
$config\['fact-ref'\]= (string)$entry->children('vuln', TRUE)->config->children($aux, TRUE)->logical_test->{'fact-ref'};
print_r($config);
}][1]
If i print the variable $tmp it says that the array is empty.
When i print $config it says:
Warning: main(): Node no longer exists in C:\xampp\htdocs\prueba\probando.php on line 56
Notice: Trying to get property of non-object in C:\xampp\htdocs\prueba\probando.php on line 56
Array ( [fact-ref] => )
Thanks
Related
i am really having trouble getting this to work. xpath works fine in other functions in the same file. I am trying to get a specific item from the XML file, which works fine when i dd() it. But when i try to load the exact same code into a variable it prompts "undefined offset 0". I am working local with Laravel.
XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<idPkg:Graphic xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="15.1">
<Color Self="Color/u2a64f" Model="Process" Space="RGB" ColorValue="204 204 204" />
</idPkg:Graphic>
PHP
function getColor($colorID){ //$colorID = Color/u2a64f
$xml= simplexml_load_file('/Resources/Graphic.xml');
$colorNode = $xml->xpath('//Color[#Self="'.$colorID.'"]');
dd($colorNode[0]->attributes()->ColorValue);
/* outputs:
SimpleXMLElement {#316 ▼
+"0": "204 204 204"
}
*/
$fillColor = $colorNode[0]->attributes()->ColorValue; // Line of error
/* outputs:
ErrorException (E_NOTICE)
Undefined offset: 0
*/
return $fillColor;
}
EDIT:
what i just found out: when i replace the variable in xpath with the content of the variable it works.
function getColor($colorID){ //$colorID = Color/u2a64f
$xml= simplexml_load_file('/Resources/Graphic.xml');
// doesnt work
$path = '//Color[#Self="'.$colorID.'"]';
// does work
$path = '//Color[#Self="Color/u2a64f"]';
$colorNode = $xml->xpath($path);
dd($colorNode[0]->attributes()->ColorValue);
/* outputs:
SimpleXMLElement {#316 ▼
+"0": "204 204 204"
}
*/
$fillColor = $colorNode[0]->attributes()->ColorValue; // Line of error
/* outputs:
ErrorException (E_NOTICE)
Undefined offset: 0
*/
return $fillColor;
}
i just cant get my head around it... I am glad for any hints or help!
Nevermind i found the solution myself.
After checking my code again i saw that sometimes some XML nodes dont have the attribute FillColor. So i checked my param first if it is set and then proceed to xpath.
I'm trying to add customers with a code but the PrestaShop is giving me a bug.
I'm using PHP and XML
$XMLRQString = '<?xml version="1.0" encoding="utf-8"?>'.
'<x:Winmax4GetEntitiesRQ xmlns:x="urn:Winmax4GetEntitiesRQ">'.
'</x:Winmax4GetEntitiesRQ >';
$Params=array(
'CompanyCode'=>'',
'UserLogin'=>'',
'UserPassword'=>'',
'Winmax4GetEntitiesRQXML'=> $XMLRQString
);
$return = $client->GetEntities($Params);
$XMLRSString = new SimpleXMLElement($return->GetEntitiesResult);
foreach ($XMLRSString->Entities->Entity as $entity)
{
$default_lang= Configuration::get('PS_LANG_DEFAULT');
$customer=new Customer();
$customer->email= $entity->Email;
$customer->lastname= $entity->EntityType;
$customer->firstname= [$default_lang => $entity->Name];
$customer->contribuinte= $entity->TaxPayerID;
$customer->passwd= $entity->TaxPayerID;
$customer->active = 1;
$customer->add();
}
ERROR: (1/1) ContextErrorException Warning: preg_match() expects
parameter 2 to be string, array given
in Validate.php line 172
at ValidateCore::isCustomerName(array(object(SimpleXMLElement))) in
ObjectModel.php line 1149
at ObjectModelCore->validateField('firstname',
array(object(SimpleXMLElement))) in ObjectModel.php line 981
at ObjectModelCore->validateFields() in ObjectModel.php line 284
at ObjectModelCore->getFields() in ObjectModel.php line 551
at ObjectModelCore->add(true, true) in Customer.php line 264
at CustomerCore->add() in create_clients.php line 66
When storing values from SimpleXML, if you just refer to the element itself by it's tag name - this will be an instance of SimpleXMLElement. As you want the actual content of the element, the simplest way to do this is to cast it to a string...
$customer->firstname= (string)$entity->Name;
This works on my test environment, but on my live server there is a later version of PHP which is throwing up an error and breaking my program
The code is
$oldFile = fopen("D:/ftpfolderreport/report/" . $last_file, "r");
while(!feof($oldFile))
{
$buffler = fgets($oldFile);
$bufflerArray = explode(",", $buffler);
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}
fclose($oldFile);
This line:
$oldFileArray[$key] = $bufflerArray[1];
Is throwing out this error
Notice: Undefined offset: 1 in D:\apps\wamp\www\Compliance2\compareFtpReports.php on line 57
I think this is to do with how I'm adding the $key variable inside the argument. I've tried it as ["$key"] and ['$key'] but it doesn't like it.
I have tried defining the key variable earlier in the program but still doesn't like it. I've been searching around online but can't find anything of help. Anyone any ideas?
Thanks,
Stephen.
add checks for empty
if (!empty($bufflerArray[1])) {
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}
I am parsing a SVG file using SimpleXMLElement in PHP. The SVG file is carefully constructed in Adobe Illustrator follow a layer format that I am attempted to dissect.
Consider this code:
// Create an XML object out of the SVG
$svg = new SimpleXMLElement('floorplan.svg', null, true);
// Register the SVG namespace
$svg->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
// Get the normal floorplan layer
$normal = $svg->xpath('svg:g[#id="Normal"]');
// If the normal layer has content, continue
if(count($normal) > 0) {
// If there are floors, continue
if(count($normal[0]->g > 0)) {
// Loop through each floor
foreach($normal[0]->g as $floor) {
// Declare the namespace for the floor
$floor->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
// Select the base floorplan
$floorsvg = $floor->xpath('svg:g[#id="Base"]')[0];
var_dump($floorsvg);
echo $floorsvg->asXML(); // THIS CAUSES THE ERROR
}
}
}
When I do a var_dump on $floorsvg, it is declaring it is a SimpleXMLElement object:
object(SimpleXMLElement)[9]
public '#attributes' =>
array (size=1)
'id' => string 'Base' (length=4)
public 'g' =>
array (size=859)
0 => ...
However, when I run asXML() on the object, I am presented with the following PHP error:
PHP Fatal error: Call to a member function asXML() on a non-object
I'm uncertain why asXML() is failing, considering it is an object. Can anyone shed any light on why this problem is occurring and what I might try to remedy it?
EDIT: Adding an echo $normal->asXML(); up above results in the same error. It seems like xpath is causing the object to become malformed somehow.
EDIT 2: The SVG file being parsed can be seen here: http://pastebin.com/zK1yRFA7
There are a few issues. Your code assumes that:
$floor->xpath('svg:g[#id="Base"]')
Will return an array with at least 1 element. The <g id="Second_Floor"> element does not contain any child elements that will be matched by that XPath expression, so trying to access element 0 of an empty array will give you the error you are seeing.
Adding a simple guard expression:
// Select the base floorplan
$floorsvg = $floor->xpath('svg:g[#id="Base"]')
if (count($floorsvg) > 0) {
echo $floorsvg[0]->asXML();
}
Will resolve that. Secondly, you have some misplaced parentheses in this line:
if(count($normal[0]->g > 0)) {
It should be:
if(count($normal[0]->g) > 0) {
Appears to be just a simple typo and doesn't appear to affect the outcome of this particular script one way or another.
i am unable to parse xml document .
well my task was like that
i got xml page from curl which contain ip info
<ip_address>209.59.194.20</ip_address><ip_type>Mapped</ip_type><Network><organization>thoughtconvergence.com</organization><carrier>whidbey internet services</carrier><asn>6295</asn><connection_type/><line_speed/><ip_routing_type>fixed</ip_routing_type><Domain><tld>com</tld><sld>trafficz</sld></Domain></Network><Location><continent>north america</continent><latitude>34.03708</latitude><longitude>-118.42789</longitude><CountryData><country>united states</country><country_code>us</country_code><country_cf>99</country_cf></CountryData><region>southwest</region><StateData><state>california</state><state_code>ca</state_code><state_cf>80</state_cf></StateData><dma>803</dma><msa>31100</msa><CityData><city>los angeles</city><postal_code>90064</postal_code><time_zone>-8</time_zone><area_code>323</area_code><city_cf>61</city_cf></CityData></Location></ipinfo>
i try to parse it
$book = simplexml_load_string($datax);
$ipadd = $book->ip_address;
$ipatype = $book->ip_type;
$ip_routing_type = $book->Network->ip_routing_type;
$state = $book->Location->StateData->state;
$country = $book->Location->CountryData->country;
$continent = $book->Location->continent;
$region = $book->Location->region;
now am getting few errors
1) Entity: line 2: parser error : Extra content at the end of the document
2 Trying to get property of non-object in line #
Your XML is invalid. It has a closing tag for a root element at the end of the data
</ipinfo>
but there is no header tag. If you tack:
<ipinfo>
to the front of it I'll bet it will work.