Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-06

XML read

12 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marcos Douglas

Hi,
What is the more simpler way to get all text in each paragraph in the XML below, using fcl-xml?
For example:

Page 1
-------------------
Para 1:
-1524
-888
-14/06/06
Para 2
-TEXT 2

Page 2
-------------------
Para 1
--TEXT page 2

I need each paragraph in a TStringList.

<?xml version="1.0" encoding="UTF-8"?>
<Document filename="127.pdf" pageCount="7" filesize="141859" linearized="false" pdfVersion="1.5">
<Pages>
<Page number="1" width="612" height="1008">
<Options>granularity=line</Options>
<Content granularity="line" dehyphenation="false" dropcap="false" font="false" geometry="false" shadow="false" sub="false" sup="false"> <Para>
<Line>
<Text>1524</Text>
</Line>
<Line>
<Text>888</Text>
</Line>
<Line>
<Text>14/06/06</Text>
</Line>
</Para>
<Para>
<Line>
<Text>TEXT 2</Text>
</Line>
</Para>
</Content>
</Page>
<Page number="2" width="612" height="1008">
<Options>granularity=line</Options>
<Content granularity="line" dehyphenation="false" dropcap="false" font="false" geometry="false" shadow="false" sub="false" sup="false"> <Para>
<Line>
<Text>TEXT page 2</Text>
</Line>
</Para>
</Content>
</Page>
</Pages>
</Document>

Thanks,
Marcos Douglas

Michael Van Canneyt
to Marcos Douglas
Marcos Douglas wrote 20 lines
Hi, What is the more simpler way to get all text in each paragraph in the XML below, using fcl-xml? For example: Page 1 ------------------- Para 1: -1524 -888 -14/06/06 Para 2 -TEXT 2 Page 2 ------------------- Para 1 --TEXT page 2 I need each paragraph in a TStringList.

There is no "simple" way. You must walk the nodes and append the text of all <Text> nodes.

Or you can try XPath, this should give you all the <Text> nodes, but I am not familiar with the exact structure of XPath.

Michael.

Show quoted text 48 lines
<?xml version="1.0" encoding="UTF-8"?> <Document filename="127.pdf" pageCount="7" filesize="141859" linearized="false" pdfVersion="1.5"> <Pages> <Page number="1" width="612" height="1008"> <Options>granularity=line</Options> <Content granularity="line" dehyphenation="false" dropcap="false" font="false" geometry="false" shadow="false" sub="false" sup="false"> <Para> <Line> <Text>1524</Text> </Line> <Line> <Text>888</Text> </Line> <Line> <Text>14/06/06</Text> </Line> </Para> <Para> <Line> <Text>TEXT 2</Text> </Line> </Para> </Content> </Page> <Page number="2" width="612" height="1008"> <Options>granularity=line</Options> <Content granularity="line" dehyphenation="false" dropcap="false" font="false" geometry="false" shadow="false" sub="false" sup="false"> <Para> <Line> <Text>TEXT page 2</Text> </Line> </Para> </Content> </Page> </Pages> </Document> Thanks, Marcos Douglas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Marcos Douglas
to Michael Van Canneyt
Michael Van Canneyt wrote 28 lines
On Fri, 10 Jun 2011, Marcos Douglas wrote: > Hi, > What is the more simpler way to get all text in each paragraph in the > XML below, using fcl-xml? > For example: > > Page 1 > ------------------- > Para 1: > -1524 > -888 > -14/06/06 > Para 2 > -TEXT 2 > > Page 2 > ------------------- > Para 1 > --TEXT page 2 > > I need each paragraph in a TStringList. There is no "simple" way. You must walk the nodes and append the text of all <Text> nodes. Or you can try XPath, this should give you all the <Text> nodes, but I am not familiar with the exact structure of XPath.

OK Michael, thanks.

Marcos Douglas

Bart
to Marcos Douglas

I have some code that reads/writes an addressbook in XML format. I can attach if if you think it may be usefull to you.

Bart

