samedi 1 août 2015

Count how many children are in XML with PHP

i know a few about php, so sorry for the question:

i have this file xml:

 <?xml version="1.0" encoding="ISO-8859-1"?>
  <alert>
   <status> </status>
   <nothing> </nothing>
   <info>
    <area>
    </area>
   </info>
   <info>
    <area>
    </area>
   </info>
   <info>
    <area>
    </area>
   </info>
  </alert>

i must do a for loop and inside a "foreach" for each The problem is that i'm not sure what is a way to know how many times i had to repeat a for loop. Because in this file xml (that is an example) i don't know how many are

Is good if:

$url = "pathfile";
$xml = simplexml_load_file($url);
$numvulcani = count($xml->alert->info);

for ($i = 0; $i <= $numvulcani; $i++) {
 foreach ($xml->alert->info[$i] as $entry) {
  $area = $entry->area;
 }
}

is true ?

sorry for bad english

XML Http Request: CORS issue

Hi I've been working on an angularjs app on plnkr, and I'm pulling data from a remote server to the app. Initially I had problems accessing the xml file because of the CORS issue, the cross-domain restriction, but I enabled CORS. The app now works on plnkr, but when I try to run it on the browser, the console gives me the same error, except with an additional note at the end.

The error:

`XMLHttpRequest cannot load http://ift.tt/1M35ggf. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.`

The "the reponse had HTTP status code 403" bit seems to be new. What's going on? Why does it work on plnkr and not in browser? What do I need to do to fix it? Any help is appreciated.

How to process elements only if they're _not_ followed by a specific element

I have an XML like this:

<?xml version="1.0"?>
<root>
  <mtef>
    <slot>
      <options>0</options>
      <char>
        <typeface>2</typeface>
        <mt_code_value>0x0028</mt_code_value>
      </char>
      <char>
        <typeface>3</typeface>
        <mt_code_value>0x0062</mt_code_value>
      </char>
      <char>
        <typeface>2</typeface>
        <mt_code_value>0x0029</mt_code_value>
      </char>
      <tmpl>
        <selector>tmSUP</selector>
        <template_specific_options>0</template_specific_options>
        <sub/>
        <slot>
          <options>1</options>
        </slot>
        <slot>
          <options>0</options>
          <char>
            <typeface>3</typeface>
            <mt_code_value>0x0063</mt_code_value>
          </char>
          <end/>
        </slot>
        <end/>
      </tmpl>
      <end/>
    </slot>
    <end/>
  </mtef>
</root>

If the tmpl element with the tmSUP selector is preceded by a char with a mt_code_value of 0x0029 (HTML entity for right parenthesis), then the processing of this tmpl element must find the opening, left parenthesis, and use those in-between char elements in its template.

The issue I'm having is with the double processing of char elements, first by their own template, then by the tmpl[selector='tmSUP'] template. How do I prevent char elements from being processed when a succeeding tmpl element is following a char with the mt_code_value of 0x0029?

My stylesheet looks like this currently:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://ift.tt/tCZ8VR"
    xmlns:xs="http://ift.tt/tphNwY"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:template match="/">
        <root>
           <xsl:apply-templates select=".//mtef" />
        </root>
    </xsl:template>

    <xsl:template match="mtef">
        <math>
            <xsl:apply-templates select="slot"/>
        </math>
    </xsl:template>

    <xsl:template match="slot">
        <mrow>
            <xsl:apply-templates/>
        </mrow>
    </xsl:template>

    <xsl:template match="tmpl[selector = 'tmSUP']">
        <msup>
        <mrow>
            <xsl:choose>
                <!-- Closing bracket -->
                <xsl:when test="preceding-sibling::char[1]/mt_code_value = '0x0029'">
                    <xsl:for-each select="preceding-sibling::*">
                        <xsl:sort select="position()" data-type="number" order="ascending"/>
                        <xsl:apply-templates select="."/>
                    </xsl:for-each>
                </xsl:when>
            </xsl:choose>            
        </mrow>
            <xsl:apply-templates select="slot[2]"/>
        </msup>
     </xsl:template>

    <xsl:template match="char[typeface = '2']">
        <mn>
            <xsl:text disable-output-escaping="yes">&amp;#</xsl:text>
            <xsl:value-of select="substring(mt_code_value/text(), 2)"/>
            <xsl:text>;</xsl:text>
        </mn>
    </xsl:template>

    <xsl:template match="char[typeface = '3']">
        <mi>
            <xsl:text disable-output-escaping="yes">&amp;#</xsl:text>
            <xsl:value-of select="substring(mt_code_value/text(), 2)"/>
            <xsl:text>;</xsl:text>
        </mi>
    </xsl:template>

    <xsl:template match="*" />

