Jump to content

Read file and find a specific content


apbonn
 Share

Recommended Posts

Hi,

i ve got a file (file.xml) where i want to find content between a tag.

The file.xml looks like this:

 

<package>
    <category name="test">
        <resource>test1</resource>
        <resource>test2</resource>
        <resource>test3</resource>
        <resource>test4</resource>
        <resource>test5</resource>
    </category>
</package>

But it can also look different, but the content i want to know is always between <resource> tags

How do i do that, im trying this:

 

#include <Constants.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <File.au3>
#include <AutoItConstants.au3>
#include <Guiconstants.au3>
#include <FileConstants.au3>


 

$f = FileOpen("file.xml")
$content = FileRead($f)
)
$arr = StringRegExp($content,"<resource>(.|\n)*?<\/resource>",3)

for $i = 0 to UBound($arr) -1
    msgbox(0,"",$arr[$i])
Next
FileClose($f)

But it doesnt work.

Does anybody has an idea?

Link to comment
Share on other sites

And for extra bookmarking

#include <Array.au3>

Local $data = '' _
         & '<package>' _
         & @LF & '    <category name="test">' _
         & @LF & '        <resource>test1</resource>' _
         & @LF & '        <resource>test2</resource>' _
         & @LF & '        <resource>test3</resource>' _
         & @LF & '        <resource>test4</resource>' _
         & @LF & '        <resource>test5</resource>' _
         & @LF & '    </category>' _
         & @LF & '</package>'

$arr = StringRegExp($data, "[\w\d]+(?=</)", 3)
_ArrayDisplay($arr)

Deye

Edited by Deye
Link to comment
Share on other sites

Hey guys new, Problem ;) Im looing for something new

Sample content

<tag name="sample"/>
<tag name="main"/>
</tags>
<group_id value="XMB_DEF_20502"/>
<aspect_ratio max="0.9992224" min="0.9992224" opt="0.9992224"/>
<colorization_supported value="false"/>

And i'm searching for the value of group_id, so the output should look like this

XMB_DEF_20502

Im trying do to this like this:

 

'<group_id value="([^<]*)"\/>'

But the result is empty, do you have any idea ?

Link to comment
Share on other sites

$findGroup = FileOpen('test.xml')
$contentXML = FileRead($findGroup)
$groupID = StringRegExp($contentXML,'<group_id value="([^"]*)',3)
MsgBox($MB_SYSTEMMODAL, "groupID", $groupID)
FileClose($findGroup)

test.xml

<?xml version="1.0" ?>
<decoeffect>
    <tags>
        <tag name="category.test"/>
        <tag name="exclusiveproduct.test"/>
        <tag name="language.default"/>
        <tag name="page.test"/>
        <tag name="page.test"/>
        <tag name="page.inner"/>
        <tag name="product.default"/>
        <tag name="theme.test"/>
    </tags>
    <group_id value="B00003"/>
    <aspect_ratio max="1.6222" min="1.3506" opt="1.4864"/>
    <colorization_supported value="false"/>
    <shadow_supported value="false"/>
    <shadow_colorization_supported value="false"/>
    <relative_padding bottom="0.1023" left="0.0993" right="0.1235" top="0.1316"/>
    <stretch_and_compress_constraints horz_max="200.0" horz_min="0.1" vert_max="200.0" vert_min="0.1"/>
    <overlay tile_usage="2"/>
</decoeffect>

 

Link to comment
Share on other sites

Link to comment
Share on other sites

big big thanks :)

so one last question, but it is a different topic.

if got a string that looks like this:

IPL/decoeffects/birthday/tripplemint

But it can have more or less / , like

IPL/decoeffects/birthday/tripplemint/IPL/decoeffects/birthday/tripplemint
IPL/decoeffects/birthday/tripplemint/test/test
IPL/decoeffects

What i want is the the string without the last word, for example

IPL/decoeffects/birthday/tripplemint/IPL/decoeffects/birthday/tripplemint  
IPL/decoeffects/birthday/tripplemint/test/contnent
IPL/decoeffects


