[DELPHI]XMLDocuments

0

Mam mały problem. Jeśli mam plik xml o strukturze:

<samochod>

    <marka>
           <model par="costam" par2="costam"/>
   { <b><- to jak w tej linijce wstawić Childa o takiej samiej nazwie czyli "model" tylko z innymi parametrami??</b>}
     </marka>

</samochod>

Bo jeśli zastosuje:

XMLNode.ChildNodes['model'].Attributes['par'] := 'costam2';
XMLNode.ChildNodes['model'].Attributes['par2'] := 'costam2';

to program podmienia mi tą linijke <model par="costam" par2="costam" /> :[, a ja chce zeby nowa pojawiła się ona linijke niżej :/. Jesli bym zastosował inna nazwe childa np model2 to pojawi się niżej ale mój program wymaga tego żeby childy miały taką samą nazwe. Z góry dzięki!

0

XMLNode.ChildNodes['model'].ChildNodes['model'].Attributes['par'] := 'costam2';

czy jakos tak [nie pamietam dokladnie]

0
nav napisał(a)

XMLNode.ChildNodes['model'].ChildNodes['model'].Attributes['par'] := 'costam2';

czy jakos tak [nie pamietam dokladnie]

Nav też źle bo wyszło mi takie coś:

<samochod>

    <marka>
           <model par="costam" par2="costam">
           <model par="costam2" par2="costam2"/>
     </model>
</marka>

</samochod>

Ma ktoś inne pomysł??Może ułomny jest ten komponent i się nie da zrobić :/.

0

To raczej logiczne, że tak się dzieje. Do tworzenia elementów chyba jest specjalna metoda, pewnie TXMLNode.AddChild, czy coś podobnego :). Poszukaj.

0

Hmm nie musi być to koniecznie XMLDocumentem robione bo ten plik xml wykorzystuje program ktorego nie robilem. Ja robie program ktory dodaje wpisy do plikow xml ktore progeam bedzie wykorzystywal. Wiec jakim kolwiek sposobem ta linijka mzoe byc wstawiona. Wpadlem na pomysl ze moze mozna wstawic linijke jak do pliku tekstowego gdzie parametry beda stringami, nawet tak zrobilem ale sa ogranczenia bo musze wskazac w ktorej linijce ma byc wklejony wiec program jest zbyt malo elastyczny. Czy da sie tak zrobic żeby:

<samochod>

    <marka>
           <model par="costam" par2="costam"/>
<b>...program wyszukał wpisu marka "<marka>" jako odnośnik jesli go znajdzie to czy da się żeby program określił gdzie w którym wersie się znajduje czyli przypisze wartość zmiennej X -> i wtedy list.Insert(X,s); gdzie 
S:='<model par="'+c+'" par2="'+d+'"/>'; i  dokona się  wstawienie "modelu" linike niżej pod <marka>.</b> 

Bo wykminilem wlasnie taka prosta rzecz:

List:=TStringList.Create;
  List.LoadFromFile('c:\testt.xml');
  list.Insert(6,s);
  List.SaveToFile('c:\testt.xml');
  List.Free;
ale jak już mówłem mało plastyczne bo plik xml może być zmieniony i wers sie wstawi w nieodpowiednie miejsce.

</marka> </samochod>
0

Ale kombinujesz... nie mogłeś zajrzeć do helpa? Nawet trafiłem z tą nazwą metody :>.

help delphi napisał(a)

TXMLNode.AddChild Method

Adds a new child node to this node.

Class
TXMLNode

Syntax

[Delphi] protected function AddChild(const TagName: WideString, Index: Integer): IXMLNode;

[Delphi] protected function AddChild(const TagName: WideString, const NamespaceURI: WideString, GenPrefix: Boolean, Index: Integer): IXMLNode;

[Delphi] protected function AddChild(const TagName: WideString, const NamespaceURI: WideString, NodeClass: TXMLNodeClass, Index: Integer): IXMLNode;

Description
Use the TXMLNode object's IXMLNode interface to call the protected AddChild method. Only the first two versions of AddChild are available on the IXMLNode interface. The third syntax is used by TXMLNode descendants that the XML data binding wizard creates.

AddChild creates a new element node as the child of this node.

TagName provides the tag name of the newly created node.
Index indicates the position of the child node in this node's list of children, where 0 is the first position, 1 is the second position, and so on. If Index is ?1, the new node is added to the end.