</xsl:stylesheet>

Which results in this output:

<?xml version="1.0" encoding="UTF-8"?><root>
  <math>
    <mrow>  
      <mn>&#x0028;</mn>
      <mi>&#x0061;</mi>
      <mo>&#x2212;</mo>
      <mi>&#x0062;</mi>
      <mn>&#x0029;</mn>
      <msup>
        <mrow>
          <mn>&#x0028;</mn><mi>&#x0061;</mi><mo>&#x2212;</mo><mi>&#x0062;</mi><mn>&#x0029;</mn>
        </mrow>
        <mrow>  
          <mi>&#x0063;</mi>
        </mrow>
      </msup>
    </mrow>
  </math>
</root>

Notice how some elements appear twice. What is a general approach to solving this kind of conditional "look-ahead" processing with XSLT?

Xml shoutcast to a mysql database

i have made a php file to retrieve data to a database from a xml file and it works:

$title = $xml->SONGTITLE; {


// performing sql query

$sql = "INSERT INTO test_xml (`title`) VALUES ('$title') ON DUPLICATE KEY UPDATE time = now()";

the xml is from a shoutcast server, witch is made automatically from my radio. The xml line is:

<SONGTITLE>Morrissey - Don`t Make Fun of Daddy`s Voice (John Peel session)</SONGTITLE>

is there a way to separate the artist from the Song name? And retrieve title to title and artist to artist. Can you give me ideas how to start?

VTD-XML: How to get XPath of current element?

I'm using VTD to parse and split very large XML files and require the actual XPath of the current element pointed to by the cursor after a call to AutoPilot.evalXPath().

I tried almost all methods in VTDNav-

  • getXPathStringVal() / getXPathStringVal(short mode)
  • toNormalizedXPathString(int j)

Any ideas?

Recursive XSL Selection

The XML looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
<nodes xmlns:xsi="http://ift.tt/ra1lAU">
    <element>Content1</element>
    <element>Content2</element>
    <element>Content3</element>
    <parent>
        <element>Content4</element>
        <element>Content5</element>
            <parent>
                <element>Content6</element>
                    <parent>
                        <element>Content7</element>
                        <element>Content8</element>
                    </parent>
                <element>Content9</element>
            </parent>
        <element>Content10</element>
        <element>Content11</element>
    </parent>
    <element>Content12</element>
    <parent>
        <element>Content13</element>
        <element>Content14</element>
    </parent>
    <element>Content15</element>
</nodes>

I want to select all nodes called elements, also if they are inside of parent and i want to know how many parent elements they have so I can display this html structure in html like this:

<pre>
Content1
Content2
Content3
    Content4
    Content5
        Content6
            Content7
            Content8
        Content9
    Content10
    Content11
Content12
    Content13
    Content14
Content15
</pre>

Any thoughts?

java, xml - Pop up menu in app to appear and add Layout dynamically

I've been googling this sort of stuff for several days now and found a couple pages that did say that it works, but it sorta doesn't explain the process and stuff.

http://ift.tt/1SqMgfe http://ift.tt/1I9MTSy

Other than these two, I wanted to know what it wanted me to do on the MainActivity.java to get this working.

I've already got the button on my menu_main.xml, but am I missing something to make it work? Does the method have to be in the MainActivity.java then call the method in the other java class to make it work? Or does the MainActivity.java have to just instantiate the other java class and the button can easily call the method in the other class?

Lastly, I'm making an app that will start off with an empty background with a text, implying that it is empty and there is nothing here. There is a button on the top right that you hit that will create a LinearLayout and after adding in information on the pop up menu on the earlier issue, you then add the LinearLayout onto the page and erase the text. I can easily erase the text but adding the LinearLayout is proving to be difficult. I couldn't find a proper answer for this one.

Image sample of how it will look initially: enter image description here

What will happen after hitting the top right button and filling in info on pop up (ignore missing add and settings button):

enter image description here

Thank you for your help.