Marcos Douglas wrote 40 lines
On Sat, Jun 11, 2011 at 5:52 AM, Michael Van Canneyt wrote: > > > On Fri, 10 Jun 2011, Marcos Douglas wrote: > >> Hi, >> What is the more simpler way to get all text in each paragraph in the >> XML below, using fcl-xml? >> For example: >> >> Page 1 >> ------------------- >> Para 1: >> -1524 >> -888 >> -14/06/06 >> Para 2 >> -TEXT 2 >> >> Page 2 >> ------------------- >> Para 1 >> --TEXT page 2 >> >> I need each paragraph in a TStringList. > > There is no "simple" way. You must walk the nodes and append the text of > all <Text> nodes. > > Or you can try XPath, this should give you all the <Text> nodes, but I am > not familiar with the exact structure of XPath. OK Michael, thanks. Marcos Douglas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Marcos Douglas
to Bart
Bart wrote
I have some code that reads/writes an addressbook in XML format. I can attach if if you think it may be usefull to you.

OK, if you can I appreciate that.
Some functions of fcl-web returns a new object... I have to be careful with memory leaks.

Marcos Douglas

Bart
to Marcos Douglas

Hi Marcos,

OK, if you can I appreciate that. Some functions of fcl-web returns a new object... I have to be careful with memory leaks.

See: addressbook.pp.
Mind you, my code isn't web related, it just handles an xml-file.

Bart

Marcos Douglas
to Bart
Bart wrote
Attached a sample xml file created with TAddressBook.

Hi Bart,
Sorry for late.
Thank you for addressbook.pp and test.xml files. I will study them.

Marcos Douglas

Marcos Douglas
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote
The Wiki has some XML tutorials: http://wiki.lazarus.freepascal.org/XML_Tutorial
Yes, I saw that.
My doubt is how use a safe algorithm to parse my XML. See the first
example "Printing the names of nodes and attributes" in link that you
posted:
with Child.ChildNodes do
try
//..
finally
  Free;
end;

I have to know which functions create a new object. That is not clear if we use Child.ChildNodes, right?

Marcos Douglas

Bart
to Marcos Douglas

My XmlToList procedure is based upon the XmlToTree example in the wiki.

There are no memory leaks in my code (checked it with heaptrace).

Bart

Marcos Douglas wrote 25 lines
On Tue, Jun 14, 2011 at 10:12 AM, Felipe Monteiro de Carvalho wrote: > The Wiki has some XML tutorials: > > http://wiki.lazarus.freepascal.org/XML_Tutorial Yes, I saw that. My doubt is how use a safe algorithm to parse my XML. See the first example "Printing the names of nodes and attributes" in link that you posted: with Child.ChildNodes do try //.. finally Free; end; I have to know which functions create a new object. That is not clear if we use Child.ChildNodes, right? Marcos Douglas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Marcos Douglas
to Bart
Bart wrote
My XmlToList procedure is based upon the XmlToTree example in the wiki. There are no memory leaks in my code (checked it with heaptrace).

Hi Bart,
Below is my "simple way" to get all <Text> for each <Page>:

----------------------------------------------------------------------------
program t_xml;
{$mode objfpc}{$H+}
uses
  Classes, sysutils, dom, XMLRead;
procedure ProcessPage(ANode: TDOMNode; APageNo: Integer);
var
  Node: TDOMNode;
begin
  if not Assigned(ANode) then
    Exit;
  Node := ANode.FirstChild;
  while Assigned(Node) do
  begin
    if Node.CompareName('Text') = 0 then
    begin
      writeln(Node.TextContent); // << do anything...
    end;
    Node := Node.NextSibling;
    ProcessPage(Node, APageNo);
  end;
end;
procedure ProcessNodes(ANode: TDOMNode);
var
  Node: TDOMNode;
  PageNo: Integer;
begin
  if not Assigned(ANode) then
    Exit;
  Node := ANode.FirstChild;
  while Assigned(Node) do
  begin
    if Node.CompareName('Page') = 0 then
    begin
      PageNo := StrToInt(Node.Attributes.GetNamedItem('number').NodeValue);
      ProcessPage(Node, PageNo);
    end;
    Node := Node.NextSibling;
    ProcessNodes(Node);
  end;
end;
var
  Doc: TXMLDocument;
begin
  ReadXMLFile(Doc, 'cc.xml');
  try
    ProcessNodes(Doc);
  finally
    Doc.Free;
  end;
end.
----------------------------------------------------------------------------

Thanks for you example!
BTW, I could not use FindNode('Pages'). Looks like is broke, isn't?

Marcos Douglas