NamespaceURI identifies the namespace that includes the new node's definition. If NamespaceURI is omitted (the first syntax), the namespace of the new node is deduced from the namespace prefix of TagName. If TagName has no namespace prefix and there is no NamespaceURI parameter, the namespace is the same as this node's NamespaceURI property.

GenPrefix controls whether AddChild generates a namespace prefix for the namespace URI when the NamespaceURI parameter identifies a URI that is not already declared in the document. Note that setting GenPrefix to true does not cause AddChild to add a declaration of the new namespace prefix unless the document's Options property includes doNamespaceDecl.

NodeClass identifies the implementation class to use for the new child node. It must be TXMLNode or one of its descendants.

AddChild returns the interface for the newly created child node.

Tip: AddChild only adds element nodes. To add other types of child nodes, use the XML document's CreateNode method instead, and then add the resulting node to the ChildNodes property array.

0

Działa, ale pozwala to dodac tylko jeden atrybut :/

XML.DocumentElement.ChildNodes['model'].addchild('marka').attributes['par1']:= ('costam');

kombinowałem, ale nie wymyśliłem jak dodać drugi atrybut.

0

Może spróbuj addchild('marka', -1) ? :)

0

Jesli chodzilo Ci o takie cos:

XML.DocumentElement.ChildNodes['model'].addchild('marka').attributes['atr]:= ('aaa');
XML.DocumentElement.ChildNodes['model'].addchild('marka',-1).attributes['atr2']:= ('bbb');

to nadal bedzie to samo i wyjdzie:

<record atr="aaa"/>  
<record from="costam"/>
0

Sorry za posta pod postem ale źle sie zrozumieliśmy. Atrybut czyli atr=aaaa :D. Chodzilo mi o to, ze moge dodac tylko jeden :D, a nie umiem tak zrobic zeby do "marki" dodac dwa atrybuty :D.

0

with XML.DocumentElement.ChildNodes['model'].addchild('marka') do
begin
//i tutaj atrybuty
end;

0

Dzieki! Zarejestrowałem się zeby nie było zamieszania z linkami :D

0

Mam jeszcze jeden mały problem. Mam plik XML :

<model>

<marka par="aa"/>
<marka par="aa"/>
<marka par="aa"/>

</model>

i chce we wszystkich childach zmienic parametr par na bb.

Poerwszy child to umiem :D

xml.DocumentElement.childnodes['marka'].Attributes['par']:='bb';

Moge zastosować też integer zamiast 'marka' ale to mało uniwersalne.

0

for I:=0 to ChildNodes.Count-1 do
if ChildNodes.LocalName='marka' then
ChildNodes[I].CosTam;

Coś takiego zrób.

0

Cos nie dziala:

XML.LoadFromFile('C://xml2//plik.xml');

XML.Active := True;

  for I:=0 to xml.documentelement.childnodes.Count-1 do

if xml.DocumentElement.ChildNodes=('fmarka') then {nie moze tak być bo musi być (...).ChildNodes['marka'] :/

xml.documentelement.ChildNodes[I].Attributes['par']:=('bb');

XML.Active := False;
0

nie ma czegoś takiego jak GetNodeName?

[<font color="green">edit</span>]
czytałeś już to ?

0

Czytałem, a co jest tam rozwiązanie???? Nie ma takiego czegos jak getnodename, czy tez innych podobnych. :(

0

musi być, poczytaj help'a

0

W helpie jest Getnodename metoda txmlnode i ixmlnode, ale nie moge znalezc tego w komponencie :/. Tzn wiem ze Nodename jest z tym powiazane no xml.documentelement.nodename=('marka') wzialem to nic nie robi bo jest falsz bo nie znajduje marki, ale jesli wpisze 'model' to zatrybi bo to jest Local Name matka ale wywali blad co jest oczywiste bo nie mozna atrybutow jej przypisywac(chyba). No ale ja chce do marki przypsiac atrybut, a nie do modelu.

0

UDALO MI SIE !!!!!!! :D

for i:=0 to xml.documentelement.childnodes.Count-1 do if
 xml.documentelement.ChildNodes[i].LocalName='marka' then
xml.documentelement.childnodes[i].Attributes['value']:=('dupa');

Dziekuje :D za pomoc.

1 użytkowników online, w tym zalogowanych: 0, gości: 1