RESULT =

IPL/decoeffects/birthday/tripplemint/IPL/decoeffects/birthday/
IPL/decoeffects/birthday/tripplemint/test/
IPL/

Do you know how to do that?

Link to comment
Share on other sites

Try this :

#include <Array.au3>

; this could be the result of an FileReadToArray ()
Local $aArray [] = _
["IPL/decoeffects/birthday/tripplemint/IPL/decoeffects/birthday/tripplemint", _
"IPL/decoeffects/birthday/tripplemint/test/contnent" , _
"IPL/decoeffects"]

For $i = 0 to UBound($aArray)-1
  $aArray[$i] = StringTrimRight($aArray[$i],StringLen($aArray[$i])-StringInStr($aArray[$i],"/",0,-1))
Next

_ArrayDisplay ($aArray)

 

Link to comment
Share on other sites

You could also use XML dom to get your xml values and use StringSplit to get the values of your string, example:

#cs Data.xml
<?xml version="1.0" ?>
<decoeffect>
<package>
    <category name="test">
        <resource>test1</resource>
        <resource>test2</resource>
        <resource>test3</resource>
        <resource>test4</resource>
        <resource>test5</resource>
    </category>
</package>
    <tags>
        <tag name="category.test"/>
        <tag name="exclusiveproduct.test"/>
        <tag name="language.default"/>
        <tag name="page.test"/>
        <tag name="page.test"/>
        <tag name="page.inner"/>
        <tag name="product.default"/>
        <tag name="theme.test"/>
    </tags>
    <group_id value="B00003"/>
    <aspect_ratio max="1.6222" min="1.3506" opt="1.4864"/>
    <colorization_supported value="false"/>
    <shadow_supported value="false"/>
    <shadow_colorization_supported value="false"/>
    <relative_padding bottom="0.1023" left="0.0993" right="0.1235" top="0.1316"/>
    <stretch_and_compress_constraints horz_max="200.0" horz_min="0.1" vert_max="200.0" vert_min="0.1"/>
    <overlay tile_usage="2"/>
</decoeffect>
#ce
#include <Array.au3>
Local $oXMLDoc = ObjCreate("MSXML2.DOMDocument")
    $oXMLDoc.validateOnParse = True
    $oXMLDoc.load(@ScriptDir & "\Data.xml")
;~ Example 1
Local $oResources = $oXMLDoc.selectNodes('//decoeffect/package/category[@name="test"]/resource')
If IsObj($oResources) Then
    For $oResource In $oResources
        ConsoleWrite("Resource: " & $oResource.text & @CRLF)
    Next
EndIf
;~ Example 2
Local $oGroupId = $oXMLDoc.selectSingleNode("//decoeffect/group_id")
If IsObj($oGroupId) Then
    ConsoleWrite("Group Id : " & $oGroupId.getAttribute("value") & @CRLF)
EndIf

;~ Example 3
Local $aResult, $sResult = "", $sString = "IPL/decoeffects/birthday/tripplemint/IPL/decoeffects/birthday/tripplemint" & @CRLF
    $sString &= "IPL/decoeffects/birthday/tripplemint/test/test" & @CRLF
    $sString &= "IPL/decoeffects" & @CRLF
    $sString &= "IPL"
Local $aString = StringSplit($sString, @CRLF, 1)
For $i = 1 To $aString[0]
    $aResult = StringSplit($aString[$i], "/", 2)
    $sResult &= (UBound($aResult) - 2) >= 0 ? _ArrayToString($aResult, "/", -1, UBound($aResult) - 2) & "/" & @CRLF : $aResult[0] & "/" & @CRLF
Next
ConsoleWrite($sResult & @CRLF)

 

Link to comment
Share on other sites

maybe im getting this wrong

$x = 'test/content/to/check'

; the part i dont get

$result = 'test/content/to/'

only the $x is changing. this has nothing to do with the previous xml part

Edited by apbonn
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...